Переглянути джерело

test事务传播,线程安全

yegang 2 роки тому
батько
коміт
465ebbd3a5

+ 15 - 0
src/main/java/com/sw/dao/SysTest2Dao.java

@@ -0,0 +1,15 @@
+package com.sw.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.sw.domain.SysTest2;
+
+/**
+ * (SysTest2)表数据库访问层
+ *
+ * @author yegang
+ * @since 2022-02-25 15:52:47
+ */
+public interface SysTest2Dao extends BaseMapper<SysTest2> {
+
+
+}

+ 28 - 0
src/main/java/com/sw/domain/SysTest2.java

@@ -0,0 +1,28 @@
+package com.sw.domain;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * (SysTest2)实体类
+ *
+ * @author yegang
+ * @since 2022-02-25 15:52:46
+ */
+@Data
+@TableName("sys_test2")
+public class SysTest2 implements Serializable {
+    private static final long serialVersionUID = 809663407382752105L;
+    /**
+     * 主键
+     */
+    private Integer id;
+    /**
+     * 名称
+     */
+    private String name;
+
+
+}

+ 19 - 0
src/main/java/com/sw/service/SysTest2Service.java

@@ -0,0 +1,19 @@
+package com.sw.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.sw.domain.SysTest2;
+
+/**
+ * (SysTest2)表服务接口
+ *
+ * @author yegang
+ * @since 2022-02-25 15:52:47
+ */
+public interface SysTest2Service extends IService<SysTest2> {
+    public void addTest20(SysTest2 sysTest2);
+    public void addTest21(SysTest2 sysTest2) throws Exception;
+    public void addTest22(SysTest2 sysTest2);
+    public void addTest23(SysTest2 sysTest2) throws Exception;
+    public void addTest24(SysTest2 sysTest2);
+    public void addTest25(SysTest2 sysTest2) throws Exception;
+}

+ 7 - 2
src/main/java/com/sw/service/SysTestService.java

@@ -2,6 +2,7 @@ package com.sw.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.sw.domain.SysTest;
+import com.sw.domain.SysTest2;
 
 /**
  * (SysTest)表服务接口
@@ -10,6 +11,10 @@ import com.sw.domain.SysTest;
  * @since 2022-02-17 16:35:26
  */
 public interface SysTestService extends IService<SysTest> {
-
-
+    public void addTest10(SysTest sysTest,SysTest2 sysTest2) throws Exception;
+    public void addTest11(SysTest sysTest,SysTest2 sysTest2) throws Exception;
+    public void addTest12(SysTest sysTest,SysTest2 sysTest2) throws Exception;
+    public void addTest13(SysTest sysTest,SysTest2 sysTest2) throws Exception;
+    public void addTest14(SysTest sysTest,SysTest2 sysTest2) throws Exception;
+    public void addTest15(SysTest sysTest,SysTest2 sysTest2) throws Exception;
 }

+ 64 - 0
src/main/java/com/sw/service/impl/SysTest2ServiceImpl.java

