Skip to content

Commit 537d73e

Browse files
committed
ref #376 - fixing error where sql2o might throw an exception if autoCommit is set to false on data source level
1 parent f9a9091 commit 537d73e

File tree

6 files changed

+63
-8
lines changed

6 files changed

+63
-8
lines changed

core/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@
6363
<version>${embedded-db-junit.version}</version>
6464
<scope>test</scope>
6565
</dependency>
66-
<dependency>
67-
<groupId>org.junit.jupiter</groupId>
68-
<artifactId>junit-jupiter</artifactId>
69-
<version>5.11.0</version>
70-
<scope>test</scope>
71-
</dependency>
7266

7367
<dependency>
7468
<groupId>org.mockito</groupId>

core/src/main/java/org/sql2o/Connection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ public Query createQueryWithParams(String queryText, Object... paramValues){
123123
.withParams(paramValues);
124124
}
125125

126+
public void prepareForTransaction(int isolationLevel) throws SQLException {
127+
if (!getJdbcConnection().getAutoCommit()) {
128+
getJdbcConnection().setAutoCommit(true); // force autocommit to true to allow for transaction isolation level to be set
129+
}
130+
getJdbcConnection().setTransactionIsolation(isolationLevel);
131+
getJdbcConnection().setAutoCommit(false);
132+
}
133+
126134
public Sql2o rollback(){
127135
return this.rollback(true).sql2o;
128136
}

core/src/main/java/org/sql2o/Sql2o.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ public Connection beginTransaction(ConnectionSource connectionSource, int isolat
296296

297297
boolean success = false;
298298
try {
299-
connection.getJdbcConnection().setAutoCommit(false);
300-
connection.getJdbcConnection().setTransactionIsolation(isolationLevel);
299+
connection.prepareForTransaction(isolationLevel);
301300
success = true;
302301
} catch (SQLException e) {
303302
throw new Sql2oException("Could not start the transaction - " + e.getMessage(), e);

extensions/postgres/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
<version>42.7.2</version>
3737
<optional>true</optional>
3838
</dependency>
39+
40+
<dependency>
41+
<groupId>com.zaxxer</groupId>
42+
<artifactId>HikariCP</artifactId>
43+
<version>6.2.1</version>
44+
<scope>test</scope>
45+
</dependency>
3946
</dependencies>
4047

4148
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.sql2o.extensions.postgres;
2+
3+
import com.zaxxer.hikari.HikariConfig;
4+
import com.zaxxer.hikari.HikariDataSource;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.sql2o.Sql2o;
8+
9+
public class DataSourceTest {
10+
11+
private Sql2o sql2o;
12+
13+
@BeforeEach
14+
void setUp() {
15+
var dsConfig = new HikariConfig();
16+
dsConfig.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
17+
dsConfig.addDataSourceProperty("serverName", "localhost");
18+
dsConfig.addDataSourceProperty("portNumber", 15432);
19+
dsConfig.addDataSourceProperty("databaseName", "postgres");
20+
dsConfig.addDataSourceProperty("user", "testuser");
21+
dsConfig.addDataSourceProperty("password", "testpassword");
22+
dsConfig.setMaximumPoolSize(5);
23+
dsConfig.setSchema("public");
24+
25+
// this is important to test, as it make the data source automatically open a connection when a connection is
26+
// created. This might break some functionality of sql2o.
27+
dsConfig.setAutoCommit(false);
28+
dsConfig.setReadOnly(false);
29+
30+
var ds = new HikariDataSource(dsConfig);
31+
sql2o = new Sql2o(ds);
32+
}
33+
34+
@Test
35+
void testQueryUsingHikariDataSource() {
36+
try (var con = sql2o.beginTransaction()) {
37+
var result = con.createQuery("SELECT 1").executeScalar(Integer.class);
38+
System.out.println(result);
39+
}
40+
}
41+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
<version>${junit.version}</version>
109109
<scope>test</scope>
110110
</dependency>
111+
<dependency>
112+
<groupId>org.junit.jupiter</groupId>
113+
<artifactId>junit-jupiter</artifactId>
114+
<version>5.11.4</version>
115+
<scope>test</scope>
116+
</dependency>
111117

112118
<!-- all projects use slf4j-simple when testing -->
113119
<dependency>

0 commit comments

Comments
 (0)