View Javadoc
1   /*
2    * Copyright 2010-2024 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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    // TODO Test does not compile
44    // @Disabled
45    // @Test
46    // public void shouldManageWithOtherDatasource() throws Exception {
47    // DefaultTransactionDefinition txDef = new DefaultTransactionDefinition();
48    // txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED");
49    // TransactionStatus status = txManager.getTransaction(txDef);
50    //
51    // SpringManagedTransactionFactory transactionFactory = new SpringManagedTransactionFactory(new MockDataSource());
52    // SpringManagedTransaction transaction = (SpringManagedTransaction) transactionFactory.newTransaction(connection,
53    // false);
54    // transaction.commit();
55    // transaction.close();
56    // assertEquals(1, connection.getNumberCommits(), "should call commit on Connection");
57    // assertTrue(connection.isClosed(), "should close the Connection");
58    //
59    // txManager.commit(status);
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  }