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 } }
相关文章推荐
- 在B/S结构的应用程序中使用Session的管理方案
- hibernate,spring管理事务中(transaction,JDBC connection,Hibernate Session的使用)
- hibernate使用spring的事务管理器在测试dao的时候获取不到session
- 使用OpenSessionInViewFilter方案解决hibernate懒加载的异常
- 使用ThreadLocal模式管理Hibernate的Session
- hibernate 管理 Session(单独使用session,不spring)
- hibernate 管理 Session(单独使用session,非spring)
- hibernate 管理 Session(单独使用session,非spring)
- 动态WEB应用中的用户SESSION管理方案
- Hibernate/Spring/Struts架构使用OpenSessionInView的问题
- Hibernate中的Session和Transactaion的管理
- 使用ColdFusion和Flex构建您的第一个数据管理应用程序
- 第七章 Session的使用与管理(书摘)
- Hibernate Tip: 使用JBOSS MBEAN时,Session会在事务提交后自动关闭!
- Spring管理Hibernate开发 net/sf/hibernate/Session
- 使用Microsoft® Visual Studio® 2008解决软件开发中的挑战-应用程序生命周期管理
- 使用wsadmin命令脚本管理应用程序
- hibernate包的导入,这样才可以正常使用hibernateSessionFactory
- Hibernate/Spring/Struts架构使用OpenSessionInView的问题
- 解决了Hibernate+Spring+Struts架构使用OpenSessionInView的问题