0.项目结构

具体代码见:https://github.com/xkzhangsan/spring-transaction-practice.git,包括创建表sql在内。

1.编程式事务使用DataSourceTransactionManager git对应版本: v0.0.1

(1)Spring的xml配置:spring\app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <description>Example configuration to get you started.</description> <context:component-scan base-package="com.xkzhangsan.tx" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/xkspringmvc"></property>
<property name="user" value="xktest"></property>
<property name="password" value="123456"></property>
</bean> <bean id="accountDao" class="com.xkzhangsan.tx.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="accountService" class="com.xkzhangsan.tx.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean> <!-- 配置事务管理器 ,管理器需要事务,事务从Connection获得,连接从连接池DataSource获得 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>

(2)AccountServiceImpl

package com.xkzhangsan.tx.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition; import com.xkzhangsan.tx.dao.AccountDao;
import com.xkzhangsan.tx.service.AccountService; public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; @Autowired
private DataSourceTransactionManager dataSourceTransactionManager; public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, int money) {
DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); // 定义事务属性
transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED); // 设置传播行为属性
TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef); // 获得事务状态
try {
// 数据库操作
accountDao.out(outer, money);
int i = 1/0;
accountDao.in(inner, money); dataSourceTransactionManager.commit(status);// 提交
} catch (Exception e) {
dataSourceTransactionManager.rollback(status);// 回滚
}
} }

2.声明式事务基于AOP的xml配置 git对应版本: v0.0.2

(1)Spring的xml配置:spring\app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <description>Example configuration to get you started.</description> <context:component-scan base-package="com.xkzhangsan.tx" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/xkspringmvc"></property>
<property name="user" value="xktest"></property>
<property name="password" value="123456"></property>
</bean> <bean id="accountDao" class="com.xkzhangsan.tx.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="accountService" class="com.xkzhangsan.tx.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean> <!-- 1 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 2 事务详情(事务通知) , 在aop筛选基础上,比如对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
<tx:attributes> 用于配置事务详情(属性属性)
<tx:method name=""/> 详情具体配置
propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
isolation 隔离级别
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice> <!-- 3 AOP编程,利用切入点表达式从目标类方法中 确定增强的连接器,从而获得切入点 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xkzhangsan.tx.service..*.*(..))"/>
</aop:config> </beans>

(2)AccountServiceImpl

package com.xkzhangsan.tx.service.impl;

import com.xkzhangsan.tx.dao.AccountDao;
import com.xkzhangsan.tx.service.AccountService; public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, int money) {
accountDao.out(outer, money);
int i=10/0;
accountDao.in(inner, money);
} }

3. 声明式事务基于AOP的注解 git对应版本: v0.0.3

(1)Spring的xml配置:spring\app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <description>Example configuration to get you started.</description> <context:component-scan base-package="com.xkzhangsan.tx" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/xkspringmvc"></property>
<property name="user" value="xktest"></property>
<property name="password" value="123456"></property>
</bean> <bean id="accountDao" class="com.xkzhangsan.tx.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="accountService" class="com.xkzhangsan.tx.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean> <!-- 1 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2 将管理器交予spring
* transaction-manager 配置事务管理器
* proxy-target-class
true : 底层强制使用cglib 代理
-->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/> </beans>

(2)AccountServiceImpl

package com.xkzhangsan.tx.service.impl;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import com.xkzhangsan.tx.dao.AccountDao;
import com.xkzhangsan.tx.service.AccountService; public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
@Transactional(propagation=Propagation.REQUIRED , isolation = Isolation.DEFAULT)
public void transfer(String outer, String inner, int money) {
accountDao.out(outer, money);
int i=10/0;
accountDao.in(inner, money);
} }

参考:https://www.cnblogs.com/ysocean/p/7617620.html

最新文章

  1. 在web浏览器上显示室内温度(nodeJs+arduino+socket.io)
  2. Mono-D在MacOS上的设置
  3. REORG TABLE命令优化数据库性能
  4. mysql 查看用户的权限
  5. backbonejs mvc框架的增删查改实例
  6. windowns--HANDLE,
  7. maven依赖的全局排除
  8. 用java发送email邮件例子
  9. ubuntu安装oracle java
  10. HDFS之SequenceFile和MapFile
  11. js原生写的微博留言板有angularjs效果
  12. mysql 关于表与字段的增删改查操作
  13. day08(字符编码,字符与字节,文件操作)
  14. 4.产生10个1-100的随机数,并放到一个数组中 (1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。 (2)把数组中的数字放到当前文件夹的numArr.txt文件中
  15. leetcode python 010
  16. elastalert邮件报警
  17. 2款适合HTML做音频和视频的插件
  18. 来谈谈你对CSS盒模型的认识?
  19. July 17th 2017 Week 29th Monday
  20. 使用腾讯云 GPU 学习深度学习系列之二:Tensorflow 简明原理【转】

热门文章

  1. Vue--过滤器、指令、插件
  2. Dynamics CRM 2015/2016新特性之三十三:有了ExecuteTransactionRequest,再也不用担心部分成功部分失败了
  3. Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
  4. liteos分散加载(十四)
  5. 最新内核3.4)Linux 设备树加载I2C client adapter 的流程(内核3.4 高通)【转】
  6. 浅谈js的类数组对象arguments
  7. .gclient文件
  8. day45_9_4前端(2)css
  9. Mybatis动态SQL(五)
  10. Tensorflow加载预训练模型和保存模型(ckpt文件)以及迁移学习finetuning