Skip to content

Commit

Permalink
ref #376 - fixing error where sql2o might throw an exception if autoC…
Browse files Browse the repository at this point in the history
…ommit is set to false on data source level
  • Loading branch information
aaberg committed Jan 1, 2025
1 parent f9a9091 commit 537d73e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
6 changes: 0 additions & 6 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@
<version>${embedded-db-junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/sql2o/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public Query createQueryWithParams(String queryText, Object... paramValues){
.withParams(paramValues);
}

public void prepareForTransaction(int isolationLevel) throws SQLException {
if (!getJdbcConnection().getAutoCommit()) {
getJdbcConnection().setAutoCommit(true); // force autocommit to true to allow for transaction isolation level to be set
}
getJdbcConnection().setTransactionIsolation(isolationLevel);
getJdbcConnection().setAutoCommit(false);
}

public Sql2o rollback(){
return this.rollback(true).sql2o;
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/sql2o/Sql2o.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ public Connection beginTransaction(ConnectionSource connectionSource, int isolat

boolean success = false;
try {
connection.getJdbcConnection().setAutoCommit(false);
connection.getJdbcConnection().setTransactionIsolation(isolationLevel);
connection.prepareForTransaction(isolationLevel);
success = true;
} catch (SQLException e) {
throw new Sql2oException("Could not start the transaction - " + e.getMessage(), e);
Expand Down
7 changes: 7 additions & 0 deletions extensions/postgres/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
<version>42.7.2</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>6.2.1</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.sql2o.extensions.postgres;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sql2o.Sql2o;

public class DataSourceTest {

private Sql2o sql2o;

@BeforeEach
void setUp() {
var dsConfig = new HikariConfig();
dsConfig.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
dsConfig.addDataSourceProperty("serverName", "localhost");
dsConfig.addDataSourceProperty("portNumber", 15432);
dsConfig.addDataSourceProperty("databaseName", "postgres");
dsConfig.addDataSourceProperty("user", "testuser");
dsConfig.addDataSourceProperty("password", "testpassword");
dsConfig.setMaximumPoolSize(5);
dsConfig.setSchema("public");

// this is important to test, as it make the data source automatically open a connection when a connection is
// created. This might break some functionality of sql2o.
dsConfig.setAutoCommit(false);
dsConfig.setReadOnly(false);

var ds = new HikariDataSource(dsConfig);
sql2o = new Sql2o(ds);
}

@Test
void testQueryUsingHikariDataSource() {
try (var con = sql2o.beginTransaction()) {
var result = con.createQuery("SELECT 1").executeScalar(Integer.class);
System.out.println(result);
}
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>

<!-- all projects use slf4j-simple when testing -->
<dependency>
Expand Down

0 comments on commit 537d73e

Please sign in to comment.