您的位置:首页 > 其它

Hibernate_在应用程序中使用的Session的管理方案

2017-05-13 21:34 337 查看


class txFilter implements Filter{
public void doFilter(request,response,chain){
session = sf.opensession();
Utils.threadLocal.set(session);// 绑定到当前线程上
try{
tx = session.beginTransaction();
chain.doFilter();// 放行
tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}finally{
session.close();
Utils.threadLocal.remove();// 移除
}
}
}

class UserDao{
public void saveUser(User user){
session = Utils.threadLocal.get();
session.save(User);
}
}

class txFilter增强 implements Filter{
public void doFilter(request,response,chain){
session = sf.getCurrentsession();
try{
tx = session.beginTransaction();
chain.doFilter();// 放行
tx.commit();
}catch(Exception e){
tx.rollback();
throw e;
}
}
}

class UserDao增强{
public void saveUser(User user){
session = sessionFactory.getCurrentsession();
session.save(User);
}
}


/*

package cn.itcast.m_session_manage;

import org.hibernate.Session;

public class Utils {
public static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
}


*/

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory name="foo">
<!-- 配置数据库信息 -->
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- mysql连接配置 -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_20170423</property>
<!-- 配置连接mysql驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql用户名 -->
<property name="connection.username">root</property>
<!-- mysql密码 -->
<property name="hibernate.connection.password">root</property>

<!-- 其它配置 -->
<!-- 显示生成的SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL语 -->
<property name="hibernate.format_sql">false</property>

<!-- create:先删除,再创建。 update:如果表不存在就创建,不一样就更新,一样就什么都不做。 create-dorp:初始化时创建表,SessionFactory执行close()时删除表。
validate:验证表结构是否一致,如果不一致,就抛异常。 -->
<property name="hbm2ddl.auto">update</property>

<!-- 当配置为thread时,SessionFactory的getCurrentSession()方法就能使用了 -->
<property name="current_session_context_class">thread</property>

<!-- 导入映射文件 -->
<!-- <mapping resource="cn/itcast/a_helloworld/User.hbm.xml" /> -->

</session-factory>
</hibernate-configuration>

package cn.itcast.m_session_manage;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
* 应用程序操作类
*
* @author 风清杨
* @version V3.0
*
*/
public class App {
private static SessionFactory sessionFactory = new Configuration()//
.configure("cn/itcast/m_session_manage/myhibernate.cfg.xml")//
.buildSessionFactory();

// 要相使用SessionFactory.getCurrentSession()方法
// 需要在Hibernate主配置文件中配置current_session_context_class项。
// getCurrentSession()方法:
// >> 去指定的上下文中(如thread)查找绑定的Session对象,如果有就返回。
// >> 如果没有,就创建一个并绑定好,然后再返回
// >> openSession()只是开启一个新的Session,不会做绑定和查找操作。
@Test
public void testSession() throws Exception {
Session session1 = sessionFactory.getCurrentSession();
Session session2 = sessionFactory.getCurrentSession();

System.out.println(session1 != null);// true
System.out.println(session1 == session2);// true
}

// 当使用getCurrentSession时,Hibernate会在提交或回滚后自动的关闭Session
@Test
public void testSessionClose() throws Exception {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();

System.out.println("使用Session做xxx操作");

session.getTransaction().commit();
// session.close();//就不能再自已关闭了
}

@Test
public void testSession2() throws Exception {
Session session1 = sessionFactory.openSession();
Session session2 = sessionFactory.openSession();

System.out.println(session1 != null);// true
System.out.println(session1 == session2);// false
}

@Test
public void testSession3() throws Exception {
Session session1 = sessionFactory.openSession();
Session session2 = sessionFactory.getCurrentSession();

System.out.println(session1 != null);// true
System.out.println(session1 == session2);// false
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: