diff --git a/core/pom.xml b/core/pom.xml index 06e87169..46c2e69b 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -63,12 +63,6 @@ ${embedded-db-junit.version} test - - org.junit.jupiter - junit-jupiter - 5.11.0 - test - org.mockito diff --git a/core/src/main/java/org/sql2o/Connection.java b/core/src/main/java/org/sql2o/Connection.java index 6c96afb1..412302ed 100644 --- a/core/src/main/java/org/sql2o/Connection.java +++ b/core/src/main/java/org/sql2o/Connection.java @@ -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; } diff --git a/core/src/main/java/org/sql2o/Sql2o.java b/core/src/main/java/org/sql2o/Sql2o.java index b0004276..cea06e6b 100644 --- a/core/src/main/java/org/sql2o/Sql2o.java +++ b/core/src/main/java/org/sql2o/Sql2o.java @@ -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); diff --git a/extensions/postgres/pom.xml b/extensions/postgres/pom.xml index c230dc97..3def001a 100644 --- a/extensions/postgres/pom.xml +++ b/extensions/postgres/pom.xml @@ -36,6 +36,13 @@ 42.7.2 true + + + com.zaxxer + HikariCP + 6.2.1 + test + diff --git a/extensions/postgres/src/test/java/org/sql2o/extensions/postgres/DataSourceTest.java b/extensions/postgres/src/test/java/org/sql2o/extensions/postgres/DataSourceTest.java new file mode 100644 index 00000000..6f95e5e7 --- /dev/null +++ b/extensions/postgres/src/test/java/org/sql2o/extensions/postgres/DataSourceTest.java @@ -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); + } + } +} diff --git a/pom.xml b/pom.xml index 2232bf57..bcff9beb 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,12 @@ ${junit.version} test + + org.junit.jupiter + junit-jupiter + 5.11.4 + test +