View Javadoc
1   /*
2    *    Copyright 2016-2025 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 issues.gh324;
17  
18  import java.io.InputStream;
19  import java.io.InputStreamReader;
20  import java.sql.Connection;
21  import java.sql.DriverManager;
22  import java.util.Optional;
23  
24  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
25  import org.apache.ibatis.jdbc.ScriptRunner;
26  import org.apache.ibatis.mapping.Environment;
27  import org.apache.ibatis.session.Configuration;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
32  
33  public class NameService {
34      private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
35      private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
36  
37      private final SqlSessionFactory sqlSessionFactory;
38  
39      public NameService() {
40          try {
41              Class.forName(JDBC_DRIVER);
42              InputStream is = getClass().getResourceAsStream("/issues/gh324/CreateDB.sql");
43              assert is != null;
44              try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
45                  ScriptRunner sr = new ScriptRunner(connection);
46                  sr.setLogWriter(null);
47                  sr.runScript(new InputStreamReader(is));
48              }
49  
50              UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
51              Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
52              Configuration config = new Configuration(environment);
53              config.addMapper(NameTableMapper.class);
54              sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
55          } catch (Exception e) {
56              throw new RuntimeException(e);
57          }
58      }
59  
60      public void insertRecord() {
61          try (SqlSession session = sqlSessionFactory.openSession(true)) {
62              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
63              NameRecord row = new NameRecord();
64              row.setId(1);
65              row.setName("Fred");
66              mapper.insert(row);
67          }
68      }
69  
70      public void updateRecordWithAutoCommit() {
71          try (SqlSession session = sqlSessionFactory.openSession(true)) {
72              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
73              NameRecord row = new NameRecord();
74              row.setId(1);
75              row.setName("Barney");
76              mapper.updateByPrimaryKey(row);
77          }
78      }
79  
80      public void updateRecordWithoutAutoCommitAndNoExplicitCommit() {
81          // this should rollback
82          try (SqlSession session = sqlSessionFactory.openSession()) {
83              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
84              NameRecord row = new NameRecord();
85              row.setId(1);
86              row.setName("Barney");
87              mapper.updateByPrimaryKey(row);
88          }
89      }
90  
91      public void updateRecordWithoutAutoCommitAndExplicitCommit() {
92          try (SqlSession session = sqlSessionFactory.openSession()) {
93              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
94              NameRecord row = new NameRecord();
95              row.setId(1);
96              row.setName("Barney");
97              mapper.updateByPrimaryKey(row);
98              session.commit();
99          }
100     }
101 
102     public Optional<NameRecord> getRecord() {
103         try (SqlSession session = sqlSessionFactory.openSession()) {
104             NameTableMapper mapper = session.getMapper(NameTableMapper.class);
105             return mapper.selectByPrimaryKey(1);
106         }
107     }
108 
109     public void resetDatabase() {
110         try (SqlSession session = sqlSessionFactory.openSession()) {
111             NameTableMapper mapper = session.getMapper(NameTableMapper.class);
112             mapper.deleteAll();
113         }
114     }
115 }