use of java.sql.Savepoint in project derby by apache.
the class ClientConnection method rollback.
public synchronized void rollback(Savepoint savepoint) throws SQLException {
try {
int saveXaState = xaState_;
if (agent_.loggingEnabled()) {
agent_.logWriter_.traceEntry(this, "rollback", savepoint);
}
checkForClosedConnection();
if (// Throw exception if savepoint is null
savepoint == null) {
throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.XACT_SAVEPOINT_RELEASE_ROLLBACK_FAIL));
} else if (// Throw exception if auto-commit is on
autoCommit_) {
throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.NO_SAVEPOINT_ROLLBACK_OR_RELEASE_WHEN_AUTO));
}
// Only allow to rollback to a savepoint from the connection that create the savepoint.
try {
if (this != ((ClientSavepoint) savepoint).agent_.connection_) {
throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.SAVEPOINT_NOT_CREATED_BY_CONNECTION));
}
} catch (ClassCastException e) {
// savepoint is not an instance of am.Savepoint
throw new SqlException(agent_.logWriter_, new ClientMessageId(SQLState.SAVEPOINT_NOT_CREATED_BY_CONNECTION));
}
// Construct and flow a savepoint rollback statement to server.
ClientStatement stmt = null;
try {
stmt = createStatementX(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, holdability());
String savepointName;
try {
savepointName = ((ClientSavepoint) savepoint).getSavepointName();
} catch (SQLException e) {
// generate the name for an un-named savepoint.
savepointName = dncGeneratedSavepointNamePrefix__ + ((ClientSavepoint) savepoint).getSavepointId();
}
stmt.executeX("ROLLBACK TO SAVEPOINT " + Utils.quoteSqlIdentifier(savepointName));
} finally {
if (stmt != null) {
try {
stmt.closeX();
} catch (SqlException doNothing) {
}
}
xaState_ = saveXaState;
}
} catch (SqlException se) {
throw se.getSQLException();
}
}
use of java.sql.Savepoint in project derby by apache.
the class EmbedConnection method commonSetSavepointCode.
/**
* Creates a savepoint with the given name (if it is a named
* savepoint else we will generate a name because Derby only
* supports named savepoints internally) in the current
* transaction and returns the new Savepoint object that
* represents it.
*
* @param name A String containing the name of the savepoint. Will
* be null if this is an unnamed savepoint
* @param userSuppliedSavepointName If true means it's a named
* user defined savepoint.
*
* @return The new Savepoint object
*/
private Savepoint commonSetSavepointCode(String name, boolean userSuppliedSavepointName) throws SQLException {
synchronized (getConnectionSynchronization()) {
setupContextStack();
try {
verifySavepointCommandIsAllowed();
// supplied name is not null
if (userSuppliedSavepointName && (name == null)) {
throw newSQLException(SQLState.NULL_NAME_FOR_SAVEPOINT);
}
// supplied name length is not > 128
if (userSuppliedSavepointName && (name.length() > Limits.MAX_IDENTIFIER_LENGTH)) {
throw newSQLException(SQLState.LANG_IDENTIFIER_TOO_LONG, name, String.valueOf(Limits.MAX_IDENTIFIER_LENGTH));
}
// can't start with SYS
if (userSuppliedSavepointName && name.startsWith("SYS")) {
throw newSQLException(SQLState.INVALID_SCHEMA_SYS, "SYS");
}
Savepoint savePt = new EmbedSavepoint(this, name);
return savePt;
} catch (StandardException e) {
throw handleException(e);
} finally {
restoreContextStack();
}
}
}
use of java.sql.Savepoint in project incubator-skywalking by apache.
the class SWConnectionTest method testRollBackWithSavePointWithException.
@Test(expected = SQLException.class)
public void testRollBackWithSavePointWithException() throws SQLException {
doThrow(new SQLException()).when(jdbcConnection).rollback(any(Savepoint.class));
swConnection.rollback(savepoint);
assertThat(segmentStorage.getTraceSegments().size(), is(1));
TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
assertThat(spans.size(), is(1));
assertDBSpan(spans.get(0), "Mysql/JDBI/Connection/rollback to savepoint");
assertDBSpanLog(SpanHelper.getLogs(spans.get(0)).get(0));
}
use of java.sql.Savepoint in project narayana by jbosstm.
the class JDBC3Test method testSavepointTx.
@Test
public void testSavepointTx() throws Exception {
System.out.println("testSavepointTx...");
// it is not permitted to use savepoint methods if we have an XA tx...
javax.transaction.UserTransaction tx = com.arjuna.ats.jta.UserTransaction.userTransaction();
tx.begin();
boolean gotExpectedException = false;
Savepoint savepoint = null;
Savepoint mySavepoint = null;
try {
savepoint = conn.setSavepoint();
} catch (SQLException e) {
gotExpectedException = true;
}
if (!gotExpectedException) {
fail("Failed to get expected exception from setSavepoint inside tx");
}
gotExpectedException = false;
try {
mySavepoint = conn.setSavepoint("mySavepoint");
} catch (SQLException e) {
gotExpectedException = true;
}
if (!gotExpectedException) {
fail("Failed to get expected exception from setSavepoint(String) inside tx");
}
gotExpectedException = false;
try {
conn.rollback(mySavepoint);
} catch (SQLException e) {
gotExpectedException = true;
}
if (!gotExpectedException) {
fail("Failed to get expected exception from rollback(String) inside tx");
}
try {
conn.releaseSavepoint(savepoint);
} catch (SQLException e) {
gotExpectedException = true;
}
if (!gotExpectedException) {
fail("Failed to get expected exception from releaseSavepoint(String) inside tx");
}
try {
tx.rollback();
} catch (Exception e) {
e.printStackTrace(System.err);
fail();
}
}
use of java.sql.Savepoint in project debezium by debezium.
the class BinlogReaderBufferIT method shouldProcessRolledBackSavepoint.
@FixFor("DBZ-411")
@Test
public void shouldProcessRolledBackSavepoint() throws SQLException, InterruptedException {
String masterPort = System.getProperty("database.port", "3306");
String replicaPort = System.getProperty("database.replica.port", "3306");
boolean replicaIsMaster = masterPort.equals(replicaPort);
if (!replicaIsMaster) {
// Give time for the replica to catch up to the master ...
Thread.sleep(5000L);
}
// Use the DB configuration to define the connector's configuration to use the "replica"
// which may be the same as the "master" ...
config = Configuration.create().with(MySqlConnectorConfig.HOSTNAME, System.getProperty("database.replica.hostname", "localhost")).with(MySqlConnectorConfig.PORT, System.getProperty("database.replica.port", "3306")).with(MySqlConnectorConfig.USER, "snapper").with(MySqlConnectorConfig.PASSWORD, "snapperpass").with(MySqlConnectorConfig.SERVER_ID, 18765).with(MySqlConnectorConfig.SERVER_NAME, DATABASE.getServerName()).with(MySqlConnectorConfig.SSL_MODE, SecureConnectionMode.DISABLED).with(MySqlConnectorConfig.POLL_INTERVAL_MS, 10).with(MySqlConnectorConfig.DATABASE_WHITELIST, DATABASE.getDatabaseName()).with(MySqlConnectorConfig.DATABASE_HISTORY, FileDatabaseHistory.class).with(MySqlConnectorConfig.INCLUDE_SCHEMA_CHANGES, true).with(FileDatabaseHistory.FILE_PATH, DB_HISTORY_PATH).build();
// Start the connector ...
start(MySqlConnector.class, config);
// 11 schema change records + 1 SET statement
SourceRecords records = consumeRecordsByTopic(5 + 9 + 9 + 4 + 11 + 1);
// ---------------------------------------------------------------------------------------------------------------
if (replicaIsMaster) {
try (MySQLConnection db = MySQLConnection.forTestDatabase(DATABASE.getDatabaseName())) {
try (JdbcConnection connection = db.connect()) {
final Connection jdbc = connection.connection();
connection.setAutoCommit(false);
final Statement statement = jdbc.createStatement();
statement.executeUpdate("CREATE TEMPORARY TABLE tmp_ids (a int)");
statement.executeUpdate("INSERT INTO tmp_ids VALUES(5)");
jdbc.commit();
statement.executeUpdate("DROP TEMPORARY TABLE tmp_ids");
statement.executeUpdate("INSERT INTO customers VALUES(default, 'first', 'first', 'first')");
final Savepoint savepoint = jdbc.setSavepoint();
statement.executeUpdate("INSERT INTO customers VALUES(default, 'second', 'second', 'second')");
jdbc.rollback(savepoint);
jdbc.commit();
connection.query("SELECT * FROM customers", rs -> {
if (Testing.Print.isEnabled())
connection.print(rs);
});
connection.setAutoCommit(true);
}
}
// Bug DBZ-533
// INSERT + SAVEPOINT + INSERT + ROLLBACK
records = consumeRecordsByTopic(1 + 1 + 1 + 1);
assertThat(records.topics().size()).isEqualTo(1 + 1);
assertThat(records.recordsForTopic(DATABASE.topicForTable("customers"))).hasSize(2);
assertThat(records.allRecordsInOrder()).hasSize(4);
Testing.print("*** Done with savepoint TX");
}
}
Aggregations