java.sql.Savepoint Java Examples

The following examples show how to use java.sql.Savepoint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectionStore.java    From aceql-http with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Stores the Savepoint in static for username + connectionId
 * 
 * @param savepoint
 *            the Savepoint to store
 */
public void put(Savepoint savepoint) {

	debug("Creating a Savepoint for user: " + connectionKey);
	if (savepoint == null) {
		throw new IllegalArgumentException("savepoint is null!");
	}

	Set<Savepoint> savepointSet = savepointMap.get(connectionKey);
	if (savepointSet == null) {
		savepointSet = new LinkedHashSet<Savepoint>();
	}

	savepointSet.add(savepoint);
	savepointMap.put(connectionKey, savepointSet);
}
 
Example #2
Source File: Transaction.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Commits transaction. If rollBackToOnError is null - whole transaction
 * will be rolledback
 * 
 * @param rollBackToOnError
 *            savepoint that should be used to rollback
 * @throws SQLException
 *             if something went wrongF
 */
public void commit(Savepoint rollBackToOnError) throws SQLException {

	try {
		connection.commit();
	} catch (SQLException e) {
		log.warn("Error while commiting transaction", e);

		try {
			if (rollBackToOnError != null) {
				connection.rollback(rollBackToOnError);
			} else {
				connection.rollback();
			}
		} catch (SQLException e1) {
			log.error("Can't rollback transaction", e1);
		}
	}

	connection.setAutoCommit(true);
	connection.close();
}
 
Example #3
Source File: JooqHealthCheckTest.java    From droptools with Apache License 2.0 6 votes vote down vote up
private HealthCheck.Result runHealthCheck(MockDataProvider provider) throws Exception {
    MockConnection mockConnection = new MockConnection(provider) {
        @Override
        public Savepoint setSavepoint() throws SQLException {
            return new Savepoint() {
                @Override
                public int getSavepointId() throws SQLException {
                    return 0;
                }

                @Override
                public String getSavepointName() throws SQLException {
                    return "savepoint";
                }
            };
        }
    };

    Configuration configuration = new DefaultConfiguration().set(mockConnection);
    configuration.settings().setExecuteLogging(false);
    JooqHealthCheck healthCheck = new JooqHealthCheck(configuration, validationQuery);

    return healthCheck.check();
}
 
Example #4
Source File: AutoGenJDBC30Test.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Inserts one row into a table with an auto-generated column while inside
 * a savepoint unit, does a rollback, then gets keys after an insert
 * into a table without an auto-generated column.
 * Old master Test 13.
 * Expected result: ResultSet has one row with a non-NULL key, and the
 * key value should be the same before and after the rollback.
 * @throws SQLException 
 */
public void testGetKeyAfterSavepointRollback() throws SQLException
{
    Connection conn = getConnection();
    Statement s = createStatement();
    Savepoint savepoint1 = conn.setSavepoint();

    int expected=1;

    s.execute("insert into t11_AutoGen(c11) values(99)", 
        Statement.RETURN_GENERATED_KEYS);
    int keyval = getKeyValue (s.getGeneratedKeys());
    assertEquals("Key value before rollback", expected, keyval);

    conn.rollback(savepoint1);

    s.execute("insert into t21_noAutoGen values(39, 'true')",
        Statement.RETURN_GENERATED_KEYS);
    keyval = getKeyValue (s.getGeneratedKeys());
    assertEquals("Key value after rollback", expected, keyval);

    s.close();
}
 
Example #5
Source File: ClientConnection.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Savepoint setSavepoint() throws SQLException {
  super.lock();
  try {
    if (autoCommit()) { // Throw exception if auto-commit is on
      throw ThriftExceptionUtil
          .newSQLException(SQLState.NO_SAVEPOINT_WHEN_AUTO);
    }
    if (++this.generatedSavepointId < 0) {
      this.generatedSavepointId = 1; // restart from 1 on overflow
    }
    InternalSavepoint savepoint = new InternalSavepoint(this,
        this.generatedSavepointId);
    setSavepoint(savepoint);
    return savepoint;
  } finally {
    super.unlock();
  }
}
 
