Spring 声明式事务控制。
文章目录
Spring 事务。
- JavaEE 体系进行分层开发,事务处理位于业务层,Spring 提供了分层设计业务层的事务处理解决方案。
- Spring 框架提供了一组事务控制的接口。这组接口在 spring-tx-5.0.2.RELEASE.jar 中。
- Spring 的事务控制都是基于 AOP 的,ta 既可以使用编程的方式实现,也可以使用配置的方式实现。
API。
PlatformTransactionManager。
接口。声明两个方法。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.transaction;
import org.springframework.lang.Nullable;
public interface PlatformTransactionManager {
TransactionStatus getTransaction(@Nullable TransactionDefinition var1) throws TransactionException;
void commit(TransactionStatus var1) throws TransactionException;
void rollback(TransactionStatus var1) throws TransactionException;
}
- org.springframework.jdbc.datasource.DataSourceTransactionManager
使用 Spring JDBC 或 iBatis 进行持久化数据。- org.springframework.orm.hibernate5.HibernateTransactionManager
使用 Hibernate 版本进行持久化数据。
TransactionDefinition。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.transaction;
import org.springframework.lang.Nullable;
public interface TransactionDefinition {
int PROPAGATION_REQUIRED = 0;
int PROPAGATION_SUPPORTS = 1;
int PROPAGATION_MANDATORY = 2;
int PROPAGATION_REQUIRES_NEW = 3;
int PROPAGATION_NOT_SUPPORTED = 4;
int PROPAGATION_NEVER = 5;
int PROPAGATION_NESTED = 6;
int ISOLATION_DEFAULT = -1;
int ISOLATION_READ_UNCOMMITTED = 1;
int ISOLATION_READ_COMMITTED = 2;
int ISOLATION_REPEATABLE_READ = 4;
int ISOLATION_SERIALIZABLE = 8;
int TIMEOUT_DEFAULT = -1;
int getPropagationBehavior();
// 事务传播行为。
int getIsolationLevel();
// 事务隔离级别。
int getTimeout();
// 事务超时时间。
boolean isReadOnly();
// 事务是否只读。
@Nullable
String getName();
// 事务对象名称。
}
事务隔离级别。
int ISOLATION_DEFAULT = -1;
// 默认级别。不同数据库决定。
int ISOLATION_READ_UNCOMMITTED = 1;
// 读未提交。
int ISOLATION_READ_COMMITTED = 2;
// 读已提交。Oracle 默认。解决脏读。Oracle 默认。
int ISOLATION_REPEATABLE_READ = 4;
// 读取其他事务提交修改后的数据。解决不可重复读问题。MySQL 默认。
int ISOLATION_SERIALIZABLE = 8;
// 读取其他事务提交添加后的数据。解决幻读。
事务传播行为。
int PROPAGATION_REQUIRED = 0;
// 默认。如果当前没有事务,就新建一个事务。如果存在事务,就加入到这个事务中。
int PROPAGATION_SUPPORTS = 1;
// 支持当前事务。如果当前没有事务,就以非事务方式执行。
int PROPAGATION_MANDATORY = 2;
// 使用当前事务。如果当前没有事务,就抛出异常。
int PROPAGATION_REQUIRES_NEW = 3;
// 新建事务。如果当前在事务中,把当前事务挂起。
int PROPAGATION_NOT_SUPPORTED = 4;
// 以非事务方式执行。如果当前在事务中,把当前事务挂起。
int PROPAGATION_NEVER = 5;
// 以非事务方式执行。如果当前存在事务,抛出异常。
int PROPAGATION_NESTED = 6;
// 如果当前存在事务,则嵌套在事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作。
int ISOLATION_DEFAULT = -1;
超时时间。
int TIMEOUT_DEFAULT = -1;
// 默认值是 -1。没有超时时间。如果有,以秒为单位进行设置。
只读事务。
建议查询时设置为只读。
boolean isReadOnly();
TransactionStatus。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.transaction;
import java.io.Flushable;
public interface TransactionStatus extends SavepointManager, Flushable {
boolean isNewTransaction();
// 获取事务是否为新事务。
boolean hasSavepoint();
// 获取事务是否存在存储点。
void setRollbackOnly();
// 设置事务回滚。
boolean isRollbackOnly();
// 获取事务是否回滚。
void flush();
// 刷新事务。
boolean isCompleted();
// 获取事务是否完成。
}
基于 xml 的声明式事务控制。
- pom.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>com.geekl</groupId>
<artifactId>spring_tx</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
</project>
- bean.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:context="http://www.springframework.org/schema/context"
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">
<!-- 配置业务层。-->
<bean id="accountService" class="com.geek.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置账户持久层。-->
<bean id="accountDao" class="com.geek.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置数据源。-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.142.135:3307/geek_spring_dbutils"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
- 账户实体类。
package com.geek.domain;
import lombok.Data;
/**
* 账户实体类。
*/
@Data
public class Account {
private Integer id;
private String name;
private Float money;
}
- IAccountDao。
package com.geek.dao;
import com.geek.domain.Account;
public interface IAccountDao {
/**
* 根据 id 查询账户。
*
* @param accountId
* @return
*/
Account findAccountById(Integer accountId);
/**
* 根据名称查询账户。
*
* @param accountName
* @return
*/
Account findAccountByName(String accountName);
/**
* 更新账户。
*
* @param account
*/
void updateAccount(Account account);
}
- AccountDaoImpl。
package com.geek.dao.impl;
import com.geek.dao.IAccountDao;
import com.geek.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
@Override
public Account findAccountById(Integer accountId) {
List<Account> accountList = super.getJdbcTemplate().query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), accountId);
return accountList.isEmpty() ? null : accountList.get(0);
}
@Override
public Account findAccountByName(String accountName) {
List<Account> accountList = super.getJdbcTemplate().query("select * from account where name = ?", new BeanPropertyRowMapper<>(Account.class), accountName);
if (accountList.isEmpty()) {
return null;
}
if (accountList.size() > 1) {
throw new RuntimeException("结果集不唯一。");
}
return accountList.get(0);
}
@Override
public void updateAccount(Account account) {
int update = super.getJdbcTemplate().update("update account set name = ?, money = ? where id = ?", account.getName(), account.getMoney(), account.getId());
}
}
- IAccountService。接口。
package com.geek.service;
import com.geek.domain.Account;
/**
* 账户业务接口。
*/
public interface IAccountService {
/**
* 根据 id 查询账户信息。
*
* @param accountId
* @return
*/
Account findAccountById(Integer accountId);
/**
* 转账。
*
* @param fromName 转出账户名称。
* @param toName 转入账户名称。
* @param money 金额。
*/
void transfer(String fromName, String toName, Float money);
}
- AccountServiceImpl。
package com.geek.service.impl;
import com.geek.dao.IAccountDao;
import com.geek.domain.Account;
import com.geek.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
/**
* 根据 id 查询账户信息。
*
* @param accountId
* @return
*/
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
/**
* 转账。
*
* @param fromName 转出账户名称。
* @param toName 转入账户名称。
* @param money 金额。
*/
@Override
public void transfer(String fromName, String toName, Float money) {
System.out.println("start transfer...");
// 根据名称查询转出账户。
Account from = accountDao.findAccountByName(fromName);
// 根据名称查询转入账户。
Account to = accountDao.findAccountByName(toName);
// 转出账户扣钱。
from.setMoney(from.getMoney() - money);
// 转入账户加钱。
to.setMoney(to.getMoney() - money);
// 更新转出账户。
accountDao.updateAccount(from);
int i = 1 / 0;
// 更新转入账户。
accountDao.updateAccount(to);
}
}
- 测试类。
package com.geek.test;
import com.geek.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class TransferTest {
@Autowired
private IAccountService accountService;
@Test
public void testTransfer() {
accountService.transfer("aaa", "bbb", 100f);
}
}
- 配置。
<?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:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--<context:component-scan base-package="com.geek"/>-->
<!-- Spring 中基于 xml 的声明式事务控制。-->
<!-- 事务管理器。-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务通知。-->
<!-- transaction-manager="transactionManager"。给事务通知提供一个事务管理器引用。-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性。-->
<!--
isolation="" ~事务隔离级别。默认 DEFAULT。使用数据库的默认隔离级别。
no-rollback-for="" ~ 用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,表示任意异常都回滚。
propagation="" ~事务传播行为。默认 REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择 SUPPORTS。
read-only="" ~事务是否只读。只有查询方法才能设置为 true。默认 false,表示读写。
rollback-for="" ~ 用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值,表示任意异常都回滚。
timeout="" ~事务超时时间。默认 -1,永不超时。以秒为单位。
-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置 aop。-->
<aop:config>
<!-- 配置 aop 通用切入点表达式。-->
<aop:pointcut id="pt1" expression="execution(* com.geek.service.impl.*.*(..))"/>
<!-- 建立切入点表达式和事务通知的关系。-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
<!-- 配置业务层。-->
<bean id="accountService" class="com.geek.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置账户持久层。-->
<bean id="accountDao" class="com.geek.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置数据源。-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.142.135:3307/geek_spring_dbutils"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
</beans>
基于注解的声明式事务控制。
@Transactional。
package com.geek.service.impl;
import com.geek.dao.IAccountDao;
import com.geek.domain.Account;
import com.geek.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("accountService")
@Transactional
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
/**
* 根据 id 查询账户信息。
*
* @param accountId
* @return
*/
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
/**
* 转账。
*
* @param fromName 转出账户名称。
* @param toName 转入账户名称。
* @param money 金额。
*/
@Override
public void transfer(String fromName, String toName, Float money) {
System.out.println("start transfer...");
// 根据名称查询转出账户。
Account from = accountDao.findAccountByName(fromName);
// 根据名称查询转入账户。
Account to = accountDao.findAccountByName(toName);
// 转出账户扣钱。
from.setMoney(from.getMoney() - money);
// 转入账户加钱。
to.setMoney(to.getMoney() - money);
// 更新转出账户。
accountDao.updateAccount(from);
int i = 1 / 0;
// 更新转入账户。
accountDao.updateAccount(to);
}
}
- AccountDao 去掉 extends。
@Repository("accountDao")
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
- 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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.geek"/>
<!-- 配置 JdbcTemplate。-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置数据源。-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.142.135:3307/geek_spring_dbutils"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- Spring 中基于 annotation 的声明式事务控制。-->
<!--
配置事务管理器。
开启 Spring 对注解事务的支持。
在需要事务支持的地方使用 @Transaction 注解。
-->
<!-- 事务管理器。-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Spring 对注解事务的支持。-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
- 使用了
public @interface Transactional {就可以不配置 aop 了。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.transaction.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {
@AliasFor("transactionManager")
String value() default "";
@AliasFor("value")
String transactionManager() default "";
Propagation propagation() default Propagation.REQUIRED;
Isolation isolation() default Isolation.DEFAULT;
int timeout() default -1;
boolean readOnly() default false;
Class<? extends Throwable>[] rollbackFor() default {};
String[] rollbackForClassName() default {};
Class<? extends Throwable>[] noRollbackFor() default {};
String[] noRollbackForClassName() default {};
}
纯注解。
- 配置类。
package com.geek.config;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* 事务相关配置类。
*/
public class TransactionConfig {
/**
* 创建事务管理器对象。
*
* @param dataSource
* @return
*/
@Bean("transactionManager")
public PlatformTransactionManager createTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
package com.geek.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
/**
* 连接数据库相关配置。
*/
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 创建 JdbcTemplate。
*
* @param dataSource
* @return
*/
@Bean("jdbcTempalte")
public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean("dataSource")
public DataSource createDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
package com.geek.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* Spring 的配置类,相当于 bean.xml。
*/
@Configuration
@ComponentScan("com.geek")
@Import({JdbcConfig.class, TransactionConfig.class})
@PropertySource("jdbcConfig.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
- 测试类。
package com.geek.test;
import com.geek.config.SpringConfiguration;
import com.geek.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class TransferTest {
@Autowired
private IAccountService accountService;
@Test
public void testTransfer() {
accountService.transfer("aaa", "bbb", 100f);
}
}
Spring 编程式事务控制。
实际开发很少使用。
<!-- 事务模板对象。-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"/>
package com.geek.service.impl;
import com.geek.dao.IAccountDao;
import com.geek.domain.Account;
import com.geek.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)// 只读型事务。
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
/**
* 根据 id 查询账户信息。
*
* @param accountId
* @return
*/
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
/**
* 转账。
*
* @param fromName 转出账户名称。
* @param toName 转入账户名称。
* @param money 金额。
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
@Override
public void transfer(String fromName, String toName, Float money) {
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus transactionStatus) {
System.out.println("start transfer...");
// 根据名称查询转出账户。
Account from = accountDao.findAccountByName(fromName);
// 根据名称查询转入账户。
Account to = accountDao.findAccountByName(toName);
// 转出账户扣钱。
from.setMoney(from.getMoney() - money);
// 转入账户加钱。
to.setMoney(to.getMoney() - money);
// 更新转出账户。
accountDao.updateAccount(from);
int i = 1 / 0;
// 更新转入账户。
accountDao.updateAccount(to);
return null;
}
});
}
}
@Nullable
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
Assert.state(this.transactionManager != null, "No PlatformTransactionManager set");
if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
return ((CallbackPreferringPlatformTransactionManager)this.transactionManager).execute(this, action);
} else {
TransactionStatus status = this.transactionManager.getTransaction(this);
Object result;
try {
result = action.doInTransaction(status);
} catch (Error | RuntimeException var5) {
this.rollbackOnException(status, var5);
throw var5;
} catch (Throwable var6) {
this.rollbackOnException(status, var6);
throw new UndeclaredThrowableException(var6, "TransactionCallback threw undeclared checked exception");
}
this.transactionManager.commit(status);
return result;
}
}

本文深入解析Spring框架中的事务控制机制,包括声明式和编程式事务管理,探讨了PlatformTransactionManager、TransactionDefinition等核心接口,以及事务隔离级别、传播行为等关键概念。通过XML配置和注解两种方式,展示了如何在业务层实现事务处理。
1393

被折叠的 条评论
为什么被折叠?



