Sha*_*yan 10 spring hibernate hibernate-session
我有一个应用程序,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>
Run Code Online (Sandbox Code Playgroud)
它在生产中运作良好.
现在我们正在迁移到另一个项目Hibernate4.我们复制了相同的配置,除了使用来自org.springframework.orm.hibernate4.*包的Hierrnate 4的SessionFactory,TransacionManager等.然而,它开始给出例外说"没有活动交易时保存无效".搜索了一下后,似乎有很多人遇到了问题,有些人建议不要使用
<prop key="hibernate.current_session_context_class ">thread</prop>
Run Code Online (Sandbox Code Playgroud)
财产和它的工作.它对我也有用.我可以从帖子中收集的所有小信息都与上下文会话和线程策略有关,干扰Spring的会话管理策略.但没有我能找到任何具体答案的地方.另外,为什么它适用于Hibernate3而不适用于Hibernate4.有什么不同,有什么变化?每个其他配置都是相同的.我不是使用@Transactional而是使用旧式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>
Run Code Online (Sandbox Code Playgroud)
M. *_*num 10
它取决于spring的版本,但一般来说,使用Spring时必须避免使用Spring(除非你使用JTA进行事务,然后你需要配置它).
从Hibernate 3.1开始,有一些叫做上下文会话的东西,为此hibernate提供了CurrentSessionContext接口.这有几个实现(并且thread是简称ThreadLocalSessionContext).Spring有自己的SpringSessionContext类接口实现.
Spring默认情况下将属性设置为Springs实现CurrentSessionContext,当这被设置为其他东西时(除了JTA之外),这将破坏spring管理hibernate会话(以及事务)的能力.
现在在旧版本的spring(我假设你也升级了Spring以便能够使用hibernate)与hibernate 3相结合,还有其他一些关于获取会话的技巧(由于向后兼容旧的3.x版本的hibernate).这使得Spring更少依赖于该属性的价值.(Spring创建了一个代理,SessionFactory并且基本上拦截了getSession能够管理会话的方法).
| 归档时间: |
|
| 查看次数: |
20593 次 |
| 最近记录: |