资讯专栏INFORMATION COLUMN

基于Spring中的事务管理机制

SnaiLiu / 2249人阅读

摘要:中的事务管理分类编程式事务管理机制声明式事务管理机制下面就银行转账这一实例来讲解如何利用这两种由提供的事务处理机制来进行相应的事务处理。

什么是事务?

通俗理解,事务其实就是一系列指令的集合。

为什么要使用事务管理?

</>复制代码

  1. 我们在实际业务场景中,经常会遇到数据频繁修改读取的问题。在同一时刻,不同的业务逻辑对同一个表数据进行修改,这种冲突很可能造成数据不可挽回的错乱,所以我们需要用事务来对数据进行管理。
事务的四个特性:

原子性
原子性:操作这些指令时,要么全部执行成功,要么全部不执行。只要其中一个指令执行失败,所有的指令都执行失败,数据进行回滚,回到执行指令前的数据状态。

一致性
事务的执行使数据从一个状态转换为另一个状态,但是对于整个数据的完整性保持稳定。

隔离性
在该事务执行的过程中,无论发生的任何数据的改变都应该只存在于该事务之中,对外界不存在任何影响。只有在事务确定正确提交之后,才会显示该事务对数据的改变。其他事务才能获取到这些改变后的数据。

持久性
当事务正确完成后,它对于数据的改变是永久性的。

Spring中的事务管理分类:

编程式事务管理机制

声明式事务管理机制

下面就银行转账这一实例来讲解如何利用这两种由Spring提供的事务处理机制来进行相应的事务处理。

项目前准备工作:

在项目中导入相应的jar包

编写项目相应的接口和接口实现类,本项目有两个接口--【AccountService,AccountDAO】,两个接口实现类--【AccountServiceImpl,AccountDAOImpl】,具体代码如下:

</>复制代码

  1. **AccountService接口**

package cn.muke.spring.demo1;
/**

</>复制代码

  1. * @author 熊涛

</>复制代码

  1. *银行转账的业务层接口
  2. */
  3. public interface AccountService {
  4. /*
  5. * @param out:转出账户
  6. * @param in:转入账户
  7. * @param money:转账金额
  8. */
  9. public void transfer(String out,String in,Double money);
  10. }
  11. **AccountService接口实现类AccountServiceImpl**
  12. package cn.muke.spring.demo1;
  13. import org.springframework.transaction.TransactionStatus;
  14. import org.springframework.transaction.support.TransactionCallbackWithoutResult;
  15. import org.springframework.transaction.support.TransactionTemplate;
  16. public class AccountServiceImpl implements AccountService {
  17. //注入转账的DAO
  18. private AccountDAO accountDao;
  19. //注入事务管理的模板
  20. private TransactionTemplate transactionTemplate;
  21. /**
  22. * @param out :转出账号
  23. * @param in :转入账号
  24. * @param money :转账金额
  25. */
  26. @Override
  27. public void transfer(final String out, final String in, final Double money) {
  28. transactionTemplate.execute(new TransactionCallbackWithoutResult() {
  29. @Override
  30. protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
  31. accountDao.outMoney(out, money);
  32. int i = 1/0;
  33. accountDao.inMoney(in, money);
  34. }
  35. });
  36. }
  37. public void setAccountDao(AccountDAO accountDao) {
  38. this.accountDao = accountDao;
  39. }
  40. public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
  41. this.transactionTemplate = transactionTemplate;
  42. }
  43. }
  44. **AccountDAO接口**
  45. package cn.muke.spring.demo1;
  46. /**
  47. * @author 熊涛
  48. *转账案例DAO层的接口
  49. */
  50. public interface AccountDAO {
  51. /*
  52. * @param out:转出账号
  53. * @param money:转账金额
  54. */
  55. public void outMoney(String out,Double money);
  56. /*
  57. * @param in:转入账户
  58. * @param money:转账金额
  59. */
  60. public void inMoney(String in,Double money);
  61. }
  62. **AccountDAO接口的实现类AccountDAOImpl**

package cn.muke.spring.demo1;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
/**

@author 熊涛

*转账案例DAO层的实现类
*/
public class AccountDAOImpl extends JdbcDaoSupport implements AccountDAO {

</>复制代码

