目录
PlatformTransactionManager平台事务管理
编程式:即使用java的api书写代码
声明式:使用配置去配置
PlatformTransactionManager接口时spring的事务管理器,它里面提供来我们常用的操作事务的方法

PlatformTransactionManager是接口类型,不同的Dao层技术则有不同的实现类,例如:Dao层技术是jdbc或mybatis时:orqspringframeworkidbcdatasourceDataSourceTransactionManager
Dao层技术是hibernate时:orq.springframework.orm.hibernate5.HibernateTransactionManager
TransactionDefinition是事务的定义信息对象,里面有如下方法:

设置隔离级别,可以解决事务并发产生的问题,如
- ISOLATION_DEFAULT//默认的
- ISOLATION_READ_UNCOMMITTED//读未提交,哪种都不能解决
- ISOLATION_READ_COMMITTED//读已提交,解决脏读
- ISOLATION_REPEATABLE READ//可重复读,解不可重复读
- ISOLATION_SERIALIZABLE//串行化,解决所有,性能最低
- REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)
-
- SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
-
- MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常
-
- RFOUFRS NEW:新增事务,如果当前在事务中,把当前事务挂起
-
- NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
-
- NEVER:以非事务方式运行,如果当前存在事务,抛出异常
-
- NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行REOUIRED类似的操作
-
- 超时时间:默认值是-1.没有超时限制,如果有,以秒为单位进行设置
-
- 是否只读:建议查询时设置为只读,
TransactionStatus接口时提供事务具体运行状态(是被动产生的,不需要自己去设置),方法介绍如下

spring的声明式事务就是指在配置文件中声明,用在spring配置文件中的声明式的处理事务来代替diam式的处理事务
转账业务演示事务

controller包下AccountController类
- package com.controller;
-
- import com.service.AccountService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class AccountController {
-
- public static void main(String[] args) {
- ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext1.xml");
- AccountService accountService = app.getBean(AccountService.class);
- accountService.transfer("KC","ZH",500);
- }
-
- }
service包下AccountService接口
- package com.service;
-
- public interface AccountService {
-
- public void transfer(String outMan, String inMan, double money);
-
- }
接口实现类
- package com.service.impl;
-
- import com.dao.AccountDao;
- import com.service.AccountService;
-
- public class AccountServiceImpl implements AccountService {
-
- private AccountDao accountDao;
- public void setAccountDao(AccountDao accountDao) {
- this.accountDao = accountDao;
- }
-
- public void transfer(String outMan, String inMan, double money) {
- accountDao.out(outMan,money);
-
- accountDao.in(inMan,money);
- }
- }
pojo包下Account类
- package com.pojo;
-
- public class Account {
-
- private String name;
- private double money;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public double getMoney() {
- return money;
- }
-
- public void setMoney(double money) {
- this.money = money;
- }
- }
dao包下AccountDao
- package com.dao;
-
- public interface AccountDao {
-
- public void out(String outMan, double money);
- public void in(String inMan, double money);
-
- }
实现类下
- package com.dao.impl;
-
- import com.dao.AccountDao;
- import org.springframework.jdbc.core.JdbcTemplate;
-
- public class AccountDaoImpl implements AccountDao {
-
- private JdbcTemplate jdbcTemplate;
- public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
- this.jdbcTemplate = jdbcTemplate;
- }
-
- public void out(String outMan, double money) {
- jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
- }
-
- public void in(String inMan, double money) {
- jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
- }
- }
配置文件applicationCntext1.xml下
- <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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
- ">
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
- <property name="user" value="root"/>
- <property name="password" value="123456"/>
- bean>
-
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"/>
- bean>
-
- <bean id="accountDao" class="com.dao.impl.AccountDaoImpl">
- <property name="jdbcTemplate" ref="jdbcTemplate"/>
- bean>
-
-
- <bean id="accountService" class="com.service.impl.AccountServiceImpl">
- <property name="accountDao" ref="accountDao"/>
- bean>
-
-
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- bean>
-
-
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
-
- <tx:attributes>
- <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
- <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
- <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
- <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
- <tx:method name="*"/>
- tx:attributes>
- tx:advice>
-
-
- <aop:config>
- <aop:pointcut id="txPointcut" expression="execution(* com.service.impl.*.*(..))"/>
- <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
- aop:config>
-
- beans>
pom.xml下
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
-
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.aspectjgroupId>
- <artifactId>aspectjweaverartifactId>
- <version>1.8.4version>
- dependency>
-
- <dependency>
-
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.0.5.RELEASEversion>
- <scope>compilescope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-txartifactId>
- <version>5.0.5.RELEASEversion>
- dependency>
- <dependency>
- <groupId>c3p0groupId>
- <artifactId>c3p0artifactId>
- <version>0.9.1.1version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.32version>
- dependency>
- dependencies>
数据库中

运行结果

数据库中

当发生错误时,数据库中的值都不变这就控制住了事务

- <tx:advice id="txAdvice" transaction-manager="transactionManager">
-
- <tx:attributes>
- <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED">
-
- tx:attributes>
- tx:advice>
其中
声明式事务控制的配置要点