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>
在SessionFactory启动的时候,Hibernate会根据配置创建相应的CurrentSessionContext,在 getCurrentSession()被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession()。在currentSession()执行时,如果当前Session 为空,currentSession 会调用SessionFactory 的openSession。所以getCurrentSession() 对于Java EE 来说是更好的获取Session 的方法。
sessionFactory.getCurrentSession()可以完成一系列的工作,当调用时,hibernate将session绑定到当前线程,事务结束后,hibernate将session从当前线程中释放,并且关闭session,当再次调用getCurrentSession()时,将得到一个新的session,并重新开始这一系列工作。
示例代码如下:
...
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
...
分享到:
相关推荐
博文链接:https://shaqiang32.iteye.com/blog/201918
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
NULL 博文链接:https://zgdkik.iteye.com/blog/1835667
ssh整合中getCurrentSession()的使用,个人写的练习,备忘用,不建议下载
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
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...
Hibernate 修改数据 1.用 HQL方式来更新 在 这里修改 Person 的name 和age 通过 id 标识 Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //创建 HQL String ...
123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...
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、...
9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...
Assert.assertNotNull(sessionFactory.openSession()); } 2,配置声明式事务(使用基于注解的方式) 1,配置 <!-- 配置事务管理器 --> ...
不带参数的情况下Web.config中一定要有hibernate-configuration节 5.连接是单线程的,在整个httpRequest处理过程中是公用的,缓存在HttpContext context中 see ref NHibernateHelper.getSession... 6.增加log4net演示,...
学生在学习jdbc的时候,会问到怎么调用存储过程,现在将java调用oracle存储过程的示例总结如下
Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql); query.setFirstResult(pageno); query.setMaxResults(3); //设置每页显示的条数 List result = ...
3)增强修复S2SH技术栈,不再使用openSession语句,而是使用getSessionFactory().getCurrentSession(), Spring 3,Spring 4通用,同时web.xml里增加SpringOpenSessionInViewFilter ===============0.7.19.2==========...