Spring源码窥探之:声明式事务
1. 导入驱动,连接池,jdbc和AOP的依赖
<!-- c3p0数据库连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency> <!-- spring提供的jdbcTemplate模块 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- mysql链接驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
<scope>runtime</scope>
</dependency>
<!-- AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
2. 编写配置类,@EnableTransactionManagement这个注解一定要开启
/**
* description
*
* @author 70KG
* @date 2018/12/19
*/
@Configuration
@ComponentScan("com.nmys.story.springCore.springaop.tx_sample")
@EnableTransactionManagement // -- 开启基于注解的事务管理
public class TxConfig { // -- 配置数据源
@Bean
public DataSource dataSource() throws Exception {
ComboPooledDataSource pool = new ComboPooledDataSource();
pool.setUser("root");
pool.setPassword("root");
pool.setDriverClass("com.mysql.jdbc.Driver");
pool.setJdbcUrl("jdbc:mysql://47.104.129.162:3306/usthe?useSSL=false");
return pool;
} // -- 加入模板
@Bean
public JdbcTemplate jdbcTemplate() throws Exception {
JdbcTemplate template = new JdbcTemplate(dataSource());
return template;
} // -- 配置事务管理器,它才是用来提交回滚事务的主导者
@Bean
public DataSourceTransactionManager txManager() throws Exception {
DataSourceTransactionManager tx = new DataSourceTransactionManager(dataSource());
return tx;
} }
3. Service类和Dao类
/**
* description
*
* @author 70KG
* @date 2018/12/19
*/
@Service
public class TxService { @Autowired
private TxDao txDao; public void insertLog(){
txDao.insertSth();
} }
/**
* description
*
* @author 70KG
* @date 2018/12/19
*/
@Repository
public class TxDao {
@Autowired
private JdbcTemplate jdbcTemplate; // @Transactional仅表明它是一个事务方法,开启事务仅有注解是不够的,还需要配置事务管理器
@Transactional
public void insertSth() {
String sql = "INSERT into sys_log (username) VALUES(?);";
jdbcTemplate.update(sql, "lisi");
System.out.println("------>插入成功");
int i = 10/0;
}
}
4. 测试类
/**
* description
*
* @author 70KG
* @date 2018/12/19
*/
public class Test01 { public static void main(String[] args) {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(TxConfig.class);
TxService bean = app.getBean(TxService.class);
bean.insertLog();
} }
5 结果
出异常就回滚,否则入库。
Spring源码窥探之:声明式事务的更多相关文章
- 使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务。
使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务.
- Spring注解驱动开发之声明式事务
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
- Spring:(三) --常见数据源及声明式事务配置
Spring自带了一组数据访问框架,集成了多种数据访问技术.无论我们是直接通过 JDBC 还是像Hibernate或Mybatis那样的框架实现数据持久化,Spring都可以为我们消除持久化代码中那些 ...
- spring 事物(三)—— 声明式事务管理详解
spring的事务处理分为两种: 1.编程式事务:在程序中控制事务开始,执行和提交:详情请点此跳转: 2.声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码:(建议使用) 我对&q ...
- spring给予XML配置的声明式事务
步骤: 1.添加aop.tx命名空间声明: 2.配置事务管理器: 3.配置增强: 4.配置aop 具体xml设置如下: <?xml version="1.0" encodin ...
- Spring学习之Spring中AOP方式切入声明式事务
mybatis-spring官方文档说明 一个使用 MyBatis-Spring 的其中一个主要原因是它允许 MyBatis 参与到 Spring 的事务管理中.而不是给 MyBatis 创建一个新的 ...
- Spring AOP应用之一:声明式事务
所有数据访问技术都提供事务处理机制,这些技术提供了API用来开启事务.提交事务完成数据操作,或者在发生错误的时候回滚数据.Spring本身并不支持事务实现,同时只是负责提供标准接口来处理不同数据访问技 ...
- 阶段3 2.Spring_10.Spring中事务控制_8 spring基于纯注解的声明式事务控制
新建项目 把之前项目src下的内容全部复制过来 pom.xml内复制过来 开始配置 新建一个config的包,然后再新建配置文件类SpringConfiguration @Configuration这 ...
- spring下春注解的声明式事务控制
package com.hope.test;import com.hope.domain.Account;import com.hope.service.IAccountService;import ...
随机推荐
- 将笔记本无线网卡链接wifi通过有线网卡共享给路由器
1.背景 背景这个就说来长了,在公司宿舍住着,只给了一个账号,每次登录网页都特别麻烦(需要账号认证那种).然后每个账号只支持一个设备在线,这就很尴尬了,那我笔记本.手机.Ipad怎么办? 当然,这时候 ...
- ElasticSearch中"distinct","count"和"group by"的实现
最近在业务中需要使用ES来进行数据查询,在某些场景下需要对数据进行去重,以及去重后的统计.为了方便大家理解,特意从SQL角度,方便大家能够理解ES查询语句. 1 - distinct ; { &quo ...
- LocalStack和Local对象实现栈的管理
flask里面有两个重要的类Local和LocalStack 输入from flask import globals 左键+ctrl点globals进入源码,进去后找57行 flask只会实例化出这两 ...
- Linux基础(03)gdb调试
1. 安装GDB增强工具 (gef) * GDB的版本大于7.7 * wget -q -O- https://github.com/hugsy/gef/raw/master/scripts/gef.s ...
- hadoop2.x大数据视频教程(十二天学会)
- 分布式缓存重建并发冲突和zookeeper分布式锁解决方案
如果缓存服务在本地的ehcache中都读取不到数据. 这个时候就意味着,需要重新到源头的服务中去拉去数据,拉取到数据之后,赶紧先给nginx的请求返回,同时将数据写入ehcache和redis中 分布 ...
- redis监控工具汇总
redis-stat redis-stat是一个比较有名的redis指标可视化的监控工具,采用ruby开发,基于redis的info命令来统计,不影响redis性能. docker运行 docker ...
- EgretWing链接微信开发工具调试问题
EgretWing链接微信开发工具调试问题 EgretWing 编译器支持持三种调试模式,Node.js .Chrome .EgretWing 扩展开发. 开发过程中会遇到工具配置错误. 这就需要在E ...
- C# 获取系统字体方法
//需要引用命名空间 using System.Drawing; using System.Drawing.Text; //获取系统字体方法 public dynamic GetFontNames() ...
- 简单计算器设计(WPF)
要求: 文本框居中,用户不能修改运算结果 当用户选择不同的运算类型时 下方GroupBox的标题与所选运算类型相对应 且文本框数字立即清空 单击[计算]按钮时 如果文本框输入的内容非法 结果文本框显示 ...