spring 使用 current_session_context_class 属性 hibernate 3 hibernate 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19875485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using current_session_context_class property hibernate 3 hibernate 4
提问by Shailesh Vaishampayan
I have an application with Spring and Hibernate3 running well in production. Following is the config for session factory in Spring's applicationContext.xml
我有一个在生产环境中运行良好的 Spring 和 Hibernate3 应用程序。以下是 Spring 的 applicationContext.xml 中会话工厂的配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/hibernate</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.autocommit">false</prop>
<prop key="hibernate.current_session_context_class ">thread</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean
below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="count*" propagation="SUPPORTS" read-only="true" />
<tx:method name="validate*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="login" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any execution of
an operation defined by the service interfaces -->
<aop:config>
<aop:pointcut id="projectServiceOperation"
expression="execution(* com.service.project.IProjectService.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="projectServiceOperation" />
</aop:config>
It is working fine in production.
它在生产中运行良好。
Now for another project we are migrating to Hibernate4. we copied over the same configuration except using Hibernate 4's SessionFactory,TransacionManager etc. from org.springframework.orm.hibernate4.* package. However it started giving exception saying "Save is not valid without active transaction". After searching a bit many people seemed to have faced the problems and several people suggested not to use
现在对于另一个项目,我们正在迁移到 Hibernate4。我们复制了相同的配置,除了使用来自 org.springframework.orm.hibernate4.* 包的 Hibernate 4 的 SessionFactory、TransacionManager 等。但是它开始给出异常说“没有活动交易,保存无效”。经过一番搜索,似乎很多人都遇到了问题,有几个人建议不要使用
<prop key="hibernate.current_session_context_class ">thread</prop>
property and it worked. It also worked for me. All little information I could gather from the posts that it has something to do with contextual sessions and thread strategy interfering with Spring's session management strategy. But no where I could find any concrete answer.
Also, why did it work with Hibernate3 and not with Hibernate4. What is the difference and what has changed? Every other configuration is same. I am not using @Transactional but the old school XML way.
财产和它的工作。它也对我有用。我可以从帖子中收集到的所有信息都与上下文会话和干扰 Spring 会话管理策略的线程策略有关。但没有地方我可以找到任何具体的答案。
另外,为什么它适用于 Hibernate3 而不适用于 Hibernate4。有什么不同,发生了什么变化?所有其他配置都相同。我没有使用@Transactional,而是使用老派的 XML 方式。
Can somebody point me to a clear explanation for this behavioral difference between Hibernate3 and Hibernate4?
有人能指出我对 Hibernate3 和 Hibernate4 之间这种行为差异的明确解释吗?
采纳答案by M. Deinum
It depends on the version of spring but in general messing around with that property whilst using Spring must be avoided (unless you use JTA for transactions then you need to configure this).
这取决于 spring 的版本,但通常必须避免在使用 Spring 时弄乱该属性(除非您使用 JTA 进行事务,否则您需要配置它)。
As of Hibernate 3.1 there is something called contextual sessions and for that hibernate provides the CurrentSessionContext
interface. There are several implementations for this (and thread
is short for ThreadLocalSessionContext
). Spring has its own implementation of this interface the SpringSessionContext
class.
从 Hibernate 3.1 开始,有一种叫做上下文会话的东西,并且为此 Hibernate 提供了CurrentSessionContext
接口。对此有多种实现(thread
是 的缩写ThreadLocalSessionContext
)。Spring有自己实现这个接口的SpringSessionContext
类。
Spring by default sets the property to Springs implementation of the CurrentSessionContext
, when this is set to something else (other then JTA) this will break springs ability to manage the hibernate session (and thus the transaction).
Spring 默认将属性设置为 的 Springs 实现CurrentSessionContext
,当它设置为其他东西(除了 JTA)时,这将破坏 springs 管理休眠会话(以及事务)的能力。
Now in older versions of spring (and I assume you also upgraded Spring to be able to use hibernate) combined with hibernate 3 there was some other trickery involved regarding getting the session (due to backwards compatibility with older 3.x versions of hibernate). This made Spring less depended on the value of that property. (Spring created a proxy for the SessionFactory
and basically intercepted the getSession
method to be able to manage the session).
现在在旧版本的 spring(我假设你也升级了 Spring 以能够使用 hibernate)与 hibernate 3 结合时,还有一些其他的技巧涉及获取会话(由于向后兼容旧的 3.x 版本的 hibernate)。这使得 Spring 不那么依赖于该属性的价值。(Spring 为 建立了一个代理SessionFactory
,基本上拦截了getSession
方法就可以管理会话了)。