spring事务实现方式
时间: 2024-05-16 21:13:14 浏览: 17
Spring框架提供了多种事务管理方式,包括编程式事务管理和声明式事务管理。
1. 编程式事务管理
编程式事务管理是通过编写代码实现事务管理,使用Spring提供的TransactionTemplate来管理事务。编程式事务管理的实现方式如下:
```
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private TransactionTemplate transactionTemplate;
@Override
public void transfer(String fromUserName, String toUserName, Double amount) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
try {
User fromUser = userDao.findByName(fromUserName);
User toUser = userDao.findByName(toUserName);
fromUser.setBalance(fromUser.getBalance() - amount);
toUser.setBalance(toUser.getBalance() + amount);
userDao.update(fromUser);
userDao.update(toUser);
} catch (Exception e) {
transactionStatus.setRollbackOnly();
throw new RuntimeException("转账失败");
}
}
});
}
}
```
2. 声明式事务管理
声明式事务管理是通过AOP的方式实现事务管理,可以将事务的管理与业务代码分离。声明式事务管理的实现方式如下:
(1)通过XML配置文件实现声明式事务管理
```
<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="transfer" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServicePointcut" expression="execution(public * com.example.service.UserService.transfer(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointcut"/>
</aop:config>
```
(2)通过注解方式实现声明式事务管理
```
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void transfer(String fromUserName, String toUserName, Double amount) {
try {
User fromUser = userDao.findByName(fromUserName);
User toUser = userDao.findByName(toUserName);
fromUser.setBalance(fromUser.getBalance() - amount);
toUser.setBalance(toUser.getBalance() + amount);
userDao.update(fromUser);
userDao.update(toUser);
} catch (Exception e) {
throw new RuntimeException("转账失败");
}
}
}
```
以上就是Spring事务的两种实现方式,可以根据实际情况选择合适的方式。
相关推荐
![png](https://img-home.csdnimg.cn/images/20210720083516.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)