Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19c2c43

Browse files
committed
add transaction propagation required in spring sample
1 parent 29b86c4 commit 19c2c43

File tree

7 files changed

+215
-14
lines changed

7 files changed

+215
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ipman.springboot.transactional.sample.core.service;
2+
3+
import com.ipman.springboot.transactional.sample.common.domain.model.User1;
4+
5+
/**
6+
* Created by ipipman on 2021/4/2.
7+
*
8+
* @version V1.0
9+
* @Package com.ipman.springboot.transactional.sample.core.service
10+
* @Description: (用一句话描述该文件做什么)
11+
* @date 2021/4/2 11:49 上午
12+
*/
13+
public interface User1Service {
14+
15+
/**
16+
* Propagation.REQUIRED
17+
* 如果当前没有事务,就创建一个事务
18+
* 如果已经存在一个事务中,加入这个事务中
19+
*
20+
*/
21+
void addRequired(User1 user1);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ipman.springboot.transactional.sample.core.service;
2+
3+
import com.ipman.springboot.transactional.sample.common.domain.model.User2;
4+
5+
/**
6+
* Created by ipipman on 2021/4/2.
7+
*
8+
* @version V1.0
9+
* @Package com.ipman.springboot.transactional.sample.core.service
10+
* @Description: (用一句话描述该文件做什么)
11+
* @date 2021/4/2 11:50 上午
12+
*/
13+
public interface User2Service {
14+
15+
16+
/**
17+
* Propagation.REQUIRED
18+
* 如果当前没有事务,就创建一个事务
19+
* 如果已经存在一个事务中,加入这个事务中
20+
*/
21+
void addRequired(User2 user2);
22+
23+
/**
24+
* Propagation.REQUIRED
25+
* 如果当前没有事务,就创建一个事务
26+
* 如果已经存在一个事务中,加入这个事务中
27+
*/
28+
void addRequiredException(User2 user2);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.ipman.springboot.transactional.sample.core.service.impl;
2+
3+
import com.ipman.springboot.transactional.sample.common.domain.model.User1;
4+
import com.ipman.springboot.transactional.sample.core.dao.User1Mapper;
5+
import com.ipman.springboot.transactional.sample.core.service.User1Service;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.annotation.Propagation;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import javax.annotation.Resource;
11+
12+
/**
13+
* Created by ipipman on 2021/4/2.
14+
*
15+
* @version V1.0
16+
* @Package com.ipman.springboot.transactional.sample.core.service.impl
17+
* @Description: (用一句话描述该文件做什么)
18+
* @date 2021/4/2 11:49 上午
19+
*/
20+
@Service
21+
public class User1ServiceImpl implements User1Service {
22+
23+
@Resource
24+
private User1Mapper user1Mapper;
25+
26+
/**
27+
* Propagation.REQUIRED
28+
* 如果当前没有事务,就创建一个事务
29+
* 如果已经存在一个事务中,加入这个事务中
30+
*/
31+
@Override
32+
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
33+
public void addRequired(User1 user1) {
34+
user1Mapper.insert(user1);
35+
}
36+
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.ipman.springboot.transactional.sample.core.service.impl;
2+
3+
import com.ipman.springboot.transactional.sample.common.domain.model.User2;
4+
import com.ipman.springboot.transactional.sample.core.dao.User2Mapper;
5+
import com.ipman.springboot.transactional.sample.core.service.User2Service;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.annotation.Propagation;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import javax.annotation.Resource;
11+
12+
/**
13+
* Created by ipipman on 2021/4/2.
14+
*
15+
* @version V1.0
16+
* @Package com.ipman.springboot.transactional.sample.core.service.impl
17+
* @Description: (用一句话描述该文件做什么)
18+
* @date 2021/4/2 11:50 上午
19+
*/
20+
@Service
21+
public class User2ServiceImpl implements User2Service {
22+
23+
@Resource
24+
private User2Mapper user2Mapper;
25+
26+
/**
27+
* Propagation.REQUIRED
28+
* 如果当前没有事务,就创建一个事务
29+
* 如果已经存在一个事务中,加入这个事务中
30+
*/
31+
@Override
32+
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
33+
public void addRequired(User2 user2){
34+
user2Mapper.insert(user2);
35+
}
36+
37+
/**
38+
* Propagation.REQUIRED
39+
* 如果当前没有事务,就创建一个事务
40+
* 如果已经存在一个事务中,加入这个事务中
41+
*/
42+
@Override
43+
@Transactional(value = "transactionManager", propagation = Propagation.REQUIRED)
44+
public void addRequiredException(User2 user2){
45+
user2Mapper.insert(user2);
46+
throw new RuntimeException();
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
1+
#datasource
2+
datasource.type= com.alibaba.druid.pool.DruidDataSource
3+
datasource.driver-class-name= com.mysql.jdbc.Driver
4+
datasource.url= jdbc:mysql://127.0.0.1:3306/stx
5+
datasource.username= root
6+
datasource.initialSize= 5
7+
datasource.minIdle= 5
8+
datasource.maxActive= 30
9+
datasource.maxWait= 60000
10+
datasource.timeBetweenEvictionRunsMillis= 60000
11+
datasource.minEvictableIdleTimeMillis= 30000
12+
datasource.validationQuery= select 'x'
13+
datasource.testWhileIdle= true
14+
datasource.testOnBorrow= true
15+
datasource.testOnReturn= false
16+
datasource.poolPreparedStatements= false
17+
datasource.maxPoolPreparedStatementPerConnectionSize= 20

springboot-transactional-sample/src/test/java/com/ipman/springboot/transactional/sample/SpringbootTransactionalSampleApplicationTests.java

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.ipman.springboot.transactional.sample;
2+
3+
import com.ipman.springboot.transactional.sample.common.domain.model.User1;
4+
import com.ipman.springboot.transactional.sample.common.domain.model.User2;
5+
import com.ipman.springboot.transactional.sample.core.service.User1Service;
6+
import com.ipman.springboot.transactional.sample.core.service.User2Service;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
11+
@SpringBootTest
12+
class TransactionPropagationTest {
13+
14+
@Autowired
15+
private User1Service user1Service;
16+
17+
@Autowired
18+
private User2Service user2Service;
19+
20+
/**
21+
* Propagation.REQUIRED
22+
* 如果当前没有事务,就创建一个事务
23+
* 如果已经存在一个事务中,加入这个事务中
24+
* 总结:
25+
* 外围方法没有开启事务,插入"张三"、"李四"方法在自己事务中独立运行
26+
* 外围方法异常不影响内部插入"张三"、"李四"方法独立的事务
27+
*/
28+
@Test
29+
public void transactionPropagationRequired() {
30+
User1 user1 = new User1();
31+
user1.setName("张三");
32+
user1Service.addRequired(user1);
33+
34+
User2 user2 = new User2();
35+
user2.setName("李四");
36+
user2Service.addRequired(user2);
37+
38+
throw new RuntimeException();
39+
}
40+
41+
/**
42+
* Propagation.REQUIRED
43+
* 如果当前没有事务,就创建一个事务
44+
* 如果已经存在一个事务中,加入这个事务中
45+
* 总结:
46+
* 外围方法没有开启事务,插入"张三"、"李四"方法在自己事务中独立运行
47+
* 所以插入"李四"方法抛出异常只会回滚插入"李四"的方法,插入"张三"的方法不受影响
48+
*/
49+
@Test
50+
public void transactionPropagationRequiredException() {
51+
User1 user1 = new User1();
52+
user1.setName("张三");
53+
user1Service.addRequired(user1);
54+
55+
User2 user2 = new User2();
56+
user2.setName("李四");
57+
user2Service.addRequiredException(user2);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)