Hibernate txProxyTemplate 和声明事务
我有一个扩展这个 txProxyTemplate 的 bean,在它内部,这个方法 orderUpdateOverseer 调用 getHibernateTemplate().saveOrUpdate(someObject);
private void orderUpdateOverseer(OrderReturnState orderReturnState) throws ReturnsOrderUpdateException
我注意到,只有当我完全退出扩展 txProxyTemplate 的 bean 时,hibernate 才会刷新数据库的更新,而不是在我声明为新事务的 orderUpdateOverseer 方法退出时执行此操作。这是为什么?我做错了什么?
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="hibernateTransactionMgr" />
<property name="transactionAttributes">
<props>
<prop key="getAmos*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="orderUpdateOverseer">PROPAGATION_REQUIRES_NEW</prop>
<prop key="recordProcessOverseer">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
<prop key="merge*">PROPAGATION_REQUIRED</prop>
<prop key="split*">PROPAGATION_REQUIRED</prop>
<prop key="receive*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
I have a bean that extends this txProxyTemplate and inside it, this method orderUpdateOverseer calls getHibernateTemplate().saveOrUpdate(someObject);
private void orderUpdateOverseer(OrderReturnState orderReturnState) throws ReturnsOrderUpdateException
I noticed that hibernate flushes the updates to the database only when I fully exit the bean that extens txProxyTemplate instead of doing that on exit of the method orderUpdateOverseer that I have declared to be in a new transaction. Why is that? What am I doing wrong?
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="hibernateTransactionMgr" />
<property name="transactionAttributes">
<props>
<prop key="getAmos*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="orderUpdateOverseer">PROPAGATION_REQUIRES_NEW</prop>
<prop key="recordProcessOverseer">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
<prop key="merge*">PROPAGATION_REQUIRED</prop>
<prop key="split*">PROPAGATION_REQUIRED</prop>
<prop key="receive*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring 事务方面是基于代理的,因此它们不适用于
私有
方法以及从同一对象的另一个方法调用一个方法。如果您需要在新事务中调用同一对象的方法,最简单的方法是使用 程序化事务管理 (
TransactionTemplate
)。Spring transaction aspects are proxy-based, therefore they are not applied to
private
methods as well as to calls of a method from another method of the same object.If you need to call a method of the same object inside a new transaction, the simpliest way to do it is to use programmatic transaction management (
TransactionTemplate
).