技术开发 频道

让Spring架构减化事务配置



     在划分事务时,我们需要进行事务定义,也就是配置事务的属性。事务的属性有传播行业,隔离级别,超时值及只读标志。TransactionAttribute接口指定哪些异常将导致一个回滚,哪些应该一次性提交。

(1) 使用ProxyFactoryBean 和TransactionInterceptor
<!--定义本地数据源--> <bean id="dataSource" name="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
<!-- !定义单个jdbc数据源的事务管理器--> <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
<!—定义拦截器--> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop> </props> </property> </bean>
<!—定义业务对象--> <bean id="com.prs.application.ehld.sample.biz.service.sampleService.target" class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl"> <property name="userInfoDAO" ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO"> </property> </bean>
0
相关文章