项目架构 Hinbernate + Spring
使用spring声明式事物管理,配置如下:
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="HibernateSessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="moaService" expression="execution(public * com.teamsun.moa.service.Impl..*.*(..))" />
<aop:pointcut id="sdfService" expression="execution(public * com.teamsun.sdf.service.impl..*.*(..))" />
<aop:pointcut id="frameWorkService" expression="execution(public * com.teamsun.web.framework.service.impl..*.*(..))" />
<aop:pointcut id="formService" expression="execution(public * com.teamsun.web.framework.form.service.impl..*.*(..))" />
<aop:pointcut id="layoutService" expression="execution(public * com.teamsun.web.framework.layout.service.impl..*.*(..))" />
<aop:pointcut id="systemService" expression="execution(public * com.teamsun.web.framework.system.service.impl..*.*(..))" />
<aop:pointcut id="toolsService" expression="execution(public * com.teamsun.web.framework.tools.service.impl..*.*(..))" />
<aop:pointcut id="widgetService" expression="execution(public * com.teamsun.web.framework.widget.service.impl..*.*(..))" />
<aop:pointcut id="workflowService" expression="execution(public * com.teamsun.web.framework.workflow.service.impl..*.*(..))" />
<aop:advisor pointcut-ref="moaService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="sdfService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="frameWorkService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="formService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="layoutService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="systemService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="toolsService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="widgetService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="workflowService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
利用Aop切面管理所有业务service类,进行事物管理。
但service层在调用多个dao操作时,无法完成事物同步回滚。
DAO_A.save(a);
DAO_B.save(a);
当其中一个dao执行出错时,hinbernate不会立即抛出异常,而且等所有代码执行完毕后在抛出异常。
这时其中一个dao层的sql还会执行。不能同步回滚,不解。
利用手动回滚,可正常同步回滚事物。
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);;
/HibernateTransactionManager txManager = new HibernateTransactionManager();
TransactionStatus status = txManager.getTransaction(def);;
txManager.rollback(status);
请spring高手解释原因何在?是否配置有问题?
注:不使用TransactionProxyFactoryBean代理配置形式。
使用spring声明式事物管理,配置如下:
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="HibernateSessionFactory" />
</bean>
<aop:config>
<aop:pointcut id="moaService" expression="execution(public * com.teamsun.moa.service.Impl..*.*(..))" />
<aop:pointcut id="sdfService" expression="execution(public * com.teamsun.sdf.service.impl..*.*(..))" />
<aop:pointcut id="frameWorkService" expression="execution(public * com.teamsun.web.framework.service.impl..*.*(..))" />
<aop:pointcut id="formService" expression="execution(public * com.teamsun.web.framework.form.service.impl..*.*(..))" />
<aop:pointcut id="layoutService" expression="execution(public * com.teamsun.web.framework.layout.service.impl..*.*(..))" />
<aop:pointcut id="systemService" expression="execution(public * com.teamsun.web.framework.system.service.impl..*.*(..))" />
<aop:pointcut id="toolsService" expression="execution(public * com.teamsun.web.framework.tools.service.impl..*.*(..))" />
<aop:pointcut id="widgetService" expression="execution(public * com.teamsun.web.framework.widget.service.impl..*.*(..))" />
<aop:pointcut id="workflowService" expression="execution(public * com.teamsun.web.framework.workflow.service.impl..*.*(..))" />
<aop:advisor pointcut-ref="moaService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="sdfService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="frameWorkService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="formService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="layoutService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="systemService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="toolsService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="widgetService" advice-ref="txAdvice" />
<aop:advisor pointcut-ref="workflowService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
利用Aop切面管理所有业务service类,进行事物管理。
但service层在调用多个dao操作时,无法完成事物同步回滚。
DAO_A.save(a);
DAO_B.save(a);
当其中一个dao执行出错时,hinbernate不会立即抛出异常,而且等所有代码执行完毕后在抛出异常。
这时其中一个dao层的sql还会执行。不能同步回滚,不解。
利用手动回滚,可正常同步回滚事物。
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);;
/HibernateTransactionManager txManager = new HibernateTransactionManager();
TransactionStatus status = txManager.getTransaction(def);;
txManager.rollback(status);
请spring高手解释原因何在?是否配置有问题?
注:不使用TransactionProxyFactoryBean代理配置形式。