spring @Transaction 事务控制

1. 第一种用法
--spring.xml 配置文件中
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>



<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

--java service 代码中
@Service("userService")
@Transactional(readOnly = true)
public class UserServiceImpl implements IUserService {

@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void insertUserAndRole(User user, Role role) {
。。。。



2.第二种用法

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allServiceOperation" expression="execution(* com.frank.test.service.I*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceOperation"/>
</aop:config>



注意:

匹配格式: com.frank.test.service.I*(接口名).*(..)

猜你喜欢

转载自frank1998819.iteye.com/blog/1452299