spring使用事物的几种方法总结(转载)


转自:https://www.jb51.net/article/140158.htm#_lab2_1_0

目录

  • I. 前提
  • II. 实例演示
    • 1. 硬编码方式
    • 2. 基于TransactionProxyFactoryBean方式
    • 3. xml使用方式
    • 4. 注解方式
  • III. 小结
  • IV. 其他
    • 1. 参考
  • 总结

前言

本文主要记录下spring是如何支持事物的,以及在Spring结合mybatis时,可以怎么简单的实现数据库的事物功能,下面话不多说了,来一起看看详细的介绍吧。

http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.git.hui.demo.mybatis.mapper.MoneyDao">  <sql id="moneyEntity">  id, `name`, `money`, `isDeleted`, `created`, `updated`  sql>    <select id="queryMoney" resultType="com.git.hui.demo.mybatis.entity.MoneyEntity">  select  <include refid="moneyEntity"/>  from money  where id=#{id}    select>    <update id="incrementMoney">  update money  set money=money + #{addMoney}  where id=#{id}  update> mapper>

对应的mybatis连接数据源的相关配置

1 2 3 4 5 6 7 8 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 40 41 42 43 44 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  "locations">  classpath*:jdbc.properties       "dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  "driverClassName" value="${driver}"/>  "url" value="${url}"/>  "username" value="${username}"/>  "password" value="${password}"/>    "filters" value="stat"/>    "maxActive" value="20"/>  "initialSize" value="1"/>  "maxWait" value="60000"/>  "minIdle" value="1"/>    "timeBetweenEvictionRunsMillis" value="60000"/>  "minEvictableIdleTimeMillis" value="300000"/>    "validationQuery" value="SELECT 'x'"/>  "testWhileIdle" value="true"/>  "testOnBorrow" value="false"/>  "testOnReturn" value="false"/>    "poolPreparedStatements" value="true"/>  "maxPoolPreparedStatementPerConnectionSize" value="50"/>     "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  "dataSource" ref="dataSource"/>    "mapperLocations" value="classpath*:mapper/*.xml"/>     class="org.mybatis.spring.mapper.MapperScannerConfigurer">  "basePackage" value="com.git.hui.demo.mybatis"/>

http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="...  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd"   <tx:advice id="txAdvice" transaction-manager="transactionManager">  <tx:attributes>    <tx:method name="transfor" propagation="REQUIRED"/>  tx:attributes> tx:advice>     <aop:config>    <aop:pointcut expression="execution(* com.git.hui.demo.mybatis.repository.transaction.XmlDemo3.*(..))" id="pointcut1"/>    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/> aop:config>

观察上面的配置,再想想第二种方式,思路都差不多了,但是这种方式明显更加通用,通过切面和切点,可以减少大量的配置

b. 测试

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath*:spring/service.xml", "classpath*:test-datasource3.xml"}) public class XmlBeanTest {  @Autowired  private XmlDemo3 xmlDemo;    @Autowired  private MoneyDao moneyDao;      @Test  public void testTransfor() {    System.out.println("---------before----------");  System.out.println("id: 1 money = " + moneyDao.queryMoney(1).getMoney());  System.out.println("id: 2 money = " + moneyDao.queryMoney(2).getMoney());      xmlDemo.transfor(1, 2, 10, 0);    System.out.println("---------after----------");  System.out.println("id: 1 money = " + moneyDao.queryMoney(1).getMoney());  System.out.println("id: 2 money = " + moneyDao.queryMoney(2).getMoney());  } }

这个测试起来,和一般的写法就没啥两样了,比第二种的FactoryBean的注入方式简单点

正常输出

---------before----------
id: 1 money = 10000
id: 2 money = 50000
转账完成! now: 1526135301273
---------after----------
id: 1 money = 10010
id: 2 money = 49990

status=1 出现异常时,输出

---------before----------
id: 1 money = 10010
id: 2 money = 49990
转账异常!!!
---------after----------
id: 1 money = 10010
id: 2 money = 49990

status=2 转账过程中,又存钱的场景,输出,与前面预期一致

---------before----------
id: 1 money = 10010
id: 2 money = 49990
内部加钱: 1526135438403
sub modify success! now: 1526135438421
转账完成! now: 1526135441410
---------after----------
id: 1 money = 10220
id: 2 money = 49980

status=3 的输出,与前面预期一致

---------before----------
id: 1 money = 10220
id: 2 money = 49980
内部加钱: 1526135464341
转账完成! now: 1526135467349
sub modify success! now: 1526135467352
---------after----------
id: 1 money = 10230
id: 2 money = 50170

Spring事务管理的四种方式

源码

  • 项目源码:study-demo  (本地下载)
  • 主要查看包路径: 事物demo (本地下载)
  • 测试相关代码: 测试demo (本地下载)

 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。