Search in sources :

Example 66 with Savepoint

use of java.sql.Savepoint in project spanner-jdbc by olavloite.

the class SavepointTest method testRollbackSavepoint.

@Test
public void testRollbackSavepoint() throws SQLException {
    connection.createStatement().execute("insert into foo (id, col1) values (1, 'test 1')");
    assertEquals(1, connection.getTransaction().getNumberOfBufferedMutations());
    Savepoint savepoint = connection.setSavepoint();
    connection.createStatement().execute("insert into foo (id, col1) values (2, 'test 2')");
    assertEquals(2, connection.getTransaction().getNumberOfBufferedMutations());
    connection.rollback(savepoint);
    assertEquals(1, connection.getTransaction().getNumberOfBufferedMutations());
}
Also used : Savepoint(java.sql.Savepoint) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 67 with Savepoint

use of java.sql.Savepoint in project spanner-jdbc by olavloite.

the class SavepointTest method testNamedEqualsAndHashcode.

@Test
public void testNamedEqualsAndHashcode() throws SQLException {
    Savepoint s1 = connection.setSavepoint("test1");
    Savepoint s2 = connection.setSavepoint("test2");
    assertFalse(s1.equals(s2));
    assertTrue(s1.equals(s1));
    assertTrue(s2.equals(s2));
    assertNotEquals(s1.hashCode(), s2.hashCode());
    s1 = connection.setSavepoint("test");
    s2 = connection.setSavepoint("test");
    assertTrue(s1.equals(s2));
    assertTrue(s1.equals(s1));
    assertTrue(s2.equals(s2));
    assertEquals(s1.hashCode(), s2.hashCode());
}
Also used : Savepoint(java.sql.Savepoint) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 68 with Savepoint

use of java.sql.Savepoint in project spanner-jdbc by olavloite.

the class SavepointTest method testRollbackToBegin.

@Test
public void testRollbackToBegin() throws SQLException {
    Savepoint savepoint = connection.setSavepoint();
    connection.createStatement().execute("insert into foo (id, col1) values (1, 'test 1')");
    connection.createStatement().execute("insert into foo (id, col1) values (2, 'test 2')");
    connection.createStatement().execute("insert into foo (id, col1) values (3, 'test 3')");
    connection.createStatement().execute("insert into foo (id, col1) values (4, 'test 4')");
    assertEquals(4, connection.getTransaction().getNumberOfBufferedMutations());
    connection.rollback(savepoint);
    assertEquals(0, connection.getTransaction().getNumberOfBufferedMutations());
}
Also used : Savepoint(java.sql.Savepoint) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 69 with Savepoint

use of java.sql.Savepoint in project spanner-jdbc by olavloite.

the class SavepointTest method testRollbackMultipleMutations.

@Test
public void testRollbackMultipleMutations() throws SQLException {
    connection.createStatement().execute("insert into foo (id, col1) values (1, 'test 1')");
    Savepoint savepoint = connection.setSavepoint();
    connection.createStatement().execute("insert into foo (id, col1) values (2, 'test 2')");
    connection.createStatement().execute("insert into foo (id, col1) values (3, 'test 3')");
    connection.createStatement().execute("insert into foo (id, col1) values (4, 'test 4')");
    assertEquals(4, connection.getTransaction().getNumberOfBufferedMutations());
    connection.rollback(savepoint);
    assertEquals(1, connection.getTransaction().getNumberOfBufferedMutations());
}
Also used : Savepoint(java.sql.Savepoint) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 70 with Savepoint

use of java.sql.Savepoint in project opentheso by miledrousset.

the class RelationsHelper method addRelationMT.

/**
 * Cette fonction permet de rajouter une relation type Groupe ou domaine à
 * un concept
 *
 * @param conn
 * @param idConcept
 * @param idGroup
 * @param idThesaurus
 * @param idUser
 * @return boolean
 */
public boolean addRelationMT(Connection conn, String idConcept, String idThesaurus, String idGroup, int idUser) {
    Statement stmt;
    boolean status = false;
    String query;
    Savepoint savepoint = null;
    try {
        // Get connection from pool
        savepoint = conn.setSavepoint();
        try {
            stmt = conn.createStatement();
            try {
                /*  if (!new RelationsHelper().addRelationHistorique(conn, idConcept, idThesaurus, idConcept, "MT", idUser, "ADD")) {
                        return false;
                    }*/
                query = "Insert into concept_group_concept" + "(idgroup, idthesaurus, idconcept)" + " values (" + "'" + idGroup + "'" + ",'" + idThesaurus + "'" + ",'" + idConcept + "')";
                stmt.executeUpdate(query);
                status = true;
            } finally {
                stmt.close();
            }
        } finally {
        // conn.close();
        }
    } catch (SQLException sqle) {
        // Log exception
        if (sqle.getSQLState().equalsIgnoreCase("23505")) {
            try {
                if (savepoint != null) {
                    conn.rollback(savepoint);
                    status = true;
                }
            } catch (SQLException ex) {
                Logger.getLogger(RelationsHelper.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            log.error("Error while adding relation Group of Concept : " + idConcept, sqle);
        }
    }
    return status;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Savepoint(java.sql.Savepoint)

Aggregations

Savepoint (java.sql.Savepoint)167 Statement (java.sql.Statement)61 Connection (java.sql.Connection)56 SQLException (java.sql.SQLException)55 PreparedStatement (java.sql.PreparedStatement)32 Test (org.junit.Test)31 ResultSet (java.sql.ResultSet)26 DatabaseMetaData (java.sql.DatabaseMetaData)13 UnitTest (nl.topicus.jdbc.test.category.UnitTest)13 TransactionStatus (org.springframework.transaction.TransactionStatus)12 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)12 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)12 ArrayList (java.util.ArrayList)11 Vector (java.util.Vector)11 Test (org.junit.jupiter.api.Test)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 SQLClientInfoException (java.sql.SQLClientInfoException)7 HashMap (java.util.HashMap)5 ConcurrencyFailureException (org.springframework.dao.ConcurrencyFailureException)5 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)4