日期:2014-05-20  浏览次数:20715 次

Spring的事务管理
Spring的事务管理,例如:从Web传来的数据要同时插入两个表,在Spring的配置中怎么配置保证数据成功插入

------解决方案--------------------
如果是用配置的话,就是声明式事务,我这里说的是spring1.28的,

这里是用声明式的,先定义一个 TransactionManager 事务管理的类,
然后再定义一个事务管理的拦截器,对service中的function进行拦截.
如果是符合 add,delete,save等的名字,就处理事务~~

<bean id= "hibernateTransactionManager " class= "org.springframework.orm.hibernate3.HibernateTransactionManager ">
<property name= "sessionFactory ">
<ref bean= "sessionFactory " />
</property>
</bean>
<!-- bean id= "transactionManager " class= "org.springframework.transaction.jta.JtaTransactionManager "/ -->

<bean id= "transactionInterceptor " class= "org.springframework.transaction.interceptor.TransactionProxyFactoryBean " abstract= "true ">
<property name= "transactionManager ">
<ref bean= "hibernateTransactionManager "/>
</property>
<property name= "proxyTargetClass " value= "true "/>
<property name= "transactionAttributes ">
<props> // 定义要拦截的service函数,并进行不同的事务处理
<prop key= "add* "> PROPAGATION_REQUIRED </prop>
<prop key= "update* "> PROPAGATION_REQUIRED </prop>
<prop key= "delete* "> PROPAGATION_REQUIRED </prop>
<prop key= "set* "> PROPAGATION_REQUIRED </prop>
<prop key= "* "> PROPAGATION_REQUIRED,readOnly </prop>
</props>
</property>
</bean>


// service使用继承的方法,对service中的function进行拦截
<bean id= "columnService " parent= "transactionInterceptor ">
<property name= "target ">
<bean class= "com.cwq.modules.webtree.service.ColumnServiceImpl ">
<property name= "columnDao " ref= "columnDao " />
</bean>
</property>
</bean>


摘自:
http://jsp-tech.blogspot.com/2007/04/spring.html
具体的声明式事务:
http://jsp-tech.blogspot.com/2007/04/spring_25.html