本文整理汇总了Java中java.sql.Connection.rollback方法的典型用法代码示例。如果您正苦于以下问题:Java Connection.rollback方法的具体用法?Java Connection.rollback怎么用?Java Connection.rollback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.sql.Connection
的用法示例。
在下文中一共展示了Connection.rollback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import java.sql.Connection; //导入方法依赖的package包/类
public void execute(AbstractSqlTable table) {
Connection connection;
synchronized (connection = table.connection()) {
try {
connection.setAutoCommit(false);
for (int i = 0; i < actions.size(); i++) {
handlers.get(i).accept(actions.get(i).execute(table));
}
connection.commit();
connection.setAutoCommit(true);
} catch (SQLException e) {
try {
connection.rollback();
connection.setAutoCommit(true);
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}
}
示例2: rollbackTransaction
import java.sql.Connection; //导入方法依赖的package包/类
public void rollbackTransaction() {
LOGGER.debug("Rolling back transaction");
Connection connection = transactionConnection.get();
if(connection == null) {
throw new IllegalStateException("Unable to rollback transaction with missing connection");
}
try {
connection.rollback();
connection.close();
}
catch(SQLException e) {
try {
connection.close();
}
catch(SQLException e1) {
throw new RuntimeException("Unable to close connection", e1);
}
throw new RuntimeException("Failed to rollback transaction", e);
}
finally {
transactionConnection.remove();
}
}
示例3: addBackup
import java.sql.Connection; //导入方法依赖的package包/类
public Backup addBackup(Backup backup) throws EntityExistException {
backup.setId(newBackupId());
Connection conn = null;
try {
conn = DBPower.getConnection(table.getId());
conn.setAutoCommit(false);
backup.setCreateTime(new Date());
saver.save(conn, backup);
importBackup(conn, backup);
conn.commit();
} catch (SQLException e) {
log.error( "", e );
try {
conn.rollback();
} catch (SQLException e1) {
log.error( "", e1 );
throw new RalasafeException(e1);
}
} finally {
DBUtil.close(conn);
}
return backup;
}
示例4: getConnection
import java.sql.Connection; //导入方法依赖的package包/类
/**
* @return a Connection instance that can be used to connect to the
* given database, if a previously-opened connection is available in
* the cache. Returns null if none is available in the map.
*/
public synchronized Connection getConnection(String connectStr,
String username) throws SQLException {
CacheKey key = new CacheKey(connectStr, username);
Connection cached = connectionMap.get(key);
if (null != cached) {
connectionMap.remove(key);
if (cached.isReadOnly()) {
// Read-only mode? Don't want it.
cached.close();
}
if (cached.isClosed()) {
// This connection isn't usable.
return null;
}
cached.rollback(); // Reset any transaction state.
cached.clearWarnings();
LOG.debug("Got cached connection for " + key);
}
return cached;
}
示例5: testLockTimeoutOnRowWithMvcc
import java.sql.Connection; //导入方法依赖的package包/类
@Test
public void testLockTimeoutOnRowWithMvcc() throws Exception {
Connection connection = openConnection(true);
try {
createTableAndInsertRow(connection);
} finally {
connection.close();
}
// Start the first transaction
Connection txn1 = openConnection(true);
try {
txn1.setAutoCommit(false);
// The first transaction should read the initial value
assertEquals(123, getCounter(txn1));
// Start the second transaction
Connection txn2 = openConnection(true);
try {
txn2.setAutoCommit(false);
// The second transaction should read the initial value
assertEquals(123, getCounter(txn2));
// The first transaction updates the value but doesn't commit it
assertEquals(1, setCounter(txn1, 234));
// The second transaction tries to update the value
try {
setCounter(txn2, 345);
fail();
} catch (SQLException expected) {
// Expected: the row is locked by the first transaction
}
// Abort the transactions
txn1.rollback();
txn2.rollback();
} finally {
txn2.close();
}
} finally {
txn1.close();
}
}
示例6: doSomeWork
import java.sql.Connection; //导入方法依赖的package包/类
private static void doSomeWork() throws SQLException {
Connection conn = getConnection();
Statement stmt = conn.createStatement();
conn.setAutoCommit(false);
stmt.execute("INSERT INTO trig_test VALUES (1, 'hello')");
stmt.execute("INSERT INTO trig_test VALUES (2, 'now what?')");
stmt.execute("INSERT INTO trig_test VALUES (3, 'unchangable')");
stmt.execute("INSERT INTO trig_test VALUES (4, 'goodbye')");
conn.commit();
dumpTable("trig_test");
stmt.execute("UPDATE trig_test SET value = 'all done'");
conn.commit();
dumpTable("trig_test");
stmt.execute("DELETE FROM trig_test");
conn.rollback();
dumpTable("trig_test");
try {
stmt.execute("INSERT INTO trig_test VALUES(11, 'whatever')");
} catch (SQLException se) {
se.printStackTrace();
}
stmt.execute("INSERT INTO trig_test VALUES(10, 'whatever')");
conn.commit();
dumpTable("trig_test");
stmt.close();
conn.close();
}
示例7: listDatabases
import java.sql.Connection; //导入方法依赖的package包/类
@Override
public String[] listDatabases() {
Connection conn = null;
ResultSet rset = null;
List<String> databases = new ArrayList<String>();
try {
conn = getConnection();
rset = conn.getMetaData().getSchemas();
while (rset.next()) {
// The ResultSet contains two columns - TABLE_SCHEM(1),
// TABLE_CATALOG(2). We are only interested in TABLE_SCHEM which
// represents schema name.
databases.add(rset.getString(1));
}
conn.commit();
} catch (SQLException sqle) {
try {
if (conn != null) {
conn.rollback();
}
} catch (SQLException ce) {
LoggingUtils.logAll(LOG, "Failed to rollback transaction", ce);
}
LoggingUtils.logAll(LOG, "Failed to list databases", sqle);
throw new RuntimeException(sqle);
} finally {
if (rset != null) {
try {
rset.close();
} catch (SQLException re) {
LoggingUtils.logAll(LOG, "Failed to close resultset", re);
}
}
}
return databases.toArray(new String[databases.size()]);
}
示例8: commitOrRollback
import java.sql.Connection; //导入方法依赖的package包/类
private void commitOrRollback(Connection connection, SqlExceptionThrowingRunnable runnable) throws SQLException {
try {
runnable.run();
connection.commit();
} catch (Exception e) {
connection.rollback();
throw e;
}
}
示例9: rollback
import java.sql.Connection; //导入方法依赖的package包/类
@Override
public void rollback() {
try {
Connection c = getConnection();
inTransaction = false;
c.rollback();
c.setAutoCommit(true);
} catch (SQLException sqle) {
log.warning("\bDatabase rollback failed. You should probably restart the application.");
}
}
示例10: testBug75209
import java.sql.Connection; //导入方法依赖的package包/类
/**
* Tests fix for Bug#75209 - Set useLocalTransactionState may result in partially committed transaction.
*/
public void testBug75209() throws Exception {
createTable("testBug75209", "(id INT PRIMARY KEY)", "InnoDB");
boolean useLocTransSt = false;
do {
this.stmt.executeUpdate("TRUNCATE TABLE testBug75209");
this.stmt.executeUpdate("INSERT INTO testBug75209 VALUES (1)");
final String testCase = String.format("Case: [LocTransSt: %s]", useLocTransSt ? "Y" : "N");
final Connection testConn = getConnectionWithProps("useLocalTransactionState=" + useLocTransSt);
testConn.setAutoCommit(false);
final Statement testStmt = testConn.createStatement();
try {
assertEquals(testCase, 1, testStmt.executeUpdate("INSERT INTO testBug75209 VALUES(2)"));
// This triggers Duplicate-key exception
testStmt.executeUpdate("INSERT INTO testBug75209 VALUES(2)");
fail(testCase + ": SQLException expected here!");
} catch (Exception e) {
testConn.rollback();
}
testStmt.close();
testConn.setAutoCommit(true);
testConn.close();
this.rs = this.stmt.executeQuery("SELECT COUNT(*) FROM testBug75209");
assertTrue(this.rs.next());
assertEquals(testCase, 1, this.rs.getInt(1));
} while (useLocTransSt = !useLocTransSt);
}
示例11: testWriteLockTimeoutOnTableWithoutMvcc
import java.sql.Connection; //导入方法依赖的package包/类
@Test
public void testWriteLockTimeoutOnTableWithoutMvcc() throws Exception {
Connection connection = openConnection(false);
try {
createTableAndInsertRow(connection);
} finally {
connection.close();
}
// Start the first transaction
Connection txn1 = openConnection(false);
try {
txn1.setAutoCommit(false);
// The first transaction should read the initial value
assertEquals(123, getCounter(txn1));
// The first transaction updates the value but doesn't commit yet
assertEquals(1, setCounter(txn1, 345));
// Start the second transaction
Connection txn2 = openConnection(false);
try {
txn2.setAutoCommit(false);
// The second transaction tries to read the value
try {
getCounter(txn2);
fail();
} catch (SQLException expected) {
// Expected: the table is locked by the first transaction
}
// Abort the transactions
txn1.rollback();
txn2.rollback();
} finally {
txn2.close();
}
} finally {
txn1.close();
}
}
示例12: testShouldAddSpanForConnection
import java.sql.Connection; //导入方法依赖的package包/类
@Test
public void testShouldAddSpanForConnection() throws Exception {
Connection connection = dataSource.getConnection();
connection.commit();
connection.rollback();
connection.close();
assertThat(ExceptionUtils.getLastException()).isNull();
assertThat(spanReporter.getSpans()).hasSize(1);
Span connectionSpan = spanReporter.getSpans().get(0);
assertThat(connectionSpan.getName()).isEqualTo("jdbc:/dataSource/connection");
assertThat(connectionSpan.logs()).extracting("event").contains("commit");
assertThat(connectionSpan.logs()).extracting("event").contains("rollback");
}
开发者ID:gavlyukovskiy,项目名称:spring-boot-data-source-decorator,代码行数:16,代码来源:TracingJdbcEventListenerTests.java
示例13: rollbackToSavepoint
import java.sql.Connection; //导入方法依赖的package包/类
/**
* Calls through to the {@link Connection#setSavepoint(String) current connection}.
*/
@Override
public void rollbackToSavepoint(Savepoint savepoint)
{
try
{
Connection connection = template.getConnection();
connection.rollback(savepoint);
}
catch (SQLException e)
{
throw new RuntimeException("Failed to create SAVEPOINT: " + savepoint, e);
}
}
示例14: rollbackTransaction
import java.sql.Connection; //导入方法依赖的package包/类
/**
* 回滚事务并关闭该连接,该事务连接是线程安全的。
*/
public static void rollbackTransaction() {
Connection connection = tl_conn.get();
if (connection == null) {
throw new RuntimeException("You do not start a Transaction so you can not rollback a transaction!");
}
try {
connection.rollback();
connection.close();
tl_conn.remove();
tl_sp.remove();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
示例15: execute
import java.sql.Connection; //导入方法依赖的package包/类
@Override
public void execute(Connection connection) throws SQLException
{
connection.rollback();
}