hibernate openSession和getCurrentSession
来源:互联网 发布:mysql优化查询速度 编辑:程序博客网 时间:2024/05/17 22:50
开启一个Session 的两种方式:
1.openSession:每次都是一个新的Session
2.getCurrentSession:如果当前有session,就用当前的,否则新建一个
2.1.用途:界定事务边界
2.2. 在事务提交的时候自动close
2.3.必须在配置文件中配置,这个属性可以再文档中查找
<property name="hibernate.current_session_context_class">thread</property>
如果配置文件中没有设置,运行时会报异常No CurrentSessionContext configured,getCurrentSession是hibernate3.0以后加进去的新API,通过查看hibernate文档,我们可以知道hibernate默认是jta事务管理,所以单独用java应用程序测试是会报这样的异常的
还有一个问题:
假定SessionFactory实例已经被创建
Session session1=sessionFactory.openSession();
Session session2=sessionFactory.getCurrentSession();
System.out.println(session1==session2);//结果为false
原因:Session是一个接口,可能两个方法返回的是不同实现,具体得看hibernate源码才能知道
如果把好几个数据库操作放在同一个事务中,光靠数据库本身的connection管理根本不行,往往需要别人给你做一个事务管理器(TransactionManager)(记录对数据库的操作)
如果其中第二个数据库的操作出错,就让让第一个数据库回滚 XADriver驱动(支持对不同数据库操作)
TransactionManager有应用程序服务器提供,tomcat目前还不支持,不过可以通过spring来实现,weblogic可以,jboss可以
依赖于数据库本身,是Connection事务
分布式的数据库处理,依赖于应用程序服务器,叫jta事务
jta(java transaction API)
1.openSession:每次都是一个新的Session
2.getCurrentSession:如果当前有session,就用当前的,否则新建一个
2.1.用途:界定事务边界
2.2. 在事务提交的时候自动close
2.3.必须在配置文件中配置,这个属性可以再文档中查找
<property name="hibernate.current_session_context_class">thread</property>
如果配置文件中没有设置,运行时会报异常No CurrentSessionContext configured,getCurrentSession是hibernate3.0以后加进去的新API,通过查看hibernate文档,我们可以知道hibernate默认是jta事务管理,所以单独用java应用程序测试是会报这样的异常的
还有一个问题:
假定SessionFactory实例已经被创建
Session session1=sessionFactory.openSession();
Session session2=sessionFactory.getCurrentSession();
System.out.println(session1==session2);//结果为false
原因:Session是一个接口,可能两个方法返回的是不同实现,具体得看hibernate源码才能知道
如果把好几个数据库操作放在同一个事务中,光靠数据库本身的connection管理根本不行,往往需要别人给你做一个事务管理器(TransactionManager)(记录对数据库的操作)
如果其中第二个数据库的操作出错,就让让第一个数据库回滚 XADriver驱动(支持对不同数据库操作)
TransactionManager有应用程序服务器提供,tomcat目前还不支持,不过可以通过spring来实现,weblogic可以,jboss可以
依赖于数据库本身,是Connection事务
分布式的数据库处理,依赖于应用程序服务器,叫jta事务
jta(java transaction API)
- hibernate openSession和getCurrentSession
- hibernate opensession()和getCurrentSession()
- Hibernate中的getCurrentSession和openSession
- Hibernate之openSession和getCurrentSession
- Hibernate中openSession 和 getCurrentSession
- Hibernate中openSession 和 getCurrentSession
- hibernate openSession和getCurrentSession区别
- HIbernate中openSession和getCurrentSession
- hibernate之opensession和getCurrentSession
- Hibernate之openSession和getCurrentSession
- spring+Hibernate openSession()和getCurrentSession()
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- hibernate的session判断-openSession和getCurrentSession
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- asyncTask了解简记
- n个骰子的点数和
- android 开发环境配置
- java Class object
- hdu 1164 Eddy's research I (数论)
- hibernate openSession和getCurrentSession
- Linux音频编程指南
- 对时间管理妙招的评价
- C#正则表达式快速入门
- Hibernate 简单的annotation
- 修改镜像文件大小
- Linux 下采用开源MONO第一个C#程序
- Android socket和Tomcat等网络连接不上问题解决方案
- 博客网站排名前20