spring 配置事务管理器
在Spring中数据库事务是通过PlatformTransactionManager进行管理的,jdbcTemplate是不能支持事务的,而能够支持事务的是org.springframework.transaction.support.TransactionTemplate模板,它是Spring所提供的事务管理器的模板
•事务的创建、提交和回滚是通过PlatformTransactionManager接口来完成的。
•当事务产生异常时会回滚事务,在默认的实现中所有的异常都会回滚。我们可以通过配置去修改在某些异常发生时回滚或者不回滚事务。
•当无异常时,会提交事务。
支持JTA事务,常用的是DataSourceTransactionManager,它继承抽象事务管理器AbstractPlatformTransactionManager,而AbstractPlatformTransactionManager又实现了PlatformTransactionManager。这样Spring就可以如同源码中看到的那样使用PlatformTransactionManager接口的方法,创建、提交或者回滚事务了。
配置事务管理器
MyBatis框架用得最多的事务管理器是DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager),因此下面将以此例进行讲解。如果使用的持久框架是Hibernate,那么你就要用到spring-orm包org.springframework.orm.hibernate4.HibernateTransactionManager了。它们大同小异,一般而言我们在使用时,还会加入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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <!-- 数据库连接池 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/springmvc?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- <property name="maxActive" value="255"/>
- <property name="maxIdle" value="5"/>
- <property name="maxWait" value="10000"/>
- </bean>
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!-- 配置数据源事务管理器 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- </beans>
这里先引入了XML的命名空间,然后定义了数据库连接池,于是使用了DataSourceTransactionManager去定义数据库事务管理器,并且注入了数据库连接池。这样Spring就知道你已经将数据库事务委托给事务管理器transactionManager管理了。在jdbcTemplate源码分析时,笔者就已经指出,数据库资源的产生和释放如果没有委托给数据库管理器,那么就由jdbcTemplate管理,但是此时已经委托给了事务管理器,所以jdbcTemplate的数据库资源和事务已经由事务管理器处理了。
在Spring中可以使用声明式事务或者编程式事务,如今编程式事务几乎不用了,因为它会产生冗余,代码可读性较差。声明式事务又可以分为XML配置和注解事务,但XML方式也已经不常用了,目前主流方法是注解@Transactional。
用Java配置方式实现Spring数据库事务
用Java配置的方式来实现Spring数据库事务,需要在配置类中实现接口TransactionManagementConfigurer的annota-tionDrivenTransactionManager方法。Spring会把annotationDrivenTransactionManager方法返回的事务管理器作为程序中的事务管理器
代码清单:使用Java配置方式实现Spring数据库事物
- package com.ssm.chapter13.config;
- import org.apache.commons.dbcp.BasicDataSourceFactory;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import org.springframework.transaction.annotation.TransactionManagementConfigurer;
- import javax.sql.DataSource;
- import java.util.Properties;
- @Configuration
- @ComponentScan("com.ssm.chapter13.*")
- //使用事务驱动管理器
- @EnableTransactionManagement
- public class JavaConfig implements TransactionManagementConfigurer {
- //数据源
- private DataSource dataSource = null;
- /**
- * 配置数据源. * @return 数据源.
- */
- @Bean(name = "dataSource")
- public DataSource initDataSource() {
- if (dataSource != null) {
- return dataSource;
- }
- Properties props = new Properties();
- props.setProperty("driverClassName", "com.mysql.cj.jdbc.Driver");
- props.setProperty("url", "jdbc:mysql://localhost:3306/springmvc?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true");
- props.setProperty("username", "root");
- props.setProperty("password", "123456");
- props.setProperty("maxActive", "200");
- props.setProperty("maxIdle", "20");
- props.setProperty("maxWait", "30000");
- try {
- dataSource = BasicDataSourceFactory.createDataSource(props);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return dataSource;
- }
- /**
- * 配置jdbcTemplate * @return jdbcTemplate
- */
- @Bean(name = "jdbcTemplate")
- public JdbcTemplate initjdbcTemplate() {
- JdbcTemplate jdbcTemplate = new JdbcTemplate();
- jdbcTemplate.setDataSource(initDataSource());
- return jdbcTemplate;
- }
- /**
- * 实现接口方法,使得返回数据库事务管理器
- */
- @Override
- @Bean(name = "transactionManager")
- public PlatformTransactionManager annotationDrivenTransactionManager() {
- DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
- //设置事务管理器管理的数据源
- transactionManager.setDataSource(initDataSource());
- return transactionManager;
- }
- }
实现了TransactionManagementConfigurer接口所定义的方法annotation DrivenTransactionManager,并且我们使用DataSourceTransactionManager去定义数据库事务管理器的实例,然后把数据源设置给它。注意,使用注解@EnableTransactionManagement后,在Spring上下文中使用事务注解@Transactional,Spring就会知道使用这个数据库事务管理器管理事务了。
编程式事务
编程式事务以代码的方式管理事务,换句话说,事务将由开发者通过自己的代码来实现,这里需要使用一个事务定义类接口——TransactionDefinition,暂时不进行深入的介绍,我们只要使用默认的实现类——DefaultTransactionDefinition就可以了。
代码清单:编程式事务
- package com.ssm.chapter13.main;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.TransactionDefinition;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.DefaultTransactionDefinition;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("ssm/chapter13/spring-cfg.xml");//ctx为Spring IoC容器
- JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
- //事务定义类
- TransactionDefinition def = new DefaultTransactionDefinition();
- PlatformTransactionManager transactionManager = ctx.getBean(PlatformTransactionManager.class);
- TransactionStatus status = transactionManager.getTransaction(def);
- try {
- //执行SQL语句
- jdbcTemplate.update("insert into t_role(role_name, note) " + "values('role_name_transactionManager', 'note_transactionManager')");
- //提交事务
- transactionManager.commit(status);
- } catch (Exception ex) {
- //回滚事务
- transactionManager.rollback(status);
- }
- }
- }
从代码中可以看到所有的事务都是由开发者自己进行控制的,由于事务已交由事务管理器管理,所以jdbcTemplate本身的数据库资源已经由事务管理器管理,因此当它执行完insert语句时不会自动提交事务,这个时候需要使用事务管理器的commit方法,回滚事务需要使用rollback方法。
当然这是最简单的使用方式,因为这个方式已经不是主流方式,甚至几乎是不被推荐使用的方式,之所以介绍是因为它的代码流程更为清晰,有助于未来对编程式事务的理解。
编程式事务是一种约定型的事务,在大部分情况下,当使用数据库事务时,大部分的场景是在代码中发生了异常时,需要回滚事务,而不发生异常时则是提交事务,从而保证数据库数据的一致性。
spring 配置事务管理器的更多相关文章
- spring的annotation-driven配置事务管理器详解
http://blog.sina.com.cn/s/blog_8f61307b0100ynfb.html ——————————————————————————————————————————————— ...
- spring简单事务管理器
事务管理器 <!-- Transaction manager for a single JDBC DataSource --> <bean id="transaction ...
- ssh框架配置事务管理器
http://blog.163.com/zsq303288862@126/blog/static/9374596120111182446727/
- 解决在Spring整合Hibernate配置tx事务管理器出现错误的问题
问题描述: Error occured processing XML 'org/aopalliance/intercept/MethodInterceptor'. See Error Log for ...
- 跟我学Spring3(9.2):Spring的事务之事务管理器
原文出处: 张开涛9.2.1 概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口PlatformTransactionManage ...
- spring事务管理器的源码和理解
原文出处: xieyu_zy 以前说了大多的原理,今天来说下spring的事务管理器的实现过程,顺带源码干货带上. 其实这个文章唯一的就是带着看看代码,但是前提你要懂得动态代理以及字节码增强方面的知识 ...
- spring的事务管理配置
spring有两种事务配置器,可以使用spring的jdbc事务管理器,也可以使用对hibernate的事务管理器 第一种 使用Spring JDBC或IBatis进行事务配置(配置文件方式): &l ...
- Spring jdbctemplate和事务管理器
内部bean 对象结构: @Autowiredprivate IAccountService accountService; @Service("accountService")@ ...
- spring事务管理器设计思想(二)
上文见<spring事务管理器设计思想(一)> 对于第二个问题,涉及到事务的传播级别,定义如下: PROPAGATION_REQUIRED-- 如果当前没有事务,就新建一个事务.这是最常见 ...
随机推荐
- 关于 or 判断都是Ture的问题
问题: 1. ret1 = [1, 2, 3, 4] if 11 or 33 in ret1: print("ok") else: print("no") 2. ...
- 洛谷 P1279 字串距离 题解
每日一题 day24 打卡 Analysis 字符串+dp 仔细观察发现,对于f[i][j],它的值为以下三个值中的最小者: f[i-1][j]+k //a[i]对应空格 f[i][j-1]+k // ...
- Oracle substr() 字符截取函数
1.substr函数格式 (俗称:字符截取函数) 格式1: substr(string string, int a, int b); 格式2:substr(string string, int a ...
- Ubuntu下彻底卸载默认安装的mysql,自己手动下载安装MYSQL
彻底卸载: sudo apt-get autoremove --purge mysql-server-5.7 sudo apt-get remove mysql-common sudo rm -rf ...
- 【概率论】5-7:Gama分布(The Gamma Distributions Part II)
title: [概率论]5-7:Gama分布(The Gamma Distributions Part II) categories: - Mathematic - Probability keywo ...
- 原创:从海量数据中查找出前k个最小或最大值的算法(java)
现在有这么一道题目:要求从多个的数据中查找出前K个最小或最大值 分析:有多种方案可以实现.一.最容易想到的是先对数据快速排序,然后输出前k个数字. 二.先定义容量为k的数组,从源数据中取出前k个填 ...
- note_4.10
单位根反演 \[ \frac{1}{k}\sum_{i=0}^{k-1}\omega_k^{in}=[k|n] \] 所以 \[ \begin{equation} \begin{split} \sum ...
- SpringBoot整合ElasticSearch:基于SpringDataElasticSearch
0.注意事项 SpringDataElasticSearch可能和远程的ElasticSearch版本不匹配,会宝座 版本适配说明:https://github.com/spring-projects ...
- 美团Android自动化之旅—适配渠道包
http://tech.meituan.com/mt-apk-adaptation.html 概述 前一篇文章(美团Android自动化之旅-生成渠道包)介绍了Android中几种生成渠道包的方式,基 ...
- vue+vue-resource设置请求头(带上token)
前言 有这样的一个需求,后台服务器要求把token放在请求头里面 嗯一般是通过data里面通过参数带过去的 第一种方法 全局改变: Vue.http.headers.common['token'] = ...