`
- 浏览:
58089 次
- 性别:
- 来自:
成都
-
hibernate getCurrentSession openSession区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭
这里getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
getCurrentSession () 使用当前的session
openSession() 重新建立一个新的session
在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
博文链接:https://shaqiang32.iteye.com/blog/201918
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
NULL 博文链接:https://zgdkik.iteye.com/blog/1835667
ssh整合中getCurrentSession()的使用,个人写的练习,备忘用,不建议下载
Session session = HibernateUitl.getSessionFactory().getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher) session.get(Teacher.class, 3); t.setName("yangtb2"); session.update(t); ...
Session session = HibernateSessionFactory.getCurrentSession(); SQLQuery query = session.createSQLQuery("select * from cats"); List<Cat> cats = query.addEntity(Cat.class).list(); ``` 在上面的代码中,...
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
2. Hibernate 中 openSession()和 getCurrentSession()的差别 Hibernate 是一个基于Java的持久层框架,它提供了对数据库的访问和操作能力。openSession()和getCurrentSession()是Hibernate中两个重要的Session创建...
这两个方法的区别在于,openSession()方法可以在一个线程中使用不同的Session,而getCurrentSession()方法则可以在同一线程中,保证使用同一个Session。 三、填空题解释 1. MVC的三个组成部分:MVC的三个组成部分...
mysql,jdbc,Hibernate,tomcat [mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试-简单] <br> 1 .数据库设计:库mydb 表events <br>EVENT_ID BIGINT(20) NOT NULL AUTOINC EVENT_DATE DATETIME...
6. Hibernate 框架中的 SessionFactory 类有两个获得 session 的方法:openSession() 和 getCurrentSession()。如果需要在同一线程中,保证使用同一个 Session 则使用 getCurrentSession(),如果在一个线程中,需要...
123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...
9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...
Hibernate 修改数据 1.用 HQL方式来更新 在 这里修改 Person 的name 和age 通过 id 标识 Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //创建 HQL String ...
Configuration cfg=new Configuration().configure("hibernate.cfg.xml"); SessionFactory sf=cfg.buildSessionFactory(); Session session=sf.getCurrentSession(); Transaction tx=session.beginTransaction();
实现一个基于hibernate3的基本的dao,实现CRUD; 一行代码完成CRUD,提供多种重载方法详见IBaseDao,但是项目需要基于SSH框架。 ============ 使用方法,以用户添加为例: 1、定义User类,并提供get和set方法。 2、...