Changeset: 18069ff37a3b for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/18069ff37a3b
Modified Files:
src/test/java/JDBC_API_Tester.java
src/test/java/org/monetdb/jdbc/ApiTests.java
Branch: default
Log Message:
Convert Test_CautocommitWithErrors() ->
ApiTests#testAutocommitWithFailedTransaction()
diffs (282 lines):
diff --git a/src/test/java/JDBC_API_Tester.java
b/src/test/java/JDBC_API_Tester.java
--- a/src/test/java/JDBC_API_Tester.java
+++ b/src/test/java/JDBC_API_Tester.java
@@ -3813,129 +3813,6 @@ public final class JDBC_API_Tester exten
);
}
- @Test
- public void Test_CautocommitWithErrors() {
- Connection helper = null;
- Statement stmt = null;
- ResultSet rs = null;
- sb.setLength(0);
-
- // Older versions forget to send autocommit status change
- // notifications when transaction fails
- assumeTrue(isAtLeast56);
-
- try {
- // Connections
- stmt = con.createStatement();
- helper = DriverManager.getConnection(jdbcUrl);
-
- // Used for autocommit verification
- stmt.executeUpdate(
- "DROP TABLE IF EXISTS
autocommitcheck;\n" +
- "CREATE TABLE
autocommitcheck(i INT);");
-
- sb.append("1a. initially auto commit mode is on\n");
- checkReportedAutocommit(true, helper);
-
- sb.append("1b. after an error it's still on\n");
- try {
- stmt.execute("SELECT x FROM autocommitcheck --
column x does not exist");
- } catch (SQLException ignored) {
- sb.append(" statement failed as expected\n");
- }
- checkReportedAutocommit(true, helper);
-
- sb.append("\n2a. START TRANSACTION turns it off\n");
- stmt.execute("START TRANSACTION");
- checkReportedAutocommit(false, helper);
-
- sb.append("2b. COMMIT turns it on\n");
- stmt.execute("COMMIT");
- checkReportedAutocommit(true, helper);
-
- sb.append("\n3a. START TRANSACTION turns it off
again\n");
- stmt.execute("START TRANSACTION");
- checkReportedAutocommit(false, helper);
-
- sb.append("3b. after an error it's still off\n");
- try {
- stmt.execute("SELECT x FROM autocommitcheck --
column x does not exist");
- } catch (SQLException ignored) {
- sb.append(" statement failed as expected\n");
- }
- checkReportedAutocommit(false, helper);
-
- sb.append("3c. after ROLLBACK it's on again\n");
- stmt.execute("ROLLBACK");
- checkReportedAutocommit(true, helper);
-
- sb.append("\n4a. START TRANSACTION turns it off
again\n");
- stmt.execute("START TRANSACTION");
- checkReportedAutocommit(false, helper);
-
- sb.append("4b. after an error it's still off\n");
- try {
- stmt.execute("SELECT x FROM autocommitcheck --
column x does not exist");
- } catch (SQLException ignored) {
- sb.append(" statement failed as expected\n");
- }
- checkReportedAutocommit(false, helper);
-
- sb.append("4c. COMMIT fails, autocommit is on again\n");
- try {
- stmt.execute("COMMIT");
- } catch (SQLException ignored) {
- sb.append(" statement failed as expected\n");
- }
- checkReportedAutocommit(true, helper);
- } catch (SQLException e) {
- sb.append("FAILED: " + e.getMessage() + "\n");
- closeConx(helper);
- closeStmtResSet(stmt, rs);
- sb.append("ABORTING TEST!!!");
- }
-
- //language=TEXT
- compareExpectedOutput("Test_Cautocommit_errors", ""
- + "1a. initially auto commit mode is on\n"
- + " reported autocommit = true, as expected\n"
- + " the inserted test data is visible, as
expected\n"
- + "1b. after an error it's still on\n"
- + " statement failed as expected\n"
- + " reported autocommit = true, as expected\n"
- + " the inserted test data is visible, as
expected\n"
- + "\n"
- + "2a. START TRANSACTION turns it off\n"
- + " reported autocommit = false, as expected\n"
- + " the inserted test data is not visible, as
expected\n"
- + "2b. COMMIT turns it on\n"
- + " reported autocommit = true, as expected\n"
- + " the inserted test data is visible, as
expected\n"
- + "\n"
- + "3a. START TRANSACTION turns it off again\n"
- + " reported autocommit = false, as expected\n"
- + " the inserted test data is not visible, as
expected\n"
- + "3b. after an error it's still off\n"
- + " statement failed as expected\n"
- + " reported autocommit = false, as expected\n"
- + " connection indeed in autocommit mode\n"
- + "3c. after ROLLBACK it's on again\n"
- + " reported autocommit = true, as expected\n"
- + " the inserted test data is visible, as
expected\n"
- + "\n"
- + "4a. START TRANSACTION turns it off again\n"
- + " reported autocommit = false, as expected\n"
- + " the inserted test data is not visible, as
expected\n"
- + "4b. after an error it's still off\n"
- + " statement failed as expected\n"
- + " reported autocommit = false, as expected\n"
- + " connection indeed in autocommit mode\n"
- + "4c. COMMIT fails, autocommit is on again\n"
- + " statement failed as expected\n"
- + " reported autocommit = true, as expected\n"
- + " the inserted test data is visible, as
expected\n");
- }
-
/**
* Verify 'con' believes it's in autocommit mode.
* @param expectedAutocommit
diff --git a/src/test/java/org/monetdb/jdbc/ApiTests.java
b/src/test/java/org/monetdb/jdbc/ApiTests.java
--- a/src/test/java/org/monetdb/jdbc/ApiTests.java
+++ b/src/test/java/org/monetdb/jdbc/ApiTests.java
@@ -42,6 +42,7 @@ public class ApiTests {
Statement stmt;
PreparedStatement pstmt;
private boolean scratchTransactionStarted;
+ private int nextUniqueId = 42;
// Skip all tests if the test database isn't running
@BeforeAll
@@ -195,8 +196,7 @@ public class ApiTests {
}
/**
- * Execute SQL code using this instances' {@link stmt} Statement
- * and ignore any SQLExceptions it may throw.
+ * Execute SQL code using the {@link stmt} Statement and ignore any
SQLExceptions
* @param sql statements to execute
*/
private void ignoreFailures(String sql) {
@@ -204,8 +204,7 @@ public class ApiTests {
}
/**
- * Execute the callback using this instances' {@link stmt} Statement
- * and ignore any SQLExceptions it may throw.
+ * Execute the callback and ignore any SQLExceptions it may throw.
* @param func callback to execute
*/
private void ignoreFailures(Assertions.SQLCode func) {
@@ -247,6 +246,117 @@ public class ApiTests {
}
@Test
+ public void testAutocommitWithFailedTransaction() throws SQLException {
+
+ // Older versions forget to send autocommit status change
+ // notifications when transaction fails.
+ assumeTrue(monetVersion.hasAccurateAutocommitStatus());
+
+ // We use a helper connection and -table to verify autocommit
status
+ stmt.execute("DROP TABLE IF EXISTS autocommitcheck");
+ try (
+ Connection helper = cloneConnection(conn,
"client_remark", "helper");
+ Statement hs = helper.createStatement()
+ ) {
+ hs.execute("CREATE TABLE autocommitcheck ( id int )");
+ hs.execute("INSERT INTO autocommitcheck VALUES (NULL)");
+ testAutocommitTransaction(helper);
+ hs.execute("DROP TABLE autocommitcheck");
+ }
+ }
+
+ private void testAutocommitTransaction(Connection helper) throws
SQLException {
+ // 1a. Initially, autocommit is on
+ assertTrue(conn.getAutoCommit());
+ assertTrue(isActuallyAutoCommit(helper));
+
+ // 1b. After an error it's still on
+ ignoreFailures("SELECT nosuchcolumn FROM autocommitcheck");
+ assertTrue(conn.getAutoCommit());
+ assertTrue(isActuallyAutoCommit(helper));
+
+ // 2a. START TRANSACTION turns it off
+ stmt.execute("START TRANSACTION");
+ assertFalse(conn.getAutoCommit());
+ assertFalse(isActuallyAutoCommit(helper));
+
+ // 2b. COMMIT (as a statement!) turns it back on
+ stmt.execute("COMMIT");
+ assertTrue(conn.getAutoCommit());
+ assertTrue(isActuallyAutoCommit(helper));
+
+ // 3a. START TRANSACTION turns it off again
+ stmt.execute("START TRANSACTION");
+ assertFalse(conn.getAutoCommit());
+ assertFalse(isActuallyAutoCommit(helper));
+
+ // 3b. After an error it's still off
+ ignoreFailures("SELECT nosuchcolumn FROM autocommitcheck");
+ assertFalse(conn.getAutoCommit());
+ assertFalse(isActuallyAutoCommit(helper));
+
+ // 3c. After ROLLBACK it's on again
+ stmt.execute("ROLLBACK");
+ assertTrue(conn.getAutoCommit());
+ assertTrue(isActuallyAutoCommit(helper));
+
+ // 4a.
+ stmt.execute("START TRANSACTION");
+ assertFalse(conn.getAutoCommit());
+ assertFalse(isActuallyAutoCommit(helper));
+
+ // 4b. After an error it's still off
+ ignoreFailures("SELECT nosuchcolumn FROM autocommitcheck");
+ assertFalse(conn.getAutoCommit());
+ assertFalse(isActuallyAutoCommit(helper));
+
+ // 4c. but commit fails due to the error, causing a rollback
+ // which turns autocommit back on
+ ignoreFailures("COMMIT");
+ assertFalse(conn.getAutoCommit());
+ assertFalse(isActuallyAutoCommit(helper));
+
+
+ // At the end of the test, autocommit is on
+ assertTrue(conn.getAutoCommit());
+ assertTrue(isActuallyAutoCommit(helper));
+
+ }
+
+ /**
+ * Update table 'autocommitcheck' on {@link conn}, then check the result
+ * on {@param helper}.
+ * A transaction conflict during the update also indicates auto commit
mode is off.
+ * @param helper Connection to verify the update on
+ * @return whether the helper sees the update
+ */
+ private boolean isActuallyAutoCommit(Connection helper) throws
SQLException {
+ // Update using conn
+ int cookie = nextUniqueId++;
+ try {
+ stmt.execute("UPDATE autocommitcheck SET id = " +
cookie);
+ } catch (SQLException e) {
+ if (e.getSQLState().equals("25005")) {
+ // It's a 25005!Current transaction is aborted
(please ROLLBACK).
+ // This means we're in a connection and
therefore auto commit is off
+ return false;
+ } else {
+ throw e;
+ }
+ }
+
+ // Does the helper see the update?
+ String query = "SELECT id FROM autocommitcheck WHERE id = " +
cookie;
+ try (
+ Statement s = helper.createStatement();
+ ResultSet rs = s.executeQuery(query)
+ ) {
+ return rs.next(); // if present, 'conn' autocommitted
the update
+ }
+ }
+
+
+ @Test
public void testIsValid() throws SQLException {
// initially valid
assertTrue(conn.isValid(30));
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]