赞
踩
目录
在Java中,事务(Transaction)是指作为单个逻辑工作单元执行的一系列操作。当涉及到数据库操作时,事务确保了数据的完整性和一致性,即使在出现故障的情况下也能保证数据的安全。事务的处理通常在数据库管理系统(DBMS)层面进行,但在Java环境中,事务的管理也可以通过编程框架如Spring或Java Transaction API (JTA)来控制。
事务具有以下四个主要特性,通常称为ACID特性:
原子性(Atomicity):
一致性(Consistency):
隔离性(Isolation):
持久性(Durability):
事务在Java应用中通常用于以下几种情况:
多表更新:
复杂业务流程:
并发控制:
批量操作:
安全性:
在Java中,事务的管理可以通过手动的方式使用Connection
对象的commit()
和rollback()
方法,也可以通过Spring框架的声明式事务管理来自动处理事务边界。Spring框架允许通过注解(如@Transactional
)或XML配置来控制事务的开始和结束,这大大简化了事务的管理。
Spring Boot各版本对事务管理的开启和配置方式大体上保持了一致性,但具体实现细节和默认行为可能会随版本的更新而略有不同。下面是一些关键点,说明Spring Boot不同版本中事务管理的主要区别:
Spring Boot1.X版本
@EnableTransactionManagement
注解来显式地启用。@EnableTransactionManagement
注解,以启动Spring的事务管理功能。@Transactional
注解用于标记需要事务支持的类或方法。DataSourceTransactionManager
,并将其注册到Spring容器中。启动类配置:
-
- /**
- * 启动服务
- */
- @SpringBootApplication
- @RestController
- @EnableTransactionManagement //开启事务
- public class TestApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(TestApplication .class, args);
- }
- }
开启全局配置:
- package com.config;
-
- import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import org.springframework.transaction.interceptor.TransactionInterceptor;
-
- import javax.persistence.Transient;
- import javax.sql.DataSource;
- import java.util.Properties;
-
- /**
- * Title: 事务配置类
- */
- @Configuration
- public class TxConfigBeanName {
- @Autowired(required=true)
- @Transient
- private DataSourceTransactionManager transactionManager;
-
-
- @Bean
- public DataSourceTransactionManager transactionManager(DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- // 创建事务通知
- @Bean(name = "txAdvice")
- public TransactionInterceptor getAdvisor() {
-
- Properties properties = new Properties();
- properties.setProperty("add*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("save*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("modify*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("update*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("delete*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("*Delete*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("disable*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("enable*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("handle*", "PROPAGATION_REQUIRED,-ServiceException");
- // properties.setProperty("*", "PROPAGATION_REQUIRED,-ServiceException,readOnly");
-
- TransactionInterceptor tsi = new TransactionInterceptor(transactionManager,properties);
- return tsi;
-
- }
-
- @Bean
- public BeanNameAutoProxyCreator txProxy() {
- BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
- creator.setInterceptorNames("txAdvice");
- creator.setBeanNames("*Service", "*ServiceImpl");
- creator.setProxyTargetClass(true);
- return creator;
- }
- }
@Transactional
应用
- @Transactional(rollbackFor = Exception.class)
- public void save(test vo) {
- saveTest(vo);//保存主表
- saveItem(vo.getItem());//保存子表
- }
Spring Boot 2.x 版本
JpaTransactionManager
或相应的事务管理器。@EnableTransactionManagement
注解在大多数情况下不再是必需的,因为事务管理的自动配置会自动包含它。@Transactional
注解来控制事务的范围和行为。开启全局配置:
- package com.config;
-
- import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import org.springframework.transaction.interceptor.TransactionInterceptor;
-
- import javax.persistence.Transient;
- import javax.sql.DataSource;
- import java.util.Properties;
-
- /**
- * Title: 事务配置类
- */
- @Configuration
- public class TxConfigBeanName {
- @Autowired(required=true)
- @Transient
- private DataSourceTransactionManager transactionManager;
-
-
- @Bean
- public DataSourceTransactionManager transactionManager(DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- // 创建事务通知
- @Bean(name = "txAdvice")
- public TransactionInterceptor getAdvisor() {
-
- Properties properties = new Properties();
- properties.setProperty("add*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("save*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("modify*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("update*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("delete*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("*Delete*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("disable*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("enable*", "PROPAGATION_REQUIRED,-ServiceException");
- properties.setProperty("handle*", "PROPAGATION_REQUIRED,-ServiceException");
- // properties.setProperty("*", "PROPAGATION_REQUIRED,-ServiceException,readOnly");
-
- TransactionInterceptor tsi = new TransactionInterceptor(transactionManager,properties);
- return tsi;
-
- }
-
- @Bean
- public BeanNameAutoProxyCreator txProxy() {
- BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
- creator.setInterceptorNames("txAdvice");
- creator.setBeanNames("*Service", "*ServiceImpl");
- creator.setProxyTargetClass(true);
- return creator;
- }
- }
@Transactional
应用
- @Transactional(rollbackFor = Exception.class)
- public void save(test vo) {
- saveTest(vo);//保存主表
- saveItem(vo.getItem());//保存子表
- }
以上即为SpringBoot的事务开启、配置与使用方式!对你有所帮助,关注博主或点个赞再走吧!更多资源也可关注微信公众号获取!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。