1.spring中的TransactionManager接口

DateSourceTransactionManager 用于JDBC的事务管理

HibernateTransactionManager用于Hibernate的事务管理

JpaTransactionManager 用于Jpa的事物管理

2.spring中TransactionManager接口的定义(源码)

事务的属性介绍:这里定义了传播行为、隔离级别、超时时间、是否只读

3.转账案例

(1)项目结构

(2)导入jar

(3)建立数据库测试数据

CREATE DATABASE springjdbc
USE springjdbc
CREATE TABLE `ar_account` (`id` int(11) NOT NULL AUTO_INCREMENT,`username` varchar(255) DEFAULT NULL,`money` double(255,0) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of ar_account
-- ----------------------------
INSERT INTO `ar_account` VALUES ('1', '张三', '60');
INSERT INTO `ar_account` VALUES ('2', '李四', '40');

(4)建立日志配置(log4j.properties)

# Global logging configuration
log4j.rootLogger=info, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(5)建立数据库配置(db.properties)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springjdbc
jdbc.username=root
jdbc.password=root

(6)建立接口 AccountDao类与实现AccountDaoImpl类

package com.linxin.spring.dao;public interface AccountDao {//加钱void addMoney(Integer id,Double money);//减钱void subMoney(Integer id,Double money);}
package com.linxin.spring.dao;import javax.annotation.Resource;import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {@Resource(name="jdbctemplate")private JdbcTemplate jt;@Overridepublic void addMoney(Integer id, Double money) {String sql= "update ar_account set money = money + ? where id= ?";jt.update(sql,money,id);}@Overridepublic void subMoney(Integer id, Double money) {String sql= "update ar_account set money = money - ? where id= ?";jt.update(sql,money,id);}}

(7)创建AccountService接口类与实现类AccountServiceImpl

package com.linxin.spring.service;public interface AccountService {void transfer(Integer from, Integer to,Double money);
}
package com.linxin.spring.service;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.linxin.spring.dao.AccountDao;@Service("accountService")
public class AccountServiceImpl implements AccountService {@Resource(name="accountDao")private AccountDao accountDao;@Overridepublic void transfer(Integer from, Integer to, Double money) {accountDao.addMoney(to, money);int a=1/0;accountDao.subMoney(from, money);}}

(8)创建spring配置文件application.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:centext="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd "><!-- 加入扫描 --><centext:component-scan base-package="com.linxin.spring.dao"></centext:component-scan><centext:component-scan base-package="com.linxin.spring.service"></centext:component-scan><!-- 加载文件 --><centext:property-placeholder location="classpath:db.properties"/><!-- spring管理c3p0数据源 --><bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- Jdbctemplate --><bean name="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!-- 事务管理 --><bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 数据源 --><property name="dataSource" ref="dataSource"></property></bean><!-- 通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 传播行为 --><!-- 支持当前事务,如果不存在,就新建一个 --><tx:method name="transfer" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 切面(将通知织入切入点) --><aop:config><!-- 切入点 --><aop:pointcut expression="execution(* com.linxin.spring.service..*.*(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>
</beans>

(9)测试类

package com.linxin.spring.Testtx;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.linxin.spring.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestTx {@Resource(name="accountService")private AccountService accountService;@Testpublic void testTransfer() {accountService.transfer(2, 1, 30.0);}}

spring_事务管理 TransactionManager相关推荐

  1. Spring事务管理TransactionManager

    事务就是对一系列的数据库操作进行统一的提交或回滚操作,比如说做一个转账功能,要更改帐户两边的数据,这时候就必须要用事务才能算是严谨的做法.要么成功,要么失败,保持数据一致性.如果中间有一个操作出现异常 ...

  2. spring(三)之事务、事务管理器(TransactionManager)简介及实现事务的4种方式

    1.事务 一组业务ACID操作,要么全部成功,要么全部不成功. 事务特性:①原子性,针对整体而言(一个事务不可以被拆分):②一致性,针对数据而言(一个事务执行之前和执行之后必须处于一致性状态,一个事务 ...

  3. 自定义事务管理器TransactionManager对象

    自定义事务管理器TransactionManager对象 以aop思想,实现事务管理切面 1. DataSource注册容器 <?xml version="1.0" enco ...

  4. Spring AOP事务实现原理之事务管理器TransactionManager

    流程图 该图介绍了事务的传播行为 该流通图展示的是TransactionManager具体如何结合事务的传播行为进行事务获取 该流通图展示的是TransactionManager具体如何结合事务的传播 ...

  5. Spring之事务管理配置

    1. 基于注解的事务配置 1. 在需要添加事务的方法上加上@Transactional注解 2. Spring的配置文件中配置事务管理器 1 <!-- 添加事务管理器组件DataSourceTr ...

  6. mybatis源码分析之事务管理器

    2019独角兽企业重金招聘Python工程师标准>>> 上一篇:mybatis源码分析之Configuration 主要分析了构建SqlSessionFactory的过程中配置文件的 ...

  7. Spring事务管理 与 SpringAOP

    1,Spring事务的核心接口 Spring事务管理的实现有许多细节,如果对整个接口框架有个大体了解会非常有利于我们理解事务,下面通过讲解Spring的事务接口来了解Spring实现事务的具体策略.  ...

  8. Spring事务管理3----声明式事务管理(1)

     声明式事务管理(1)基于    基于 tx/aop  这种事务管理相比编程式事务管理来说对业务层基本没有改动,通过  TransactionProxyFactoryBean 创建业务层的代理,通过A ...

  9. SpringMVC+MyBatis 事务管理一

    前言 spring事务管理包含两种情况,编程式事务.声明式事务.而声明式事务又包括基于注解@Transactional和tx+aop的方式.那么本文先分析编程式注解事务和基于注解的声明式事务. 编程式 ...

最新文章

  1. 两者相差百分比怎么算_不知道烘焙百分比的全拖出来打屁股!
  2. java绘图机器猫_用绘图语句画机器猫(初学 C 语言的同学必看)
  3. Windows 10企业批量部署实战之WDS安装
  4. Rsync:一个很实用的文件同步命令
  5. 1.x到2.x的迁移:可观察与可观察:RxJava FAQ
  6. 设置SVN忽略文件和文件夹(文件夹)
  7. 判断display为隐藏还是显示及获取css
  8. AMD的Naples改名为EYPC
  9. 比赛 | 第一届古汉语分词与词性标注国际评测来啦
  10. c++文件操作之读取全部文本文件【zz】
  11. 开机一直转圈_天气转凉,电脑早上开机也需要预热了吗?
  12. js获取IP地址方法总结
  13. java bi报表工具_7款顶级开源BI(商务智能)软件和报表工具
  14. 如何用优盘安装服务器操作系统,使用优盘安装服务器
  15. 苹果个人开发者账号升级为公司开发者教程
  16. Tapestry经典入门教程
  17. 微软亚洲研究院 可视化_如何为亚洲市场本地化手机游戏
  18. 车联网是什么_车联网有什么用_车联网功能介绍
  19. MATLAB 詹姆斯韦伯天文望远镜轨迹 粗略效果仿真 (二)
  20. php内容管理系统 admini,BageCMS 开源网站内容管理系统 php版下载_BageCMS 开源网站内容管理系统 php版官方下载-太平洋下载中心...

热门文章

  1. mysql——cmd进入mysql及常用的mysql操作
  2. 4. hda设备中的pcm文件(第四部分)
  3. Android12 Native C++ 层AudioRecord录音AudioTrack播放
  4. tensorflow模型固化
  5. 论文总结——研究基于脑电图的深度神经网络情感识别的关键频带和通道
  6. exit(0)和exit(1)的区别
  7. 串口协议关于异或校验(自留)
  8. NanShan 即时通讯 云计算,云平台,云+端,云服务
  9. 性能优化之 线程优化
  10. Hyper-V设置桥接网络