View Javadoc
1   /*
2    *    Copyright 2016-2026 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              try (InputStream is = NameService.class.getResourceAsStream("/issues/gh324/CreateDB.sql")) {
43                  assert is != null;
44                  try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "");
45                       InputStreamReader isr = new InputStreamReader(is)) {
46                      ScriptRunner sr = new ScriptRunner(connection);
47                      sr.setLogWriter(null);
48                      sr.runScript(isr);
49                  }
50              }
51  
52              UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
53              Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
54              Configuration config = new Configuration(environment);
55              config.addMapper(NameTableMapper.class);
56              sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
57          } catch (Exception e) {
58              throw new RuntimeException(e);
59          }
60      }
61  
62      public void insertRecord() {
63          try (SqlSession session = sqlSessionFactory.openSession(true)) {
64              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
65              NameRecord row = new NameRecord(1, "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(1, "Barney");
74              mapper.updateByPrimaryKey(row);
75          }
76      }
77  
78      public void updateRecordWithoutAutoCommitAndNoExplicitCommit() {
79          // this should rollback
80          try (SqlSession session = sqlSessionFactory.openSession()) {
81              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
82              NameRecord row = new NameRecord(1, "Barney");
83              mapper.updateByPrimaryKey(row);
84          }
85      }
86  
87      public void updateRecordWithoutAutoCommitAndExplicitCommit() {
88          try (SqlSession session = sqlSessionFactory.openSession()) {
89              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
90              NameRecord row = new NameRecord(1, "Barney");
91              mapper.updateByPrimaryKey(row);
92              session.commit();
93          }
94      }
95  
96      public Optional<NameRecord> getRecord() {
97          try (SqlSession session = sqlSessionFactory.openSession()) {
98              NameTableMapper mapper = session.getMapper(NameTableMapper.class);
99              return mapper.selectByPrimaryKey(1);
100         }
101     }
102 
103     public void resetDatabase() {
104         try (SqlSession session = sqlSessionFactory.openSession()) {
105             NameTableMapper mapper = session.getMapper(NameTableMapper.class);
106             mapper.deleteAll();
107         }
108     }
109 }