-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed datasource autocommit handling without UserTransaction (#21)
- Loading branch information
1 parent
d4f1a66
commit 0f25ae9
Showing
7 changed files
with
179 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.zaxxer.sansorm; | ||
|
||
import org.h2.jdbcx.JdbcDataSource; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.sansorm.TestUtils; | ||
|
||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
@RunWith(Parameterized.class) | ||
public class SqlClosureTest { | ||
@Parameterized.Parameters(name = "autocommit={0}, ut={1}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][] { | ||
{ true, true }, { true, false }, { false, true }, { false, false } | ||
}); | ||
} | ||
|
||
@Parameterized.Parameter(0) | ||
public boolean withAutoCommit; | ||
|
||
@Parameterized.Parameter(1) | ||
public boolean withUserTx; | ||
|
||
@Before // not @BeforeClass to have fresh table in each test, also sde | ||
public void setUp() throws IOException { | ||
final JdbcDataSource dataSource = TestUtils.makeH2DataSource(/*autoCommit=*/withAutoCommit); | ||
if (withUserTx) { | ||
SansOrm.initializeTxSimple(dataSource); | ||
} else { | ||
SansOrm.initializeTxNone(dataSource); | ||
} | ||
SqlClosureElf.executeUpdate("CREATE TABLE tx_test (string VARCHAR(128))"); | ||
} | ||
|
||
@After // not @AfterClass to have fresh table in each test | ||
public void tearDown() { | ||
SqlClosureElf.executeUpdate("DROP TABLE tx_test"); | ||
SansOrm.deinitialize(); | ||
} | ||
|
||
@Test | ||
public void shouldSupportNestedCalls() { | ||
final Set<String> insertedValues = SqlClosure.sqlExecute(c -> { | ||
SqlClosureElf.executeUpdate(c, "INSERT INTO tx_test VALUES (?)", "1"); | ||
|
||
// here goes nested SqlClosure | ||
SqlClosure.sqlExecute(cNested -> SqlClosureElf.executeUpdate(cNested, "INSERT INTO tx_test VALUES (?)", "2")); | ||
|
||
return getStrings(c); | ||
}); | ||
assertThat(insertedValues).containsOnly("1", "2"); | ||
} | ||
|
||
@Test | ||
public void shouldRollbackHighestTx() { | ||
assertThatThrownBy(() -> SqlClosure.sqlExecute(c -> { | ||
SqlClosureElf.executeUpdate(c, "INSERT INTO tx_test VALUES (?)", "3"); | ||
|
||
// here goes nested SqlClosure | ||
SqlClosure.sqlExecute(cNested -> { | ||
SqlClosureElf.executeUpdate(cNested, "INSERT INTO tx_test VALUES (?)", "4"); | ||
throw new RuntimeException("boom!"); | ||
}); | ||
|
||
return SqlClosureElf.executeUpdate(c, "INSERT INTO tx_test VALUES (?)", "5"); | ||
})).isInstanceOf(RuntimeException.class).hasMessage("boom!"); | ||
|
||
final Set<String> insertedValues = SqlClosure.sqlExecute(SqlClosureTest::getStrings); | ||
assertThat(insertedValues).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void shouldRollbackNestedClosuresWithUserTransaction() { | ||
assertThatThrownBy(() -> SqlClosure.sqlExecute(c -> { | ||
SqlClosureElf.executeUpdate(c, "INSERT INTO tx_test VALUES (?)", "6"); | ||
|
||
// here goes nested SqlClosure | ||
SqlClosure.sqlExecute(cNested -> SqlClosureElf.executeUpdate(cNested, "INSERT INTO tx_test VALUES (?)", "7")); | ||
|
||
SqlClosureElf.executeUpdate(c, "INSERT INTO tx_test VALUES (?)", "8"); | ||
throw new Error("boom!"); // ie something not or type SQLException or RuntimeException | ||
})).isInstanceOf(Error.class).hasMessage("boom!"); | ||
|
||
final Set<String> insertedValues = SqlClosure.sqlExecute(SqlClosureTest::getStrings); | ||
if (withUserTx) { | ||
assertThat(insertedValues).containsOnly().as("With UserTransaction nested closures share same tx scope"); | ||
} else { | ||
assertThat(insertedValues).containsOnly("7").as("Without UserTransaction every closure defines it's own tx scope"); | ||
} | ||
} | ||
|
||
static Set<String> getStrings(Connection c) throws SQLException { | ||
ResultSet rs = SqlClosureElf.executeQuery(c, "SELECT string FROM tx_test;"); | ||
Set<String> result = new HashSet<>(); | ||
while (rs.next()) { | ||
result.add(rs.getString(1)); | ||
} | ||
return result; | ||
} | ||
} |
74 changes: 0 additions & 74 deletions
74
src/test/java/com/zaxxer/sansorm/transaction/TxTransactionManagerTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.sansorm; | ||
|
||
import org.h2.jdbcx.JdbcDataSource; | ||
import org.sqlite.SQLiteConfig; | ||
import org.sqlite.SQLiteDataSource; | ||
|
||
import java.io.File; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
|
||
public final class TestUtils { | ||
private TestUtils() { | ||
} | ||
|
||
public static JdbcDataSource makeH2DataSource() { | ||
return makeH2DataSource(true); | ||
} | ||
|
||
public static JdbcDataSource makeH2DataSource(boolean autoCommit) { | ||
final JdbcDataSource dataSource = new JdbcDataSource(); | ||
dataSource.setUrl(String.format("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;autocommit=%s", autoCommit ? "on" : "off")); | ||
return dataSource; | ||
} | ||
|
||
public static HikariDataSource makeSQLiteDataSource() { | ||
return makeSQLiteDataSource(null, true); | ||
} | ||
|
||
public static HikariDataSource makeSQLiteDataSource(File db) { | ||
return makeSQLiteDataSource(null, true); | ||
} | ||
|
||
public static HikariDataSource makeSQLiteDataSource(boolean autoCommit) { | ||
return makeSQLiteDataSource(null, autoCommit); | ||
} | ||
|
||
public static HikariDataSource makeSQLiteDataSource(File db, boolean autoCommit) { | ||
final SQLiteConfig sconfig = new SQLiteConfig(); | ||
sconfig.setJournalMode(SQLiteConfig.JournalMode.MEMORY); | ||
SQLiteDataSource sds = new SQLiteDataSource(sconfig); | ||
sds.setUrl(db == null | ||
? "jdbc:sqlite::memory:" | ||
: "jdbc:sqlite:" + db.getAbsolutePath() | ||
); | ||
|
||
HikariConfig hconfig = new HikariConfig(); | ||
hconfig.setAutoCommit(autoCommit); | ||
hconfig.setDataSource(sds); | ||
hconfig.setMaximumPoolSize(1); | ||
return new HikariDataSource(hconfig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters