java事物 tran_spring tranaction 事务入门

一、事务四个属性

原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。

一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。

隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。

持久性(durability)。持久性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

二、事务的重要性

打个最简单的比方吧,A和B两人之间的支付宝转账问题,A向B转账50RMB,正常的结果是,A - 50 并且 B + 50; 但如果是下面这种情况,那就杯具了,A - 50 成功,而B + 50 失败。这样一来岂不是 A亏大了!谁还敢随意转账?就算是首富,也不敢随意转账O(∩_∩)O哈!

所以,在进行 A - 50 和 B + 50 需要添加事务管理。

三、先看下没有加事务的Demo, 看完就知道事务的重要性啦~

(1)、整体结构、

8d53590ca63c274f2be217c043a8376c.png

(2)、jar 包

45111603290694795cfff2440c2f0ba1.png

(3)、SQL语句

创建数据库

create database spring;

建立表

create table countmoney(idCardint primary key auto_increment,name varchar(32),money int);

插入两条记录

insert into countmoney(name,money)values('xx',300);

insert into countmoney(name,money)values('++',300);

结果

select * from countmoney;

+--------+------+-------+

| idCard | name | money |

+--------+------+-------+

| 1 | xx | 300 |

| 2 | ++ | 300 |

+--------+------+-------+

(4)、代码

model 层

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.model;2

3 public classCount {4 private intidCard;5 privateString name;6 private intmoney;7

8 publicCount(){9

10 }11

12 public intgetIdCard() {13 returnidCard;14 }15 public void setIdCard(intidCard) {16 this.idCard =idCard;17 }18 publicString getName() {19 returnname;20 }21 public voidsetName(String name) {22 this.name =name;23 }24 public intgetMoney() {25 returnmoney;26 }27 public void setMoney(intmoney) {28 this.money =money;29 }30

31 }

View Code

dao 层

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.dao;2

3 public interfaceTradeDao {4

5 public void outputMoney(int idCard, intmoney);6

7 public void inputMoney(int idCard, intmoney);8 }

View Code

dao impl 层

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.dao.impl;2

3 importorg.springframework.jdbc.core.JdbcTemplate;4 importorg.springframework.jdbc.core.namedparam.MapSqlParameterSource;5 importorg.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;6 importorg.springframework.jdbc.core.namedparam.SqlParameterSource;7

8 importcom.xpw.dao.TradeDao;9

10 public class TradeDaoImpl implementsTradeDao {11

12 privateNamedParameterJdbcTemplate namedParameterJdbcTemplate;13

14 public voidsetNamedParameterJdbcTemplate(15 NamedParameterJdbcTemplate namedParameterJdbcTemplate) {16 this.namedParameterJdbcTemplate =namedParameterJdbcTemplate;17 }18

19 @Override20 public void outputMoney(int idCard, intcount) {21 String sql = "update trademoney set money = money -:count where idCard = :idCard";22 MapSqlParameterSource param = newMapSqlParameterSource();23 param.addValue("count", count);24 param.addValue("idCard", idCard);25 this.namedParameterJdbcTemplate.update(sql, param);26 }27

28 @Override29 public void inputMoney(int idCard, intcount) {30 //我们故意在此出错,抛出异常,让 B + 50失败

31 System.out.println(1/0);32 String sql = "update trademoney set money = money + :count where idCard = :idCard";33 MapSqlParameterSource param = newMapSqlParameterSource();34 param.addValue("count", count);35 param.addValue("idCard", idCard);36 this.namedParameterJdbcTemplate.update(sql, param);37 }38 }

View Code

Service层

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.service;2

3 public interfaceTradeService {4 public void trade(int fromIdCard, int toIdCard, intmoney);5 }

View Code

Service impl 层

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.service.impl;2

3 importcom.xpw.dao.TradeDao;4 importcom.xpw.service.TradeService;5

6 public class TradeServiceImpl implementsTradeService {7

8 privateTradeDao tradeDao;9

10 public voidsetTradeDao(TradeDao tradeDao) {11 this.tradeDao =tradeDao;12 }13

14 @Override15 public void trade(int fromIdCard, int toIdCard, intmoney) {16 this.tradeDao.outputMoney(fromIdCard, money);17 this.tradeDao.inputMoney(toIdCard, money);18 }19 }

View Code

(5)文件配置信息

beans.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:context="http://www.springframework.org/schema/context"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans.xsd7 http://www.springframework.org/schema/context8 http://www.springframework.org/schema/context/spring-context.xsd">

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

View Code

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/spring

jdbc.username=root

jdbc.password=root

(6)测试

package com.xpw.trade;

import org.junit.Before;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xpw.service.TradeService;

public class TradeTest {

private static ApplicationContext ac;

@Before

public void init(){

ac = new ClassPathXmlApplicationContext("beans.xml");

}

@Test

public void testTrade(){

TradeService ts = (TradeService) ac.getBean("tradeService");

int fromIdCard = 1;

int toIdCard = 2;

int money = 50;

ts.trade(fromIdCard, toIdCard, money);

}

}

结果

select * from trademoney;

+--------+------+-------+

| idCard | name | money |

+--------+------+-------+

| 1 | xx | 250 |

| 2 | ++ | 300 |

+--------+------+-------+

2 rows in set (0.00 sec)

由于,在 inputmoney()方法,我们故意 做1/0操作,也没有做try catch ,导致不会往下执行向B账户添加50的业务,所以 A亏了50。。。

从上面的结果我们知道了事务的重要性了吧。。A - 50 和 B + 50 必须同时成功,才可以称为一个成功的交易,一旦 谁出错,就必须回滚!即 不能 将 A - 50 , B 也不能 被 + 50

下面,我们就 添加事务管理吧。。当然,事务管理有两种,详情见如下

四、spring 事务分类

1、编程式事务管理

Spring 提供的事务模版类:org.springframework.transaction.support.TransactionTemplate

事务管理器:org.springframework.jdbc.datasource.DataSourceTransactionManager

service impl 层的代码有所改动(注意,便于阅者copy实践,我就把整个类的代码贴出来,下同)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.service.impl;2

3 importorg.springframework.transaction.TransactionStatus;4 importorg.springframework.transaction.support.TransactionCallback;5 importorg.springframework.transaction.support.TransactionCallbackWithoutResult;6 importorg.springframework.transaction.support.TransactionTemplate;7

8 importcom.xpw.dao.TradeDao;9 importcom.xpw.service.TradeService;10

11 public class TradeServiceImpl implementsTradeService {12

13 privateTradeDao tradeDao;14 privateTransactionTemplate transactionTemplate;15

16 public voidsetTradeDao(TradeDao tradeDao) {17 this.tradeDao =tradeDao;18 }19

20 public voidsetTransactionTemplate(TransactionTemplate transactionTemplate) {21 this.transactionTemplate =transactionTemplate;22 }23

24 //编程事务管理

25 @Override26 public void trade(final int fromIdCard, final int toIdCard, final intmoney) {27 this.transactionTemplate.execute(newTransactionCallbackWithoutResult() {28

29 @Override30 protected voiddoInTransactionWithoutResult(TransactionStatus arg0) {31 tradeDao.outputMoney(fromIdCard, money);32 tradeDao.inputMoney(toIdCard, money);33 }34 });35 }36 }

View Code

beans.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:context="http://www.springframework.org/schema/context"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans.xsd7 http://www.springframework.org/schema/context8 http://www.springframework.org/schema/context/spring-context.xsd">

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

View Code

其它的代码都没有变

结果:

mysql> select * from trademoney;

+--------+------+-------+

| idCard | name | money |

+--------+------+-------+

| 1 | xx | 300 |

| 2 | ++ | 300 |

+--------+------+-------+

2 rows in set (0.00 sec)

从上面的结果可以知道,编程式事务管理已经成功了,在 B + 50 失败了,回回滚,所以 A 不会 - 50

2、声明式事务管理

使用annotation

service impl 层

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.service.impl;2

3

4 importorg.springframework.transaction.annotation.Transactional;5

6 importorg.springframework.transaction.support.TransactionTemplate;7

8 importcom.xpw.dao.TradeDao;9 importcom.xpw.service.TradeService;10

11 @Transactional12 public class TradeServiceImpl implementsTradeService {13

14 privateTradeDao tradeDao;15 privateTransactionTemplate transactionTemplate;16

17 public voidsetTradeDao(TradeDao tradeDao) {18 this.tradeDao =tradeDao;19 }20

21 public voidsetTransactionTemplate(TransactionTemplate transactionTemplate) {22 this.transactionTemplate =transactionTemplate;23 }24

25 @Override26 public void trade(int fromIdCard, int toIdCard, intmoney) {27 this.tradeDao.outputMoney(fromIdCard, money);28 this.tradeDao.inputMoney(toIdCard, money);29 }30

31 }

View Code

beans.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:context="http://www.springframework.org/schema/context"

5 xmlns:aop= "http://www.springframework.org/schema/aop"

6 xmlns:tx="http://www.springframework.org/schema/tx"

7 xsi:schemaLocation="http://www.springframework.org/schema/beans8 http://www.springframework.org/schema/beans/spring-beans.xsd9 http://www.springframework.org/schema/context10 http://www.springframework.org/schema/context/spring-context.xsd11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd12 http://www.springframework.org/schema/aop13 http://www.springframework.org/schema/aop/spring-aop.xsd">

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

View Code

结果:

+--------+------+-------+

| idCard | name | money |

+--------+------+-------+

| 1 | xx | 300 |

| 2 | ++ | 300 |

+--------+------+-------+

2 rows in set (0.00 sec)

此方式成功 添加了事务管理

使用xml 方式

service impl 层

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xpw.service.impl;2

3 importcom.xpw.dao.TradeDao;4 importcom.xpw.service.TradeService;5

6 public class TradeServiceImpl implementsTradeService {7

8 privateTradeDao tradeDao;9

10 public voidsetTradeDao(TradeDao tradeDao) {11 this.tradeDao =tradeDao;12 }13

14

15 @Override16 public void trade(int fromIdCard, int toIdCard, intmoney) {17 this.tradeDao.outputMoney(fromIdCard, money);18 this.tradeDao.inputMoney(toIdCard, money);19 }20 }

View Code

beans.xml

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"

4 xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

5 xsi:schemaLocation="http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans.xsd7 http://www.springframework.org/schema/context8 http://www.springframework.org/schema/context/spring-context.xsd9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd10 http://www.springframework.org/schema/aop11 http://www.springframework.org/schema/aop/spring-aop.xsd">

12

13

14

15

16

17 destroy-method="close">

18

19

20

21

22

23

24

25

26 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44 class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">

45

46

47

48

49

50

51

52

53

54

55

View Code

结果

mysql> select * from trademoney;

+--------+------+-------+

| idCard | name | money |

+--------+------+-------+

| 1 | xx | 300 |

| 2 | ++ | 300 |

+--------+------+-------+

2 rows in set (0.00 sec)

五、总结

事务管理有编程式、声明式,本人推荐后者。因为前者,虽然实现了事务管理,但在一定程度上,非业务逻辑代码浸入了我们的业务逻辑代码,如果系统大型的话,也不可避免重复操作,代码看起来也不整洁了,也不方便后期维护。

【tip】转载请注明原文来自 :http://www.cnblogs.com/chenmo-xpw/p/3949264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值