Java Code Examples for java.sql.Savepoint

The following examples show how to use java.sql.Savepoint. These examples are extracted from open source projects. 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 Project: gemfirexd-oss   Source File: ClientConnection.java    License: 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 2
Source Project: aion-germany   Source File: Transaction.java    License: 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 Project: droptools   Source File: JooqHealthCheckTest.java    License: 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 Project: gemfirexd-oss   Source File: ClientConnection.java    License: 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 5
Source Project: r-course   Source File: ConnectionWrapper.java    License: 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 6
/**
 * 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 7
/**
 * 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 8
Source Project: Tomcat8-Source-Read   Source File: DelegatingConnection.java    License: 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 9
Source Project: spanner-jdbc   Source File: TransactionThread.java    License: 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 10
Source Project: cloudstack   Source File: TransactionLegacy.java    License: 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 11
@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 12
Source Project: clearpool   Source File: PoolConnectionImpl.java    License: 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 13
Source Project: FHIR   Source File: TestConnection.java    License: 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 14
Source Project: lams   Source File: ConnectionWrapper.java    License: 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 15
/** 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 16
Source Project: clearpool   Source File: PoolConnectionImpl.java    License: 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 17
@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 18
Source Project: gemfirexd-oss   Source File: BrokeredConnection30.java    License: 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 19
/**
 *  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 20
/**
 * 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 21
Source Project: ontopia   Source File: SpyConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Savepoint setSavepoint(String savepoint)
  throws SQLException {
  return conn.setSavepoint(savepoint);
}
 
Example 22
Source Project: nh-micro   Source File: MicroPooledConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Savepoint setSavepoint(String name) throws SQLException {
	return conn.setSavepoint(name);
}
 
Example 23
Source Project: ignite   Source File: JdbcConnection.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    ensureNotClosed();

    throw new SQLFeatureNotSupportedException("Transactions are not supported.");
}
 
Example 24
Source Project: rxjava-jdbc   Source File: ConnectionNonClosing.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Savepoint setSavepoint(String name) throws SQLException {
    return con.setSavepoint(name);
}
 
Example 25
Source Project: eth-jdbc-connector   Source File: EthConnection.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void rollback(Savepoint savepoint) throws SQLException {
    throw new UnsupportedOperationException();
}
 
Example 26
Source Project: olaper   Source File: ServerConnection.java    License: MIT License 4 votes vote down vote up
@Override
public Savepoint setSavepoint(String name) throws SQLException {
	// TODO Auto-generated method stub
	return null;
}
 
Example 27
Source Project: micro-integrator   Source File: TConnection.java    License: Apache License 2.0 4 votes vote down vote up
public Savepoint setSavepoint() throws SQLException {
    return null;  
}
 
Example 28
Source Project: r-course   Source File: ConnectionTest.java    License: 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 29
Source Project: jdk8u-jdk   Source File: StubConnection.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example 30
Source Project: tomee   Source File: ManagedConnectionBehaviorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Savepoint setSavepoint(String name) throws SQLException {
    return null;
}