Example #6
Source File: ClientConnection.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Savepoint setSavepoint(String name) throws SQLException {
  super.lock();
  try {
    if (name == null) {
      throw ThriftExceptionUtil
          .newSQLException(SQLState.NULL_NAME_FOR_SAVEPOINT);
    }
    if (autoCommit()) { // Throw exception if auto-commit is on
      throw ThriftExceptionUtil
          .newSQLException(SQLState.NO_SAVEPOINT_WHEN_AUTO);
    }
    InternalSavepoint savepoint = new InternalSavepoint(this, name);
    setSavepoint(savepoint);
    return savepoint;
  } finally {
    super.unlock();
  }
}
 
Example #7
Source File: ConnectionWrapper.java    From r-course with MIT License 6 votes vote down vote up
/**
 * @see Connection#setSavepoint()
 */
public java.sql.Savepoint setSavepoint() throws SQLException {
    checkClosed();

    if (isInGlobalTx()) {
        throw SQLError.createSQLException("Can't set autocommit to 'true' on an XAConnection", SQLError.SQL_STATE_INVALID_TRANSACTION_TERMINATION,
                MysqlErrorNumbers.ER_XA_RMERR, this.exceptionInterceptor);
    }

    try {
        return this.mc.setSavepoint();
    } catch (SQLException sqlException) {
        checkAndFireConnectionError(sqlException);
    }

    return null; // we don't reach this code, compiler can't tell
}
 
Example #8
Source File: JdbcTransactionObjectSupport.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation rolls back to the given JDBC 3.0 Savepoint.
 * @see java.sql.Connection#rollback(java.sql.Savepoint)
 */
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
    ConnectionHolder conHolder = getConnectionHolderForSavepoint();
    try {
        conHolder.getConnection().rollback((Savepoint) savepoint);
        conHolder.resetRollbackOnly();
    } catch (Throwable ex) {
        throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex);
    }
}
 
Example #9
Source File: DelegatingConnection.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void releaseSavepoint(final Savepoint savepoint) throws SQLException {
    checkOpen();
    try {
        connection.releaseSavepoint(savepoint);
    } catch (final SQLException e) {
        handleException(e);
    }
}
 
Example #10
Source File: TransactionThread.java    From spanner-jdbc with MIT License 5 votes vote down vote up
void releaseSavepoint(Savepoint savepoint) throws CloudSpannerSQLException {
  Preconditions.checkNotNull(savepoint);
  Integer index = savepoints.get(savepoint);
  if (index == null) {
    throw new CloudSpannerSQLException("Unknown savepoint: " + savepoint.toString(),
        Code.INVALID_ARGUMENT);
  }
  removeSavepointsAfter(index.intValue());
}
 
Example #11
Source File: TransactionLegacy.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public Savepoint setSavepoint() throws SQLException {
    _txn = true;
    StackElement st = new StackElement(START_TXN, null);
    _stack.push(st);
    final Connection conn = getConnection();
    final Savepoint sp = conn.setSavepoint();
    st.ref = sp;

    return sp;
}
 
Example #12
Source File: SavepointConstantOperationIT.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testRollingBackASavepointMakesDataInvisibleToMyself() throws Exception {
    Savepoint savepoint = conn1.setSavepoint("test");
    int value = 2;
    conn1.execute(String.format("insert into %s (taskid) values (%d)",b,value));
    long count = conn1.count(String.format("select * from %s where taskid=%d",b,value));
    assertEquals("Incorrect count after savepoint release!",1l,count);

    conn1.rollback(savepoint);
    count = conn1.count(String.format("select * from %s where taskid=%d",b,value));
    assertEquals("Incorrect count after savepoint release!",0l,count);
}
 
