spring事务的三种配置应用实例
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
spring事务的三种配置应用实例的更多相关文章
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- Spring 实现事务的三种方式
事务:保证数据的运行不会说A给B钱,A钱给了B却没收到. 实现事务的三种方式(重要代码): 1.aspectJ AOP实现事务: <bean id="dataSourceTransac ...
- 简述Spring事务有几种管理方法,写出一种配置方式
Spring事务有两种方式: 1.编程式事务:(代码中嵌入) 2.声明式事务:(注解,XML) 注解方式配置事务的方式如下: 首先,需要在applicationContext.xml中添加启动配置,代 ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- 菜鸟学习Spring——SpringIoC容器基于三种配置的对比
一.概述 对于实现Bean信息定义的目标,它提供了基于XML.基于注解及基于java类这三种选项.下面总结一下三种配置方式的差异. 二.Bean不同配置方式比较. 三.Bean不同配置方式的适用场合. ...
- 【jdbc】【c3p0】c3p0三种配置方式【整理】
c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...
- 深入浅出spring IOC中三种依赖注入方式
深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...
- spring与mybatis三种整合方法
spring与mybatis三种整合方法 本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接 http://code.googl ...
- Linux操作系统下三种配置环境变量的方法
现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以配置java环境变量为例介绍三种配置环境变量的方法. 1.修改/etc/profile文件 如果你的计算机仅仅作 ...
随机推荐
- JS的with关键字到底是什么?
with关键字 with在JS中通常被当做重复引用同一个对象多个属性的快捷方式. var obj = { a: 1, b: 2, c: 3 }; // 重复引用obj进行属性赋值 obj.a = 3; ...
- hadoop mapreduce求解有序TopN
利用hadoop的map和reduce排序特性实现对数据排序取TopN条数据. 代码参考:https://github.com/asker124143222/wordcount 1.样本数据,假设是订 ...
- 【LeetCode】1056-易混淆数
易混淆数 给定一个数字 N,当它满足以下条件的时候返回 true:把原数字旋转180°以后得到新的数字.如 0, 1, 6, 8, 9 旋转 180° 以后,得到了新的数字 0, 1, 9, 8, 6 ...
- [转]Eclipse插件开发之基础篇(6) SWT简介
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/09/01/eclipse_plugin_1_3_1.html SWT(Standard Widget ...
- [Go] golang实时监控日志文件的包tail
在linux中有一个tail命令,tail -f可以实时的监控文件新增加的内容,如果用代码实现这个逻辑,可以下载使用这个包go get github.com/hpcloud/tail/... 测试代码 ...
- [Linux] deepin系统添加PHP仓库源出错Error: could not find a distribution template for Deepin/stable
aptsources.distro.NoDistroTemplateException: Error: could not find a distribution template for Deepi ...
- 『009』Python
『004』索引-Language Python 准备更新中
- Linux文件基本命令
Linux文件基本命令学习 操作技巧: 输入文件/目录/命令的前几个字母之后,按下tab键,则自动补全 按上/下,回滚曾经用过的命令 不想执行命令使用:crtl + c 基础命令: ls(查看) 基础 ...
- 组装数据--相同的clusterID合并在一起 左边是a接口 右边是B接口如 [{a接口},{b接口}]
组装成这种 var BJData = [ [{"city": "无锡市","clusterID": 1, "y": 3 ...
- 3、zabbix组件之间的关系
我们在安装zabbix的时候安装了四个软件:zabbix-server.zabbix-server-mysql.zabbix-web-mysql.zabbix-agent,那么这个四个软件之间有什么关 ...