Spring在多線程下保持事務(wù)的一致性的方法實(shí)現(xiàn)
方法:每個(gè)線程都開(kāi)啟各自的事務(wù)去執(zhí)行相關(guān)業(yè)務(wù),等待所有線程的業(yè)務(wù)執(zhí)行完成,統(tǒng)一提交或回滾。
下面我們通過(guò)具體的案例來(lái)演示Spring如何在多線程下保持事務(wù)的一致性。
1、項(xiàng)目結(jié)構(gòu)
2、數(shù)據(jù)庫(kù)SQL
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
3、pom依賴
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>Transaction</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Transaction</name> <description>Transaction</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4、配置文件
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5、實(shí)體類
package com.example.transaction.model; import java.io.Serializable; /** * @author tom */ public class Student implements Serializable { private static final long serialVersionUID = 1L; private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(String name) { this.name = name; } }
6、Mapper
package com.example.transaction.mapper; import com.example.transaction.model.Student; import org.apache.ibatis.annotations.Insert; import org.springframework.stereotype.Component; /** * @author tom */ @Component public interface StudentMapper { /** * 插入student * @param student */ @Insert("insert into student(name) VALUES(#{name})") void insert(Student student); }
7、數(shù)據(jù)源配置
package com.example.transaction.config; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * @author tom */ @Configuration @MapperScan(basePackages = "com.example.transaction.mapper") public class DataSourceConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource getDataSource() { return DataSourceBuilder.create().build(); } @Bean public DataSourceTransactionManager getTransactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
8、測(cè)試
package com.example.transaction; import com.example.transaction.mapper.StudentMapper; import com.example.transaction.model.Student; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class TransactionApplicationTests { @Autowired private StudentMapper studentMapper; @Test void contextLoads() { studentMapper.insert(new Student("John")); } }
我們先進(jìn)行測(cè)試,看數(shù)據(jù)庫(kù)是否可以正常插入,執(zhí)行完的結(jié)果:
id | name |
---|---|
1 | John |
9、線程池
package com.example.transaction.config; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @author tom */ public class ExecutorConfig { private final static int MAX_POOL_SIZE = Runtime.getRuntime().availableProcessors(); private final static int QUEUE_SIZE = 500; private volatile static ExecutorService executorService; public static ExecutorService getThreadPool() { if (executorService == null) { synchronized (ExecutorConfig.class) { if (executorService == null) { executorService = newThreadPool(); } } } return executorService; } private static ExecutorService newThreadPool() { int corePool = Math.min(5, MAX_POOL_SIZE); return new ThreadPoolExecutor(corePool, MAX_POOL_SIZE, 10000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(QUEUE_SIZE), new ThreadPoolExecutor.AbortPolicy()); } private ExecutorConfig() { } }
10、多線程事務(wù)管理
package com.example.transaction.service; import com.example.transaction.config.ExecutorConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; /** * @author tom */ @Service public class MultiThreadingTransactionManager { /** * 數(shù)據(jù)源事務(wù)管理器 */ private DataSourceTransactionManager dataSourceTransactionManager; @Autowired public void setUserService(DataSourceTransactionManager dataSourceTransactionManager) { this.dataSourceTransactionManager = dataSourceTransactionManager; } /** * 用于判斷子線程業(yè)務(wù)是否處理完成 * 處理完成時(shí)threadCountDownLatch的值為0 */ private CountDownLatch threadCountDownLatch; /** * 用于等待子線程全部完成后,子線程統(tǒng)一進(jìn)行提交和回滾 * 進(jìn)行提交和回滾時(shí)mainCountDownLatch的值為0 */ private final CountDownLatch mainCountDownLatch = new CountDownLatch(1); /** * 是否提交事務(wù),默認(rèn)是true,當(dāng)子線程有異常發(fā)生時(shí),設(shè)置為false,回滾事務(wù) */ private final AtomicBoolean isSubmit = new AtomicBoolean(true); public boolean execute(List<Runnable> runnableList) { // 超時(shí)時(shí)間 long timeout = 30; setThreadCountDownLatch(runnableList.size()); ExecutorService executorService = ExecutorConfig.getThreadPool(); runnableList.forEach(runnable -> executorService.execute(() -> executeThread(runnable, threadCountDownLatch, mainCountDownLatch, isSubmit))); // 等待子線程全部執(zhí)行完畢 try { // 若計(jì)數(shù)器變?yōu)榱懔?則返回 true boolean isFinish = threadCountDownLatch.await(timeout, TimeUnit.SECONDS); if (!isFinish) { // 如果還有為執(zhí)行完成的就回滾 isSubmit.set(false); System.out.println("存在子線程在預(yù)期時(shí)間內(nèi)未執(zhí)行完畢,任務(wù)將全部回滾"); } } catch (Exception exception) { System.out.println("主線程發(fā)生異常,異常為: " + exception.getMessage()); } finally { // 計(jì)數(shù)器減1,代表該主線程執(zhí)行完畢 mainCountDownLatch.countDown(); } // 返回結(jié)果,是否執(zhí)行成功,事務(wù)提交即為執(zhí)行成功,事務(wù)回滾即為執(zhí)行失敗 return isSubmit.get(); } private void executeThread(Runnable runnable, CountDownLatch threadCountDownLatch, CountDownLatch mainCountDownLatch, AtomicBoolean isSubmit) { System.out.println("子線程: [" + Thread.currentThread().getName() + "]"); // 判斷別的子線程是否已經(jīng)出現(xiàn)錯(cuò)誤,錯(cuò)誤別的線程已經(jīng)出現(xiàn)錯(cuò)誤,那么所有的都要回滾,這個(gè)子線程就沒(méi)有必要執(zhí)行了 if (!isSubmit.get()) { System.out.println("整個(gè)事務(wù)中有子線程執(zhí)行失敗需要回滾, 子線程: [" + Thread.currentThread().getName() + "] 終止執(zhí)行"); // 計(jì)數(shù)器減1,代表該子線程執(zhí)行完畢 threadCountDownLatch.countDown(); return; } // 開(kāi)啟事務(wù) DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition); try { // 執(zhí)行業(yè)務(wù)邏輯 runnable.run(); } catch (Exception exception) { // 發(fā)生異常需要進(jìn)行回滾,設(shè)置isSubmit為false isSubmit.set(false); System.out.println("子線程: [" + Thread.currentThread().getName() + "]執(zhí)行業(yè)務(wù)發(fā)生異常,異常為: " + exception.getMessage()); } finally { // 計(jì)數(shù)器減1,代表該子線程執(zhí)行完畢 threadCountDownLatch.countDown(); } try { // 等待主線程執(zhí)行 mainCountDownLatch.await(); } catch (Exception exception) { System.out.println("子線程: [" + Thread.currentThread().getName() + "]等待提交或回滾異常,異常為: " + exception.getMessage()); } try { // 提交 if (isSubmit.get()) { dataSourceTransactionManager.commit(transactionStatus); System.out.println("子線程: [" + Thread.currentThread().getName() + "]進(jìn)行事務(wù)提交"); } else { dataSourceTransactionManager.rollback(transactionStatus); System.out.println("子線程: [" + Thread.currentThread().getName() + "]進(jìn)行事務(wù)回滾"); } } catch (Exception exception) { System.out.println("子線程: [" + Thread.currentThread().getName() + "]進(jìn)行事務(wù)提交或回滾出現(xiàn)異常,異常為:" + exception.getMessage()); } } private void setThreadCountDownLatch(int num) { this.threadCountDownLatch = new CountDownLatch(num); } }
11、正常插入
package com.example.transaction; import com.example.transaction.mapper.StudentMapper; import com.example.transaction.model.Student; import com.example.transaction.service.MultiThreadingTransactionManager; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @SpringBootTest public class TransactionApplicationTwoTests { @Autowired private StudentMapper studentMapper; @Autowired private MultiThreadingTransactionManager multiThreadingTransactionManager; @Test void contextLoads() { List<Student> studentList = new ArrayList<>(); studentList.add(new Student("tom")); studentList.add(new Student("marry")); List<Runnable> runnableList = new ArrayList<>(); studentList.forEach(student -> runnableList.add(() -> { System.out.println("當(dāng)前線程:[" + Thread.currentThread().getName() + "] 插入數(shù)據(jù): " + student); try { studentMapper.insert(student); } catch (Exception e) { e.printStackTrace(); } })); boolean isSuccess = multiThreadingTransactionManager.execute(runnableList); System.out.println(isSuccess); } }
日志輸出:
......
子線程: [pool-1-thread-2]
子線程: [pool-1-thread-1]
2023-11-26 17:15:42.138 INFO 15736 --- [pool-1-thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-11-26 17:15:42.319 INFO 15736 --- [pool-1-thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
當(dāng)前線程:[pool-1-thread-2] 插入數(shù)據(jù): com.example.transaction.model.Student@1f52ee45
當(dāng)前線程:[pool-1-thread-1] 插入數(shù)據(jù): com.example.transaction.model.Student@238acf6d
true
子線程: [pool-1-thread-2]進(jìn)行事務(wù)提交
子線程: [pool-1-thread-1]進(jìn)行事務(wù)提交
數(shù)據(jù)庫(kù)中的數(shù)據(jù):
id | name |
---|---|
1 | John |
2 | tom |
3 | marry |
12、異常插入
package com.example.transaction; import com.example.transaction.mapper.StudentMapper; import com.example.transaction.model.Student; import com.example.transaction.service.MultiThreadingTransactionManager; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @SpringBootTest public class TransactionApplicationThreeTests { @Autowired private StudentMapper studentMapper; @Autowired private MultiThreadingTransactionManager multiThreadingTransactionManager; @Test void contextLoads() { List<Student> studentList = new ArrayList<>(); studentList.add(new Student("張三")); studentList.add(new Student("李四")); List<Runnable> runnableList = new ArrayList<>(); studentList.forEach(student -> runnableList.add(() -> { System.out.println("當(dāng)前線程:[" + Thread.currentThread().getName() + "] 插入數(shù)據(jù): " + student); try { studentMapper.insert(student); } catch (Exception e) { e.printStackTrace(); } })); runnableList.add(() -> System.out.println(1 / 0)); boolean isSuccess = multiThreadingTransactionManager.execute(runnableList); System.out.println(isSuccess); } }
日志輸出:
......
子線程: [pool-1-thread-1]
子線程: [pool-1-thread-2]
子線程: [pool-1-thread-3]
2023-11-26 17:19:45.876 INFO 11384 --- [pool-1-thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-11-26 17:19:46.034 INFO 11384 --- [pool-1-thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
子線程: [pool-1-thread-3]執(zhí)行業(yè)務(wù)發(fā)生異常,異常為: / by zero
當(dāng)前線程:[pool-1-thread-1] 插入數(shù)據(jù): com.example.transaction.model.Student@6231e93c
當(dāng)前線程:[pool-1-thread-2] 插入數(shù)據(jù): com.example.transaction.model.Student@74568de7
false
子線程: [pool-1-thread-3]進(jìn)行事務(wù)回滾
子線程: [pool-1-thread-2]進(jìn)行事務(wù)回滾
數(shù)據(jù)庫(kù)中的數(shù)據(jù):
id | name |
---|---|
1 | John |
2 | tom |
3 | marry |
從上面我們可以看出事務(wù)進(jìn)行了回滾,并沒(méi)有插入到數(shù)據(jù)庫(kù)中。
13、在主線程中統(tǒng)一進(jìn)行事務(wù)的提交和回滾
這里將事務(wù)的回滾放在所有子線程執(zhí)行完畢之后。
package com.example.transaction.service; import com.example.transaction.config.ExecutorConfig; import lombok.Builder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.transaction.support.TransactionSynchronization; import org.springframework.transaction.support.TransactionSynchronizationManager; import javax.sql.DataSource; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; /** * @author tom */ @Service public class MultiThreadingTransactionManagerTwo { /** * 數(shù)據(jù)源事務(wù)管理器 */ private DataSourceTransactionManager dataSourceTransactionManager; @Autowired public void setUserService(DataSourceTransactionManager dataSourceTransactionManager) { this.dataSourceTransactionManager = dataSourceTransactionManager; } /** * 用于判斷子線程業(yè)務(wù)是否處理完成 * 處理完成時(shí)threadCountDownLatch的值為0 */ private CountDownLatch threadCountDownLatch; /** * 是否提交事務(wù),默認(rèn)是true,當(dāng)子線程有異常發(fā)生時(shí),設(shè)置為false,回滾事務(wù) */ private final AtomicBoolean isSubmit = new AtomicBoolean(true); public boolean execute(List<Runnable> runnableList) { // 超時(shí)時(shí)間 long timeout = 30; List<TransactionStatus> transactionStatusList = Collections.synchronizedList(new ArrayList<>()); List<TransactionResource> transactionResourceList = Collections.synchronizedList(new ArrayList<>()); setThreadCountDownLatch(runnableList.size()); ExecutorService executorService = ExecutorConfig.getThreadPool(); runnableList.forEach(runnable -> executorService.execute(() -> { try { // 執(zhí)行業(yè)務(wù)邏輯 executeThread(runnable, transactionStatusList, transactionResourceList); } catch (Exception exception) { exception.printStackTrace(); // 執(zhí)行異常,需要回滾 isSubmit.set(false); } finally { threadCountDownLatch.countDown(); } })); // 等待子線程全部執(zhí)行完畢 try { // 若計(jì)數(shù)器變?yōu)榱懔?則返回 true boolean isFinish = threadCountDownLatch.await(timeout, TimeUnit.SECONDS); if (!isFinish) { // 如果還有為執(zhí)行完成的就回滾 isSubmit.set(false); System.out.println("存在子線程在預(yù)期時(shí)間內(nèi)未執(zhí)行完畢,任務(wù)將全部回滾"); } } catch (Exception exception) { exception.printStackTrace(); } // 發(fā)生了異常則進(jìn)行回滾操作,否則提交 if (isSubmit.get()) { System.out.println("全部事務(wù)正常提交"); for (int i = 0; i < runnableList.size(); i++) { transactionResourceList.get(i).autoWiredTransactionResource(); dataSourceTransactionManager.commit(transactionStatusList.get(i)); transactionResourceList.get(i).removeTransactionResource(); } } else { System.out.println("發(fā)生異常,全部事務(wù)回滾"); for (int i = 0; i < runnableList.size(); i++) { transactionResourceList.get(i).autoWiredTransactionResource(); dataSourceTransactionManager.rollback(transactionStatusList.get(i)); transactionResourceList.get(i).removeTransactionResource(); } } // 返回結(jié)果,是否執(zhí)行成功,事務(wù)提交即為執(zhí)行成功,事務(wù)回滾即為執(zhí)行失敗 return isSubmit.get(); } private void executeThread(Runnable runnable, List<TransactionStatus> transactionStatusList, List<TransactionResource> transactionResourceList) { System.out.println("子線程: [" + Thread.currentThread().getName() + "]"); DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition); // 開(kāi)啟新事務(wù) transactionStatusList.add(transactionStatus); // copy事務(wù)資源 transactionResourceList.add(TransactionResource.copyTransactionResource()); // 執(zhí)行業(yè)務(wù)邏輯 runnable.run(); } private void setThreadCountDownLatch(int num) { this.threadCountDownLatch = new CountDownLatch(num); } /** * 保存當(dāng)前事務(wù)資源,用于線程間的事務(wù)資源COPY操作 * <p> * `@Builder`注解是Lombok庫(kù)提供的一個(gè)注解,它可以用于自動(dòng)生成Builder模式的代碼,使用@Builder注解可以簡(jiǎn)化創(chuàng)建對(duì)象實(shí)例的過(guò)程,并且可以使代碼更加清晰和易于維護(hù) */ @Builder private static class TransactionResource { // TransactionSynchronizationManager類內(nèi)部默認(rèn)提供了下面六個(gè)ThreadLocal屬性,分別保存當(dāng)前線程對(duì)應(yīng)的不同事務(wù)資源 // 保存當(dāng)前事務(wù)關(guān)聯(lián)的資源,默認(rèn)只會(huì)在新建事務(wù)的時(shí)候保存當(dāng)前獲取到的DataSource和當(dāng)前事務(wù)對(duì)應(yīng)Connection的映射關(guān)系 // 當(dāng)然這里Connection被包裝為了ConnectionHolder // 事務(wù)結(jié)束后默認(rèn)會(huì)移除集合中的DataSource作為key關(guān)聯(lián)的資源記錄 private Map<Object, Object> resources; //下面五個(gè)屬性會(huì)在事務(wù)結(jié)束后被自動(dòng)清理,無(wú)需我們手動(dòng)清理 // 事務(wù)監(jiān)聽(tīng)者,在事務(wù)執(zhí)行到某個(gè)階段的過(guò)程中,會(huì)去回調(diào)監(jiān)聽(tīng)者對(duì)應(yīng)的回調(diào)接口(典型觀察者模式的應(yīng)用),默認(rèn)為空集合 private Set<TransactionSynchronization> synchronizations; // 存放當(dāng)前事務(wù)名字 private String currentTransactionName; // 存放當(dāng)前事務(wù)是否是只讀事務(wù) private Boolean currentTransactionReadOnly; // 存放當(dāng)前事務(wù)的隔離級(jí)別 private Integer currentTransactionIsolationLevel; // 存放當(dāng)前事務(wù)是否處于激活狀態(tài) private Boolean actualTransactionActive; /** * 對(duì)事務(wù)資源進(jìn)行復(fù)制 * * @return TransactionResource */ public static TransactionResource copyTransactionResource() { return TransactionResource.builder() //返回的是不可變集合 .resources(TransactionSynchronizationManager.getResourceMap()) //如果需要注冊(cè)事務(wù)監(jiān)聽(tīng)者,這里記得修改,我們這里不需要,就采用默認(rèn)負(fù)責(zé),spring事務(wù)內(nèi)部默認(rèn)也是這個(gè)值 .synchronizations(new LinkedHashSet<>()).currentTransactionName(TransactionSynchronizationManager.getCurrentTransactionName()).currentTransactionReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).currentTransactionIsolationLevel(TransactionSynchronizationManager.getCurrentTransactionIsolationLevel()).actualTransactionActive(TransactionSynchronizationManager.isActualTransactionActive()).build(); } /** * 使用 */ public void autoWiredTransactionResource() { resources.forEach(TransactionSynchronizationManager::bindResource); //如果需要注冊(cè)事務(wù)監(jiān)聽(tīng)者,這里記得修改,我們這里不需要,就采用默認(rèn)負(fù)責(zé),spring事務(wù)內(nèi)部默認(rèn)也是這個(gè)值 TransactionSynchronizationManager.initSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(actualTransactionActive); TransactionSynchronizationManager.setCurrentTransactionName(currentTransactionName); TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(currentTransactionIsolationLevel); TransactionSynchronizationManager.setCurrentTransactionReadOnly(currentTransactionReadOnly); } /** * 移除 */ public void removeTransactionResource() { // 事務(wù)結(jié)束后默認(rèn)會(huì)移除集合中的DataSource作為key關(guān)聯(lián)的資源記錄 // DataSource如果重復(fù)移除,unbindResource時(shí)會(huì)因?yàn)椴淮嬖诖薻ey關(guān)聯(lián)的事務(wù)資源而報(bào)錯(cuò) resources.keySet().forEach(key -> { if (!(key instanceof DataSource)) { TransactionSynchronizationManager.unbindResource(key); } }); } } }
13.1 正常插入
package com.example.transaction; import com.example.transaction.mapper.StudentMapper; import com.example.transaction.model.Student; import com.example.transaction.service.MultiThreadingTransactionManagerTwo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @SpringBootTest public class TransactionApplicationFourTests { @Autowired private StudentMapper studentMapper; @Autowired private MultiThreadingTransactionManagerTwo multiThreadingTransactionManagerTwo; @Test void contextLoads() { List<Student> studentList = new ArrayList<>(); studentList.add(new Student("tom")); studentList.add(new Student("marry")); List<Runnable> runnableList = new ArrayList<>(); studentList.forEach(student -> runnableList.add(() -> { System.out.println("當(dāng)前線程:[" + Thread.currentThread().getName() + "] 插入數(shù)據(jù): " + student); try { studentMapper.insert(student); } catch (Exception e) { e.printStackTrace(); } })); boolean isSuccess = multiThreadingTransactionManagerTwo.execute(runnableList); System.out.println(isSuccess); } }
日志輸出:
......
子線程: [pool-1-thread-1]
子線程: [pool-1-thread-2]
2023-11-26 18:57:13.096 INFO 4280 --- [pool-1-thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-11-26 18:57:13.256 INFO 4280 --- [pool-1-thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
當(dāng)前線程:[pool-1-thread-2] 插入數(shù)據(jù): com.example.transaction.model.Student@6cf36c13
當(dāng)前線程:[pool-1-thread-1] 插入數(shù)據(jù): com.example.transaction.model.Student@7fc3efd5
全部事務(wù)正常提交
true
數(shù)據(jù)庫(kù)中的數(shù)據(jù):
id | name |
---|---|
1 | John |
2 | tom |
3 | marry |
6 | tom |
7 | marry |
13.2 異常插入
package com.example.transaction; import com.example.transaction.mapper.StudentMapper; import com.example.transaction.model.Student; import com.example.transaction.service.MultiThreadingTransactionManager; import com.example.transaction.service.MultiThreadingTransactionManagerTwo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @SpringBootTest public class TransactionApplicationFiveTests { @Autowired private StudentMapper studentMapper; @Autowired private MultiThreadingTransactionManagerTwo multiThreadingTransactionManagerTwo; @Test void contextLoads() { List<Student> studentList = new ArrayList<>(); studentList.add(new Student("張三")); studentList.add(new Student("李四")); List<Runnable> runnableList = new ArrayList<>(); studentList.forEach(student -> runnableList.add(() -> { System.out.println("當(dāng)前線程:[" + Thread.currentThread().getName() + "] 插入數(shù)據(jù): " + student); try { studentMapper.insert(student); } catch (Exception e) { e.printStackTrace(); } })); runnableList.add(() -> System.out.println(1 / 0)); boolean isSuccess = multiThreadingTransactionManagerTwo.execute(runnableList); System.out.println(isSuccess); } }
日志輸出:
子線程: [pool-1-thread-1]
子線程: [pool-1-thread-3]
子線程: [pool-1-thread-2]
2023-11-26 19:00:40.938 INFO 17920 --- [pool-1-thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-11-26 19:00:41.097 INFO 17920 --- [pool-1-thread-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
當(dāng)前線程:[pool-1-thread-1] 插入數(shù)據(jù): com.example.transaction.model.Student@2f7e458
當(dāng)前線程:[pool-1-thread-2] 插入數(shù)據(jù): com.example.transaction.model.Student@2b3ae8b
java.lang.ArithmeticException: / by zero
at com.example.transaction.TransactionApplicationFiveTests.lambda$contextLoads$2(TransactionApplicationFiveTests.java:37)
at com.example.transaction.service.MultiThreadingTransactionManagerTwo.executeThread(MultiThreadingTransactionManagerTwo.java:107)
at com.example.transaction.service.MultiThreadingTransactionManagerTwo.lambda$null$0(MultiThreadingTransactionManagerTwo.java:57)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
發(fā)生異常,全部事務(wù)回滾
false
數(shù)據(jù)庫(kù)中的數(shù)據(jù):
id | name |
---|---|
1 | John |
2 | tom |
3 | marry |
6 | tom |
7 | marry |
14、使用CompletableFuture實(shí)現(xiàn)
package com.example.transaction.service; import lombok.Builder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.transaction.support.TransactionSynchronization; import org.springframework.transaction.support.TransactionSynchronizationManager; import javax.sql.DataSource; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; /** * @author tom */ @Service public class MultiThreadingTransactionManagerThree { /** * 數(shù)據(jù)源事務(wù)管理器 */ private DataSourceTransactionManager dataSourceTransactionManager; @Autowired public void setUserService(DataSourceTransactionManager dataSourceTransactionManager) { this.dataSourceTransactionManager = dataSourceTransactionManager; } /** * 是否提交事務(wù),默認(rèn)是true,當(dāng)子線程有異常發(fā)生時(shí),設(shè)置為false,回滾事務(wù) */ private final AtomicBoolean isSubmit = new AtomicBoolean(true); public boolean execute(List<Runnable> runnableList) { List<TransactionStatus> transactionStatusList = Collections.synchronizedList(new ArrayList<>()); List<TransactionResource> transactionResourceList = Collections.synchronizedList(new ArrayList<>()); List<CompletableFuture<?>> completableFutureList = new ArrayList<>(runnableList.size()); runnableList.forEach(runnable -> completableFutureList.add(CompletableFuture.runAsync(() -> { try { // 執(zhí)行業(yè)務(wù)邏輯 executeThread(runnable, transactionStatusList, transactionResourceList); } catch (Exception exception) { exception.printStackTrace(); // 執(zhí)行異常,需要回滾 isSubmit.set(false); // 終止其它還未執(zhí)行的任務(wù) completableFutureList.forEach(completableFuture -> completableFuture.cancel(true)); } }))); // 等待子線程全部執(zhí)行完畢 try { // 阻塞直到所有任務(wù)全部執(zhí)行結(jié)束,如果有任務(wù)被取消,這里會(huì)拋出異常,需要捕獲 CompletableFuture.allOf(completableFutureList.toArray(new CompletableFuture[]{})).get(); } catch (Exception exception) { exception.printStackTrace(); } // 發(fā)生了異常則進(jìn)行回滾操作,否則提交 if (!isSubmit.get()) { System.out.println("發(fā)生異常,全部事務(wù)回滾"); for (int i = 0; i < runnableList.size(); i++) { transactionResourceList.get(i).autoWiredTransactionResource(); dataSourceTransactionManager.rollback(transactionStatusList.get(i)); transactionResourceList.get(i).removeTransactionResource(); } } else { System.out.println("全部事務(wù)正常提交"); for (int i = 0; i < runnableList.size(); i++) { transactionResourceList.get(i).autoWiredTransactionResource(); dataSourceTransactionManager.commit(transactionStatusList.get(i)); transactionResourceList.get(i).removeTransactionResource(); } } // 返回結(jié)果,是否執(zhí)行成功,事務(wù)提交即為執(zhí)行成功,事務(wù)回滾即為執(zhí)行失敗 return isSubmit.get(); } private void executeThread(Runnable runnable, List<TransactionStatus> transactionStatusList, List<TransactionResource> transactionResourceList) { System.out.println("子線程: [" + Thread.currentThread().getName() + "]"); DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition); // 開(kāi)啟新事務(wù) transactionStatusList.add(transactionStatus); // copy事務(wù)資源 transactionResourceList.add(TransactionResource.copyTransactionResource()); // 執(zhí)行業(yè)務(wù)邏輯 runnable.run(); } /** * 保存當(dāng)前事務(wù)資源,用于線程間的事務(wù)資源COPY操作 * <p> * `@Builder`注解是Lombok庫(kù)提供的一個(gè)注解,它可以用于自動(dòng)生成Builder模式的代碼,使用@Builder注解可以簡(jiǎn)化創(chuàng)建對(duì)象實(shí)例的過(guò)程,并且可以使代碼更加清晰和易于維護(hù) */ @Builder private static class TransactionResource { // TransactionSynchronizationManager類內(nèi)部默認(rèn)提供了下面六個(gè)ThreadLocal屬性,分別保存當(dāng)前線程對(duì)應(yīng)的不同事務(wù)資源 // 保存當(dāng)前事務(wù)關(guān)聯(lián)的資源,默認(rèn)只會(huì)在新建事務(wù)的時(shí)候保存當(dāng)前獲取到的DataSource和當(dāng)前事務(wù)對(duì)應(yīng)Connection的映射關(guān)系 // 當(dāng)然這里Connection被包裝為了ConnectionHolder // 事務(wù)結(jié)束后默認(rèn)會(huì)移除集合中的DataSource作為key關(guān)聯(lián)的資源記錄 private Map<Object, Object> resources; //下面五個(gè)屬性會(huì)在事務(wù)結(jié)束后被自動(dòng)清理,無(wú)需我們手動(dòng)清理 // 事務(wù)監(jiān)聽(tīng)者,在事務(wù)執(zhí)行到某個(gè)階段的過(guò)程中,會(huì)去回調(diào)監(jiān)聽(tīng)者對(duì)應(yīng)的回調(diào)接口(典型觀察者模式的應(yīng)用),默認(rèn)為空集合 private Set<TransactionSynchronization> synchronizations; // 存放當(dāng)前事務(wù)名字 private String currentTransactionName; // 存放當(dāng)前事務(wù)是否是只讀事務(wù) private Boolean currentTransactionReadOnly; // 存放當(dāng)前事務(wù)的隔離級(jí)別 private Integer currentTransactionIsolationLevel; // 存放當(dāng)前事務(wù)是否處于激活狀態(tài) private Boolean actualTransactionActive; /** * 對(duì)事務(wù)資源進(jìn)行復(fù)制 * * @return TransactionResource */ public static TransactionResource copyTransactionResource() { return TransactionResource.builder() //返回的是不可變集合 .resources(TransactionSynchronizationManager.getResourceMap()) //如果需要注冊(cè)事務(wù)監(jiān)聽(tīng)者,這里記得修改,我們這里不需要,就采用默認(rèn)負(fù)責(zé),spring事務(wù)內(nèi)部默認(rèn)也是這個(gè)值 .synchronizations(new LinkedHashSet<>()).currentTransactionName(TransactionSynchronizationManager.getCurrentTransactionName()).currentTransactionReadOnly(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).currentTransactionIsolationLevel(TransactionSynchronizationManager.getCurrentTransactionIsolationLevel()).actualTransactionActive(TransactionSynchronizationManager.isActualTransactionActive()).build(); } /** * 使用 */ public void autoWiredTransactionResource() { resources.forEach(TransactionSynchronizationManager::bindResource); //如果需要注冊(cè)事務(wù)監(jiān)聽(tīng)者,這里記得修改,我們這里不需要,就采用默認(rèn)負(fù)責(zé),spring事務(wù)內(nèi)部默認(rèn)也是這個(gè)值 TransactionSynchronizationManager.initSynchronization(); TransactionSynchronizationManager.setActualTransactionActive(actualTransactionActive); TransactionSynchronizationManager.setCurrentTransactionName(currentTransactionName); TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(currentTransactionIsolationLevel); TransactionSynchronizationManager.setCurrentTransactionReadOnly(currentTransactionReadOnly); } /** * 移除 */ public void removeTransactionResource() { // 事務(wù)結(jié)束后默認(rèn)會(huì)移除集合中的DataSource作為key關(guān)聯(lián)的資源記錄 // DataSource如果重復(fù)移除,unbindResource時(shí)會(huì)因?yàn)椴淮嬖诖薻ey關(guān)聯(lián)的事務(wù)資源而報(bào)錯(cuò) resources.keySet().forEach(key -> { if (!(key instanceof DataSource)) { TransactionSynchronizationManager.unbindResource(key); } }); } } }
14.1 正常插入
package com.example.transaction; import com.example.transaction.mapper.StudentMapper; import com.example.transaction.model.Student; import com.example.transaction.service.MultiThreadingTransactionManagerThree; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @SpringBootTest public class TransactionApplicationSixTests { @Autowired private StudentMapper studentMapper; @Autowired private MultiThreadingTransactionManagerThree multiThreadingTransactionManagerThree; @Test void contextLoads() { List<Student> studentList = new ArrayList<>(); studentList.add(new Student("tom")); studentList.add(new Student("marry")); List<Runnable> runnableList = new ArrayList<>(); studentList.forEach(student -> runnableList.add(() -> { System.out.println("當(dāng)前線程:[" + Thread.currentThread().getName() + "] 插入數(shù)據(jù): " + student); try { studentMapper.insert(student); } catch (Exception e) { e.printStackTrace(); } })); boolean isSuccess = multiThreadingTransactionManagerThree.execute(runnableList); System.out.println(isSuccess); } }
日志輸出:
子線程: [ForkJoinPool.commonPool-worker-1]
子線程: [ForkJoinPool.commonPool-worker-2]
2023-11-26 19:17:00.674 INFO 12344 --- [onPool-worker-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-11-26 19:17:00.815 INFO 12344 --- [onPool-worker-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
當(dāng)前線程:[ForkJoinPool.commonPool-worker-2] 插入數(shù)據(jù): com.example.transaction.model.Student@25e1950b
當(dāng)前線程:[ForkJoinPool.commonPool-worker-1] 插入數(shù)據(jù): com.example.transaction.model.Student@57e8ff9a
全部事務(wù)正常提交
true
數(shù)據(jù)庫(kù)中的數(shù)據(jù):
id | name |
---|---|
1 | John |
2 | tom |
3 | marry |
6 | tom |
7 | marry |
10 | tom |
11 | marry |
14.2 異常插入
package com.example.transaction; import com.example.transaction.mapper.StudentMapper; import com.example.transaction.model.Student; import com.example.transaction.service.MultiThreadingTransactionManagerThree; import com.example.transaction.service.MultiThreadingTransactionManagerTwo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.List; @SpringBootTest public class TransactionApplicationSevenTests { @Autowired private StudentMapper studentMapper; @Autowired private MultiThreadingTransactionManagerThree multiThreadingTransactionManagerThree; @Test void contextLoads() { List<Student> studentList = new ArrayList<>(); studentList.add(new Student("張三")); studentList.add(new Student("李四")); List<Runnable> runnableList = new ArrayList<>(); studentList.forEach(student -> runnableList.add(() -> { System.out.println("當(dāng)前線程:[" + Thread.currentThread().getName() + "] 插入數(shù)據(jù): " + student); try { studentMapper.insert(student); } catch (Exception e) { e.printStackTrace(); } })); runnableList.add(() -> System.out.println(1 / 0)); boolean isSuccess = multiThreadingTransactionManagerThree.execute(runnableList); System.out.println(isSuccess); } }
輸出日志:
子線程: [ForkJoinPool.commonPool-worker-2]
子線程: [ForkJoinPool.commonPool-worker-3]
子線程: [ForkJoinPool.commonPool-worker-1]
2023-11-26 19:19:01.862 INFO 15120 --- [onPool-worker-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-11-26 19:19:02.016 INFO 15120 --- [onPool-worker-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
當(dāng)前線程:[ForkJoinPool.commonPool-worker-1] 插入數(shù)據(jù): com.example.transaction.model.Student@3155d2ee
當(dāng)前線程:[ForkJoinPool.commonPool-worker-2] 插入數(shù)據(jù): com.example.transaction.model.Student@5ff9bde5
java.lang.ArithmeticException: / by zero
at com.example.transaction.TransactionApplicationSevenTests.lambda$contextLoads$2(TransactionApplicationSevenTests.java:37)
at com.example.transaction.service.MultiThreadingTransactionManagerThree.executeThread(MultiThreadingTransactionManagerThree.java:90)
at com.example.transaction.service.MultiThreadingTransactionManagerThree.lambda$null$1(MultiThreadingTransactionManagerThree.java:45)
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626)
at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1618)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
java.util.concurrent.ExecutionException: java.util.concurrent.CancellationException
at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
......
com.example.transaction.service.MultiThreadingTransactionManagerThree.lambda$null$1(MultiThreadingTransactionManagerThree.java:51)
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626)
at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1618)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
發(fā)生異常,全部事務(wù)回滾
false
數(shù)據(jù)庫(kù)中的數(shù)據(jù):
id | name |
---|---|
1 | John |
2 | tom |
3 | marry |
6 | tom |
7 | marry |
10 | tom |
11 | marry |
到此這篇關(guān)于Spring在多線程下保持事務(wù)的一致性的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring多線程事務(wù)一致性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié)
這篇文章主要介紹了Spring AOP proxyTargetClass的行為表現(xiàn)總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java+Swing實(shí)現(xiàn)五子棋游戲的示例代碼
本文將通過(guò)Java語(yǔ)言實(shí)現(xiàn)經(jīng)典游戲—五子棋游戲,文中采用了Swing制作游戲界面,具有開(kāi)始游戲,悔棋,認(rèn)輸,退出等功能。感興趣的可以跟隨小編一起動(dòng)手試一試2022-02-02SpringBoot自動(dòng)裝配Condition的實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot自動(dòng)裝配Condition的實(shí)現(xiàn)方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08SpringCloud Gateway 權(quán)限認(rèn)證的實(shí)現(xiàn)
Spring Cloud Gateway 作為網(wǎng)關(guān)層,承擔(dān)著請(qǐng)求轉(zhuǎn)發(fā)、權(quán)限校驗(yàn)等重要職責(zé),本文主要介紹了SpringCloud Gateway 權(quán)限認(rèn)證的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-04-04Springboot Tomcat APR模式詳解和實(shí)踐記錄
這篇文章主要介紹了Springboot Tomcat APR模式詳解和實(shí)踐記錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-04-04Java動(dòng)態(tài)調(diào)用類中方法代碼
這篇文章主要介紹了Java動(dòng)態(tài)調(diào)用類中方法代碼,需要的朋友可以參考下2014-02-02