Example #13
Source File: ConnectionWrapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public java.sql.Savepoint setSavepoint(String arg0) throws SQLException {
    if (isInGlobalTx()) {
        throw SQLError.createSQLException(Messages.getString("ConnectionWrapper.0"), MysqlErrorNumbers.SQL_STATE_INVALID_TRANSACTION_TERMINATION,
                MysqlErrorNumbers.ER_XA_RMERR, this.exceptionInterceptor);
    }

    try {
        return this.mc.setSavepoint(arg0);
    } catch (SQLException sqlException) {
        checkAndFireConnectionError(sqlException);
    }

    return null; // we don't reach this code, compiler can't tell
}
 
Example #14
Source File: SavepointJdbc30Test.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Test 15 Check savepoints in batch */
public void testSavepointsInBatch() throws SQLException {
    Connection con = getConnection();
    Statement s = createStatement();
    s.execute("delete from t1");
    s.addBatch("insert into t1 values(1,1)");
    s.addBatch("insert into t1 values(2,2)");
    Savepoint savepoint1 = con.setSavepoint();
    s.addBatch("insert into t1 values(3,3)");
    s.executeBatch();
    con.rollback(savepoint1);

    assertTableRowCount("T1", 0);
    con.rollback();
}
 
Example #15
Source File: PoolConnectionImpl.java    From clearpool with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void rollback(Savepoint savepoint) throws SQLException {
  this.checkState();
  try {
    this.connection.rollback();
  } catch (SQLException ex) {
    this.handleException(ex);
  }
}
 
Example #16
Source File: ControlDAOImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void releaseSavepoint(Savepoint savepoint)
{
    try
    {
        Connection connection = template.getConnection();
        connection.releaseSavepoint(savepoint);
    }
    catch (SQLException e)
    {
        throw new RuntimeException("Failed to create SAVEPOINT: " + savepoint, e);
    }
}
 
Example #17
Source File: BrokeredConnection30.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public final Savepoint setSavepoint(String name)
	throws SQLException
{
	try {
		control.checkSavepoint();
		return getRealConnection().setSavepoint(name);
	}
	catch (SQLException se)
	{
		notifyException(se);
		throw se;
	}
}
 
Example #18
Source File: DeclareGlobalTempTableJavaJDBC30Test.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *  Savepoint and Rollback behavior - 11 - 3O
 *  
 *  @throws SQLException 
 */
public void testSavepointRollbackbehaviour11() throws SQLException {
    Statement s = createStatement();
    s.executeUpdate("declare global temporary table SESSION.t1(" +
            "c11 int, c12 int) on commit preserve rows not logged on " +
            "rollback delete rows");
    s.executeUpdate("DECLARE GLOBAL TEMPORARY TABLE SESSION.t2(" +
            "c21 int, c22 int) on commit preserve rows not logged on " +
            "rollback delete rows");
    s.executeUpdate("insert into SESSION.t1 values(11, 1)");
    s.executeUpdate("insert into SESSION.t2 values(21, 1)");
    commit();
    s.executeUpdate("insert into SESSION.t1 values(12, 2)");
    JDBC.assertSingleValueResultSet(s.executeQuery(
            "select count(*) from SESSION.t1") , "2");
    //set the first savepoint here
    Savepoint savepoint1 = getConnection().setSavepoint();
    s.executeUpdate("insert into SESSION.t2 values(22, 2)");
    JDBC.assertSingleValueResultSet(s.executeQuery(
            "select count(*) from SESSION.t2") , "2");
    //Rollback savepoint1; expect no data in t2 but t1 should have data
    getConnection().rollback(savepoint1);
    JDBC.assertSingleValueResultSet(s.executeQuery(
            "select count(*) from SESSION.t1") , "2");
    JDBC.assertSingleValueResultSet(s.executeQuery(
            "select count(*) from SESSION.t2") , "0");
    //Commit the transaction; expect no data in t2 but t1 should have data
    commit();
    JDBC.assertSingleValueResultSet(s.executeQuery(
            "select count(*) from SESSION.t1") , "2");
    JDBC.assertSingleValueResultSet(s.executeQuery(
            "select count(*) from SESSION.t2") , "0");
    s.executeUpdate("drop table SESSION.t1");
    s.executeUpdate("drop table SESSION.t2");
}
 