@@ -0,0 +1,64 @@
+package com.sw.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.sw.dao.SysTest2Dao;
+import com.sw.domain.SysTest2;
+import com.sw.service.SysTest2Service;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+
+/**
+ * (SysTest2)表服务实现类
+ *
+ * @author yegang
+ * @since 2022-02-25 15:52:47
+ */
+@Service("sysTest2Service")
+public class SysTest2ServiceImpl extends ServiceImpl<SysTest2Dao, SysTest2> implements SysTest2Service {
+    @Resource
+    private SysTest2Dao sysTest2Dao;
+
+
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest20(SysTest2 sysTest2) {
+         sysTest2Dao.insert(sysTest2);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest21(SysTest2 sysTest2) throws Exception {
+         sysTest2Dao.insert(sysTest2);
+         throw new Exception();
+    }
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
+    public void addTest22(SysTest2 sysTest2) {
+         sysTest2Dao.insert(sysTest2);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
+    public void addTest23(SysTest2 sysTest2) throws Exception {
+         sysTest2Dao.insert(sysTest2);
+        throw new Exception();
+
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NESTED)
+    public void addTest24(SysTest2 sysTest2) {
+        sysTest2Dao.insert(sysTest2);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NESTED)
+    public void addTest25(SysTest2 sysTest2) throws Exception {
+        sysTest2Dao.insert(sysTest2);
+        throw new Exception();
+
+    }
+}

+ 45 - 1
src/main/java/com/sw/service/impl/SysTestServiceImpl.java

@@ -3,8 +3,12 @@ package com.sw.service.impl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.sw.dao.SysTestDao;
 import com.sw.domain.SysTest;
+import com.sw.domain.SysTest2;
+import com.sw.service.SysTest2Service;
 import com.sw.service.SysTestService;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 
@@ -18,6 +22,46 @@ import javax.annotation.Resource;
 public class SysTestServiceImpl extends ServiceImpl<SysTestDao, SysTest> implements SysTestService {
     @Resource
     private SysTestDao sysTestDao;
+    @Resource
+    private SysTest2Service sysTest2Service;
 
-
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest10(SysTest sysTest,SysTest2 sysTest2) throws Exception {
+        sysTestDao.insert(sysTest);
+        sysTest2Service.addTest21(sysTest2);
+    }
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest11(SysTest sysTest,SysTest2 sysTest2) throws Exception {
+        sysTestDao.insert(sysTest);
+        sysTest2Service.addTest20(sysTest2);
+        throw  new Exception();
+    }
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest12(SysTest sysTest,SysTest2 sysTest2) throws Exception {
+        sysTestDao.insert(sysTest);
+        sysTest2Service.addTest22(sysTest2);
+        throw  new Exception();
+    }
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest13(SysTest sysTest,SysTest2 sysTest2) throws Exception {
+        sysTestDao.insert(sysTest);
+        sysTest2Service.addTest23(sysTest2);
+    }
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest14(SysTest sysTest,SysTest2 sysTest2) throws Exception {
+        sysTestDao.insert(sysTest);
+        sysTest2Service.addTest24(sysTest2);
+        throw  new Exception();
+    }
+    @Override
+    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
+    public void addTest15(SysTest sysTest,SysTest2 sysTest2) throws Exception {
+        sysTestDao.insert(sysTest);
+        sysTest2Service.addTest25(sysTest2);
+    }
 }

+ 6 - 0
src/main/resources/mapper/SysTest2Mapper.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sw.dao.SysTest2Dao">
+
+
+</mapper>

+ 59 - 0
src/test/java/com/sw/propagation/ConTest.java

@@ -0,0 +1,59 @@
+package com.sw.propagation;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * 并发测试
+ *
+ * @author yegang
+ * @create 2022-02-28 9:51
+ **/
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+@WebAppConfiguration
+public class ConTest {
+    @Test
+    public void sync() {
+        Object o = new Object();
+        new Thread(() -> {
+            synchronized (o) {
+                System.out.println("异常前");
+                int a = 1 / 0;
+                System.out.println("异常后");
+            }
+        }).start();
+        new Thread(() -> {
+            synchronized (o) {
+                System.out.println("异常前");
+                int a = 1 / 0;
+                System.out.println("异常后");
+            }
+        }).start();
+    }
+
+    @Test
+    public void Lock() {
+        ReentrantLock reentrantLock = new ReentrantLock();
+        new Thread(() -> {
+            reentrantLock.lock();
+            System.out.println("异常前");
+            int a = 1 / 0;
+            System.out.println("异常后");
+            reentrantLock.unlock();
+        }).start();
+        new Thread(() -> {
+            reentrantLock.lock();
+            System.out.println("异常前");
+            int a = 1 / 0;
+            System.out.println("异常后");
+            reentrantLock.unlock();
+        }).start();
+
+    }
+}

+ 44 - 0
src/test/java/com/sw/propagation/NestedTest.java

@@ -0,0 +1,44 @@
+package com.sw.propagation;
+
+import com.sw.domain.SysTest;
+import com.sw.domain.SysTest2;
+import com.sw.service.SysTestService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+/**
+ * @author yegang
+ * @create 2022-02-25 15:57
+ **/
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+@WebAppConfiguration
+public class NestedTest {
+    @Autowired
+    SysTestService sysTestService;
+    @Test
+    public void  test1() throws Exception {
+        SysTest sysTest = new SysTest();
+        sysTest.setName("张1");
+
+        SysTest2 sysTest2 = new SysTest2();
+        sysTest2.setName("张2");
+
+        sysTestService.addTest14(sysTest,sysTest2);
+    }
+    @Test
+    public void  test2() throws Exception {
+        SysTest sysTest = new SysTest();
+        sysTest.setName("张1");
+
+        SysTest2 sysTest2 = new SysTest2();
+        sysTest2.setName("张2");
+
+        sysTestService.addTest15(sysTest,sysTest2);
+    }
+
+}

+ 44 - 0
src/test/java/com/sw/propagation/NewsTest.java

@@ -0,0 +1,44 @@
+package com.sw.propagation;
+
+import com.sw.domain.SysTest;
+import com.sw.domain.SysTest2;
+import com.sw.service.SysTestService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+/**
+ * @author yegang
+ * @create 2022-02-25 15:57
+ **/
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+@WebAppConfiguration
+public class NewsTest {
+    @Autowired
+    SysTestService sysTestService;
+    @Test
+    public void  test1() throws Exception {
+        SysTest sysTest = new SysTest();
+        sysTest.setName("张1");
+
+        SysTest2 sysTest2 = new SysTest2();
+        sysTest2.setName("张2");
+
+        sysTestService.addTest12(sysTest,sysTest2);
+    }
+    @Test
+    public void  test2() throws Exception {
+        SysTest sysTest = new SysTest();
+        sysTest.setName("张1");
+
+        SysTest2 sysTest2 = new SysTest2();
+        sysTest2.setName("张2");
+
+        sysTestService.addTest13(sysTest,sysTest2);
+    }
+
+}

+ 43 - 0
src/test/java/com/sw/propagation/RequeireTest.java

@@ -0,0 +1,43 @@
+package com.sw.propagation;
+
+import com.sw.domain.SysTest;
+import com.sw.domain.SysTest2;
+import com.sw.service.SysTestService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+/**
+ * @author yegang
+ * @create 2022-02-25 15:57
+ **/
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+@WebAppConfiguration
+public class RequeireTest {
+    @Autowired
+    SysTestService sysTestService;
+    @Test
+    public void  test1() throws Exception {
+        SysTest sysTest = new SysTest();
+        sysTest.setName("张1");
+
+        SysTest2 sysTest2 = new SysTest2();
+        sysTest2.setName("张2");
+
+        sysTestService.addTest12(sysTest,sysTest2);
+    }
+    @Test
+    public void  test2() throws Exception {
+        SysTest sysTest = new SysTest();
+        sysTest.setName("张1");
+
+        SysTest2 sysTest2 = new SysTest2();
+        sysTest2.setName("张2");
+
+        sysTestService.addTest13(sysTest,sysTest2);
+    }
+}