切换主题
十、事务控制
一、编程式事务控制相关对象底层实现,可以不用看
1、PlatformTransactionManager
PlatformTransactionManager
接囗是spring 的事务管理器,它里面提供了我们常用的操作事务的方法
方法 | 说明 |
---|---|
Transactionstatus getTransaction(TransactionDefination defination) | 获取事务的状态信息 |
void commit(Transactionstatus status) | 提交事务 |
void rollback(Transactionstatus status) | 回滚事务 |
注意
PlatformTransactionManagcr
是接口类型,不同的 Dao
层技术则有不同的实现类
- Dao 层技术是
jdbc
或mybatis
时,实现类是org.springframework.jdbc.datasource.DataSourceTransactionManager
- Dao 层技术是
hibernate
时,实现类是org.springframework.orm.hihernate5.HibernateTransactionManager
2、TransactionDefinition
TransactionDefinition
是事务的定义信息对象
方法 | 说明 |
---|---|
int getIsolationLevel() | 获得事务的隔离级别 |
int getPropogationBehavior() | 获得事务的传播行为 |
int getTimeout() | 获得超时时间 |
boolean isReadonly() | 是否只读 |
(1)事务隔离级别
设置隔离级别,可以解决事务并发产生的问题,如脏读、不可重复读和虚读
ISOLATION_DEFAULT
(isolatioin_deafault)ISOLATION_READ_UNCOMMITTED
(isolation_read_uncommitted)ISOLATION_READ_COMMITTED
(isolation_read_committed)ISOLATION_REPEATABLE_READ
(isolation_repeatable_read)ISOLATION_SERIALIZABLE
(isolation_serializable)
(2)事务传播行为
信息
REQUIRED
:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)SUPPORTS
:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)MANDATORY
:使用当前的事务,如果当前没有事务,就抛出异常REQUERS NEW
:新建事务,如果当前在事务中,把当前事务挂起NOT SUPPORTED
:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起NEVER
:以非事务方式运行,如果当前存在事务,抛出异常NESTED
:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行REQUIRED
类似的操作- 超时时间:默认值是
-1
,没有超时限制。如果有,以秒为单位进行设置 - 是否只读:建议查询时设置为只读
3、TransactionStatus
TransactionStatus
接囗提供的是事务具体的运行状态,方法介绍如下
方法 | 说明 |
---|---|
boolean hasSavepoint() | 是否存储回滚点 |
boolean isCompleted() | 事务是否完成 |
boolzan isNewTransaction() | 是否是新事务 |
boolean isRollbackonly() | 事务是否回滚 |
二、基于XML的声明式事务控制
1、什么是声明式事务控制
Spring的声明式事务顾名思义就是采用声明的方式来处理事务。
这里所说的声明,就是指在配置文件中声明用在 Spring 配置文件中声明式的处理事务来代替代码式的处理事务。
2、声明式事务处理的作用
信息
- 事务管理不侵入开发的组件。具体来说,业务逻辑对象就不会意识到正在事务管理之中,事实上也应该如此,因为事务管理是属于系统层面的服务,而不是业务逻辑的一部分,如果想要改变事务管理策划的话,也只需要在定义文件中重新配置即可
- 在不需要事务管理的时候,只要在设定文件上修改一下,即可移去事务管理服务,无需改变代码重新编译这样维护起来极其方便
注意:Spring声明式事务控制底层就是AOP
3、测试环境的搭建
java
public class TestController {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
accountService.transformTo("tom", "Alice", 500);
}
}
java
public interface AccountService {
public void transformTo(String from_account, String to_account, Integer money);
}
java
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao = accountDao;
}
@Override
public void transformTo(String from_account, String to_account, Integer money) {
accountDao.out(from_account, money);
accountDao.in(to_account, money);
}
}
java
public interface AccountDao {
public void out(String account, Integer money);
public void in(String account, Integer money);
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountDao" class="org.example.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean class="org.example.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
</beans>
properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring-jdbc
jdbc.user=root
jdbc.password=123456
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-affair</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.19</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
</project>
4、进行事务控制
引入tx命名空间
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
配置applicationContext.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- ........ -->
<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="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的aop织入-->
<aop:config>
<!--<aop:pointcut id="pointcut" expression="execution(* org.example.service.impl.*.*(..))"/>-->
<!--<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>-->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* org.example.service.impl.*.*(..))"/>
</aop:config>
</beans>
5、切点方法的事务参数的配置
xml
<!-- 事务增强配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
其中,<tx:method>
代表切点方法的事务参数的配置
xml
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1" read-only="false" />
name
:切点方法名称isolation
:事务的隔离级别propogation
:事务的传播行为timeout
:超时时间read-only
:是否只读
三、基于注解的声明式事务控制
提示
环境搭建参考上面的xml声明式事务控制
步骤1:dao和service采用组件注解
java
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void out(String account, Integer money) {
jdbcTemplate.update("update account set money=money-? where name=?", money, account);
}
@Override
public void in(String account, Integer money) {
jdbcTemplate.update("update account set money=money+? where name=?", money, account);
}
}
java
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
@Transactional
public void transformTo(String from_account, String to_account, Integer money) {
accountDao.out(from_account, money);
int a=1/0;
accountDao.in(to_account, money);
}
}
步骤2:扫描组件、删除xml文件事务控制代码,加上事务注解驱动
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:property-placeholder location="jdbc.properties"/>
<context:component-scan base-package="org.example"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</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>
<tx:annotation-driven/>
</beans>
步骤3:业务层加上Transactional
注解
java
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
@Transactional
public void transformTo(String from_account, String to_account, Integer money) {
accountDao.out(from_account, money);
int a = 1 / 0;
accountDao.in(to_account, money);
}
}