Example #19
Source File: TestConnection.java    From FHIR with Apache License 2.0 5 votes vote down vote up
public void rollback(Savepoint savepoint) throws SQLException {
    log.entering(this.getClass().getName(), methodLabel("rollback"));
    try {
        delegate.rollback(savepoint);
    } finally {
        log.exiting(this.getClass().getName(), methodLabel("rollback"));
    }
}
 
Example #20
Source File: PoolConnectionImpl.java    From clearpool with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
  this.checkState();
  try {
    this.connection.releaseSavepoint(savepoint);
  } catch (SQLException ex) {
    this.handleException(ex);
  }
}
 
Example #21
Source File: SingleConnectionDataSource.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    this.conn.releaseSavepoint(savepoint);
}
 
Example #22
Source File: Connection.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void rollback(Savepoint savepoint) throws SQLException {
}
 
Example #23
Source File: PrestoConnection.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public void rollback(Savepoint savepoint)
        throws SQLException
{
    throw new SQLFeatureNotSupportedException("rollback");
}
 
Example #24
Source File: NeverClosedConnection.java    From doma with Apache License 2.0 4 votes vote down vote up
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
  connection.releaseSavepoint(savepoint);
}
 
Example #25
Source File: MockConnection.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    throw new NotSupportException();
}
 
Example #26
Source File: ConnectionTest.java    From r-course with MIT License 4 votes vote down vote up
/**
 * Tests the savepoint functionality in MySQL.
 * 
 * @throws Exception
 *             if an error occurs.
 */
public void testSavepoint() throws Exception {
    DatabaseMetaData dbmd = this.conn.getMetaData();

    if (dbmd.supportsSavepoints()) {
        System.out.println("Testing SAVEPOINTs");

        try {
            this.conn.setAutoCommit(true);

            createTable("testSavepoints", "(field1 int)", "InnoDB");

            // Try with named save points
            this.conn.setAutoCommit(false);
            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");

            Savepoint afterInsert = this.conn.setSavepoint("afterInsert");
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");

            Savepoint afterUpdate = this.conn.setSavepoint("afterUpdate");
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();
            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);

            // Try with 'anonymous' save points
            this.conn.rollback();

            this.stmt.executeUpdate("INSERT INTO testSavepoints VALUES (1)");
            afterInsert = this.conn.setSavepoint();
            this.stmt.executeUpdate("UPDATE testSavepoints SET field1=2");
            afterUpdate = this.conn.setSavepoint();
            this.stmt.executeUpdate("DELETE FROM testSavepoints");

            assertTrue("Row count should be 0", getRowCount("testSavepoints") == 0);
            this.conn.rollback(afterUpdate);
            assertTrue("Row count should be 1", getRowCount("testSavepoints") == 1);
            assertTrue("Value should be 2", "2".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback(afterInsert);
            assertTrue("Value should be 1", "1".equals(getSingleValue("testSavepoints", "field1", null).toString()));
            this.conn.rollback();

            this.conn.releaseSavepoint(this.conn.setSavepoint());
        } finally {
            this.conn.setAutoCommit(true);
        }
    } else {
        System.out.println("MySQL version does not support SAVEPOINTs");
    }
}
 
Example #27
Source File: MultiHostMySQLConnection.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Savepoint setSavepoint() throws SQLException {
    return getActiveMySQLConnection().setSavepoint();
}
 
Example #28
Source File: TConnection.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
public Savepoint setSavepoint() throws SQLException {
    return null;  
}
 
Example #29
Source File: TConnection.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
@Override
public Savepoint setSavepoint(String name) throws SQLException {
    throw new UnsupportedOperationException("setSavepoint");
}
 
Example #30
Source File: NonClosableConnection.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
  delegate.releaseSavepoint(savepoint);
}