1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.spring.transaction;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19
20 import org.junit.jupiter.api.Test;
21 import org.mybatis.spring.AbstractMyBatisSpringTest;
22 import org.springframework.transaction.support.DefaultTransactionDefinition;
23
24 class SpringTransactionManagerTest extends AbstractMyBatisSpringTest {
25
26 @Test
27 void shouldNoOpWithTx() throws Exception {
28 var txDef = new DefaultTransactionDefinition();
29 txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED");
30 var status = txManager.getTransaction(txDef);
31
32 var transactionFactory = new SpringManagedTransactionFactory();
33 var transaction = (SpringManagedTransaction) transactionFactory.newTransaction(dataSource, null, false);
34 transaction.getConnection();
35 transaction.commit();
36 transaction.close();
37 assertThat(connection.getNumberCommits()).as("should not call commit on Connection").isEqualTo(0);
38 assertThat(connection.isClosed()).as("should not close the Connection").isFalse();
39
40 txManager.commit(status);
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 @Test
63 void shouldManageWithNoTx() throws Exception {
64 var transactionFactory = new SpringManagedTransactionFactory();
65 var transaction = (SpringManagedTransaction) transactionFactory.newTransaction(dataSource, null, false);
66 transaction.getConnection();
67 transaction.commit();
68 transaction.close();
69 assertThat(connection.getNumberCommits()).as("should call commit on Connection").isEqualTo(1);
70 assertThat(connection.isClosed()).as("should close the Connection").isTrue();
71 }
72
73 @Test
74 void shouldNotCommitWithNoTxAndAutocommitIsOn() throws Exception {
75 var transactionFactory = new SpringManagedTransactionFactory();
76 var transaction = (SpringManagedTransaction) transactionFactory.newTransaction(dataSource, null, false);
77 connection.setAutoCommit(true);
78 transaction.getConnection();
79 transaction.commit();
80 transaction.close();
81 assertThat(connection.getNumberCommits()).as("should not call commit on a Connection with autocommit").isEqualTo(0);
82 assertThat(connection.isClosed()).as("should close the Connection").isTrue();
83 }
84
85 @Test
86 void shouldIgnoreAutocommit() throws Exception {
87 var transactionFactory = new SpringManagedTransactionFactory();
88 var transaction = (SpringManagedTransaction) transactionFactory.newTransaction(dataSource, null, true);
89 transaction.getConnection();
90 transaction.commit();
91 transaction.close();
92 assertThat(connection.getNumberCommits()).as("should call commit on Connection").isEqualTo(1);
93 assertThat(connection.isClosed()).as("should close the Connection").isTrue();
94 }
95
96 }