  1. /*
  2. * @param out:转出账号
  3. * @param money:转账金额
  4. */
  5. @Override
  6. public void outMoney(String out, Double money) {
  7. String sql = "update account set money = money - ? where name = ? ";
  8. this.getJdbcTemplate().update(sql, money,out);
  9. }
  10. /*
  11. * @param in:转入账户
  12. * @param money:转账金额
  13. */
  14. @Override
  15. public void inMoney(String in, Double money) {
  16. String sql = "update account set money = money + ? where name = ? ";
  17. this.getJdbcTemplate().update(sql, money,in);
  18. }

}

编程式事务管理机制

</>复制代码

  1. package cn.muke.spring.demo1;
  2. import org.springframework.transaction.TransactionStatus;
  3. import org.springframework.transaction.support.TransactionCallbackWithoutResult;
  4. import org.springframework.transaction.support.TransactionTemplate;
  5. public class AccountServiceImpl implements AccountService {
  6. //注入转账的DAO
  7. private AccountDAO accountDao;
  8. //注入事务管理的模板
  9. private TransactionTemplate transactionTemplate;
  10. /**
  11. * @param out :转出账号
  12. * @param in :转入账号
  13. * @param money :转账金额
  14. */
  15. @Override
  16. public void transfer(final String out, final String in, final Double money) {
  17. transactionTemplate.execute(new TransactionCallbackWithoutResult() {
  18. @Override
  19. protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
  20. accountDao.outMoney(out, money);
  21. int i = 1/0;
  22. accountDao.inMoney(in, money);
  23. }
  24. });
  25. }
  26. public void setAccountDao(AccountDAO accountDao) {
  27. this.accountDao = accountDao;
  28. }
  29. public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
  30. this.transactionTemplate = transactionTemplate;
  31. }
  32. }

解释:主要是通过transaction提供的模板来进行事务的处理。

使用声明式的方法进行事务处理 【1】基于TransactionProxyFactoryBean的事务处理机制

要点:(1)主要是在Spring的配置文件中配置业务层的代理,代码例如:

</>复制代码

  1. PROPAGATION_REQUIRED

(2)在使用业务层时需注入业务层的代理

</>复制代码

  1. @Resource(name="accountServiceProxy")

【2】基于AspectJ的XML方式

</>复制代码

  1. 此种方式是利用了Spring AOP这一特性完成事务管理机制。其要点是配置事务通知和切面和切点,而且此种代理为自动代理。其在Spring配置文件的配置如下:

【3】基于注解的事务管理机制

</>复制代码

  1. 其要点是在Spring配置文件完成事务的开启,另外还需在需要进行事务管理的业务层类出标记上事务的关键字,@Tansactional

最后附上对于该项目的源码,源码中有一个名为database的文件改文件有关于创建数据库的sql语句,另外源码中海油对于每一种事务管理机制的测试类。
链接:https://pan.baidu.com/s/1hspMvnY 密码:g2h7

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70877.html

相关文章

  • 2021 年最新基于 Spring Cloud 的微服务架构分析

    摘要:是一个相对比较新的微服务框架,年才推出的版本虽然时间最短但是相比等框架提供的全套的分布式系统解决方案。提供线程池不同的服务走不同的线程池,实现了不同服务调用的隔离,避免了服务器雪崩的问题。通过互相注册的方式来进行消息同步和保证高可用。 Spring Cloud 是一个相对比较新的微服务框架,...

    cikenerd 评论0 收藏0
  • Spring Cloud 分布式事务管理

    摘要:中大致分为两部分事务管理器和本地资源管理器。具体实现分布式事务框架的核心功能是对本地事务的协调控制,框架本身并不创建事务,只是对本地事务做协调控制。 Spring Cloud 分布式事务管理 在微服务如火如荼的情况下,越来越多的项目开始尝试改造成微服务架构,微服务即带来了项目开发的方便性,又提高了运维难度以及网络不可靠的概率. @[toc]在说微服务的优缺点时,有对比才会更加明显,首先...

    aboutU 评论0 收藏0
  • Java相关

    摘要:本文是作者自己对中线程的状态线程间协作相关使用的理解与总结,不对之处,望指出,共勉。当中的的数目而不是已占用的位置数大于集合番一文通版集合番一文通版垃圾回收机制讲得很透彻,深入浅出。 一小时搞明白自定义注解 Annotation(注解)就是 Java 提供了一种元程序中的元素关联任何信息和着任何元数据(metadata)的途径和方法。Annotion(注解) 是一个接口,程序可以通过...

    wangtdgoodluck 评论0 收藏0

发表评论

0条评论

SnaiLiu

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<