-
getCurrentSession()获得的sessoin不受Spring的事务管理?这个问题困扰了我好几天。10
之前项目里没有spring,一切正常,dao里用到的session都是通过sessionFactory.getCurrentSession()获得的,手工启动事务,filter统一提交或回滚。
说明hibernate的配置没有问题。
然后引入了spring。
spring的配置里的hibernate是这么初始化的,应该也无问题<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> </bean>
然后声明式事务管理引用<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="systemService" expression="execution(* com.orm.system.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="systemService"/>
</aop:config>
Dao都是继承自HibernateDaoSupport,接受spring注入的sessionFactory实例。切面是配置在service层的,service的方法里调用dao的方法。
问题出来了。
在任何一个Dao里面
如果使用this.getSession()则session正常关闭,调用多次也没问题。
如果使用this.getHibernateTemplate().getSessionFactory().getCurrentSession(),始终打开1个session并且不能关闭。
我觉得如果使用this.getSession()正常,所以事务配置应该是没有问题的。
而且有一点,如果是通过getCurrentSession()获得的session必须要beginTransaction,不然报错,而this.getSession()则不用。
后来我配置了OpenSessionInViewFilter,发现这里打开和关闭的session和dao里面this.getSession()获得的session是同一个,而getCurrentSession()获得的不是同一个,
this.getSession().getTransaction().isActive() = true,
getCurrentSession().getTransaction().isActive() = false,
说明getCurrentSession()获得的session并不在事务管理里。
我看了很多东西,一些是说getCurrentSession()在spring的事务管理,一些是说通过调用spring创建的sessionFactory的getCurrentSession()方法获得的session在spring的事务管理(难道还有不通过spring创建的sessionFactory?)。我做了一些测试,结果表明自始至终sessionFactory都只有一个实例。
所以我得出结论,虽然sessionFactory受spring管理,但是直接从sessionFactory获取的session(不管是openSession还是getCurrentSession)不受spring的事务管理,是原生的Hibernate session。
不知道我这样理解是否有问题,还是我哪里做错了得出了错误的结果?
问题补充
没有人研究过这个问题?还是太新手了不屑回答啊?
问题补充:<div class="quote_title">xiejielin 写道</div><div class="quote_div">CurrentSession有Context,配置什么context,获取相应的Session和事务,这个很关键。 <br />Hibernate自带的context好像是三种:Thread,JTA,还有一个是什么忘了,o(∩_∩)o <br /> <br />如果用Spring管理session,在spring里配置SessionFactory的时候会把Context换成Spring自己提供的context,这个时候用CurrentSession就是从spring的事务管理体系里面获取session。 <br />如果进一步用了SPRING OSIV,在filter和currentSession()方法里,都是从spring的事务体系里拿session,就不能再为hibernate配置CurrentSession的context了。 <br /> <br />进源码,调试,跟踪session的来源,摸清了你就清楚了。</div> <br /> <br />一开始我也是这么认为的,hibernate的session是绑定到thread的。按照你的说法,getCurrentSession获得的session也是能在spring的管理里的了?那请问应该怎么配置?
问题补充:<div class="quote_title">xiejielin 写道</div><div class="quote_div">不需要配置,在applicationContext.xml里配spring的sessionFactory,会强行这么做。只要注意<strong>别</strong>在hibernate的设置中加 <br /><property name="hibernate.current_session_context_class"></property> <br /> <br />SessionHolder 你关注一下Spring里这个类。这个是从OSIV开始,到后面currentSession()都会用到的。 <br /></div> <br /> <br />确实是这样,把这个配置去掉就没问题了。 <br /><property name="hibernate.current_session_context_class">thread</property> <br /> <br />就是说,这句配置是有hibernate负责把session绑定到线程而不是spring,因此session不受spring管辖对吗?如果没有这一句,spring会负责把session绑定到线程的工作。2011年6月20日 09:38
5个答案 按时间排序 按投票排序
-
采纳的答案
不需要配置,在applicationContext.xml里配spring的sessionFactory,会强行这么做。只要注意别在hibernate的设置中加
<property name="hibernate.current_session_context_class"></property>
SessionHolder 你关注一下Spring里这个类。这个是从OSIV开始,到后面currentSession()都会用到的。2011年6月20日 12:52
-
引用就是说,这句配置是有hibernate负责把session绑定到线程而不是spring,因此session不受spring管辖对吗?如果没有这一句,spring会负责把session绑定到线程的工作。
是的!2011年6月21日 08:47
-
给你一个简单的判断原则,Spring新的作用域有五种,singleton,prototype,request,session,global session.最后三种是在web应用中使用的,都受web的ApplicationContext管,singleton是受管的,只有prototype不受管。
2011年6月20日 19:47
-
CurrentSession有Context,配置什么context,获取相应的Session和事务,这个很关键。
Hibernate自带的context好像是三种:Thread,JTA,还有一个是什么忘了,o(∩_∩)o
如果用Spring管理session,在spring里配置SessionFactory的时候会把Context换成Spring自己提供的context,这个时候用CurrentSession就是从spring的事务管理体系里面获取session。
如果进一步用了SPRING OSIV,在filter和currentSession()方法里,都是从spring的事务体系里拿session,就不能再为hibernate配置CurrentSession的context了。
进源码,调试,跟踪session的来源,摸清了你就清楚了。2011年6月20日 10:50
-
如果你单独从SessionFactory获取Session,那么当然不在Spring事物管理里面。因为这个不是Spirng处理过的。Spring在你进入事物点的时候就开启了一个Session供你使用。如果你不使用这个,单独启动一个新的Session,Spring不知道有新的Session,那么当然无法替你管理事务。
某个类的实例受Spring管理,不代表它产生的对象也一定受Spring管理。2011年6月20日 09:38
相关推荐
ssh整合中getCurrentSession()的使用,个人写的练习,备忘用,不建议下载
面试题包括以下十九部分:Java 基础、容器、多线程、反射、对象拷贝、Java Web 模块、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、Mybatis、RabbitMQ、Kafka、Zookeeper、MySql...
NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085
博文链接:https://shaqiang32.iteye.com/blog/201918
NULL 博文链接:https://zgdkik.iteye.com/blog/1835667
我可不可以写个类继承于String? 14 4、谈谈&和&&的区别? 14 5、Switch语句里面的条件可不可以是byte、long、String?使用时候还应注意什么? 14 6、short s1=1;s1=s1+1;有什么错?short s1 = 1;s1+=1 有什么错? ...
一,集成 Spring 与 Hibernate 1,配置SessionFactory 1,配置 ---------------------- applicationContext.xml ------------------------ <!-- 配置SessionFactory(整合Hibernate) --> ...
基于Oracle自带范例HR数据库做一个一个简单的演示,实现数据库的增加,删除,修改,查询,我用的是Oracle9208,代码简单,就算不能运行,也可以参考,简单改一下可以连接你自己的数据库Table演示, 演示特性如下: ...
三大框架分页的实现: DAO里写法: //查出页面要显示的字段 -----分页代码 页面上的查询全部 ... <property name="newsclassService"> //这个名字要和action里定义的那个成员变量对应 </bean>
学生在学习jdbc的时候,会问到怎么调用存储过程,现在将java调用oracle存储过程的示例总结如下
3)增强修复S2SH技术栈,不再使用openSession语句,而是使用getSessionFactory().getCurrentSession(), Spring 3,Spring 4通用,同时web.xml里增加SpringOpenSessionInViewFilter ===============0.7.19.2==========...
hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate
实现一个基于hibernate3的基本的dao,实现CRUD; 一行代码完成CRUD,提供多种重载方法详见IBaseDao,但是项目需要基于SSH框架。 ============ 使用方法,以用户添加为例: 1、定义User类,并提供get和set方法。 2、...
Session currentSession = H3Utils.getCurrentSession(); currentSession.beginTransaction(); //创建 HQL String hqlString = update Person p set p.name=? , p.age=? where p.id=?; //构建 Query Query ...
Session session1 =sessionFactory.getCurrentSession(); session1.beginTransaction(); <br> String title="jkh" ; Date theDate= new Date(); Event theEvent = new Event(); theEvent....
编写对DAO编写单元测试[4]publicabstractclassTransactionCallback{publicfinalObjectexecute()throwsException{Transactiontx=HibernateUtil.getCurrentSession().beginTransaction();try{Objectr=doInTransaction...