Hibernate openSession() 和 getCurrentSession的区别
来源:互联网 发布:中国网络管理条例 编辑:程序博客网 时间:2024/04/30 03:31
1、getCurrentSession()与openSession()的区别?
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()
创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()
创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
栗子:
package
com.myj.hibernate.model;
import
org.hibernate.HibernateException;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.AnnotationConfiguration;
import
org.hibernate.classic.Session;
import
org.junit.AfterClass;
import
org.junit.BeforeClass;
import
org.junit.Test;
public
class
TeacherTest {
public
static
SessionFactory sessionFactory =
null
;
@BeforeClass
public
static
void
beforeClass() {
try
{
sessionFactory =
new
AnnotationConfiguration().configure().buildSession Factory();
}
catch
(HibernateException e) {
e.printStackTrace();
}
}
@Test
public
void
testTeacherSave() {
Teacher teacher =
new
Teacher();
teacher.setName(
"martian"
);
teacher.setAge(
23
);
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(teacher);
//测试getCurrentSession()方法,这里输出true,因为在一个事务内,所以取得线程中的session
Session session1 = sessionFactory.getCurrentSession();
System.out.println(session == session1);
//commit()之后不用close(),假如使用sessionFactory.openSession();就需要close();
session.getTransaction().commit();
//测试getCurrentSession()方法,这输出false,上一个事务已经提交,这里将重新生成一个session
Session session2 = sessionFactory.getCurrentSession();
System.out.println(session == session2);
}
@AfterClass
public
static
void
afterClass() {
sessionFactory.close();
}
}
阅读全文
0 0
- hibernate 的openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate: openSession() 和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别 .
- Hibernate的openSession和getCurrentSession区别
- hibernate中 openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- 【Hibernate】OpenSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate getCurrentSession()和openSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- 杭电1016 Prime Ring Problem
- 数字图像处理——用Java对数字图像进行读写
- 适配器模式
- 关于接口的一些问题
- 【arduino】通过Esp8266-01模块实现的WiFi通信(1)简介和搭建编程环境
- Hibernate openSession() 和 getCurrentSession的区别
- 51nod1125 交换机器的最小代价(贪心)
- 用递归解决Fibonacci问题
- Activity的四种启动模式
- linux下安装composer
- Inception-v4,Inception-ResNet and the Impact of Residual Connections on Learning
- 第三周学习
- hdu1312 Red and Black_广搜 dfs(java版本)
- 大数据的相关性和因果性