当前位置: 首页>>代码示例>>Java>>正文


Java Connection.rollback方法代码示例

本文整理汇总了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();
            }
        }

    }
}
 
开发者ID:shawlaf,项目名称:Banmanager,代码行数:25,代码来源:Transaction.java

示例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();
    }
}
 
开发者ID:oyvindBjerke,项目名称:jdbc-middleware,代码行数:24,代码来源:ConnectionManager.java

示例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;
}
 
开发者ID:yswang0927,项目名称:ralasafe,代码行数:25,代码来源:BackupManagerImpl.java

示例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;
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:30,代码来源:OracleManager.java

示例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();
	}
}
 
开发者ID:rafjordao,项目名称:Nird2,代码行数:40,代码来源:TransactionIsolationTest.java

示例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();
    }
 
开发者ID:tiweGH,项目名称:OpenDiabetes,代码行数:32,代码来源:TriggerSample.java

示例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()]);
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:38,代码来源:Db2Manager.java

示例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;
  }
}
 
开发者ID:alfasoftware,项目名称:morf,代码行数:10,代码来源:SqlScriptExecutor.java

示例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.");
    }
}
 
开发者ID:drytoastman,项目名称:scorekeeperfrontend,代码行数:12,代码来源:PostgresqlDatabase.java

示例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);
}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:37,代码来源:ConnectionRegressionTest.java

示例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();
	}
}
 
开发者ID:rafjordao,项目名称:Nird2,代码行数:38,代码来源:TransactionIsolationTest.java

示例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);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:17,代码来源:ControlDAOImpl.java

示例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);
    }
}
 
开发者ID:FlyingHe,项目名称:UtilsMaven,代码行数:18,代码来源:JDBCUtils.java

示例15: execute

import java.sql.Connection; //导入方法依赖的package包/类
@Override
public void execute(Connection connection) throws SQLException
{
	connection.rollback();
}
 
开发者ID:olavloite,项目名称:spanner-jdbc,代码行数:6,代码来源:DMLTester.java


注:本文中的java.sql.Connection.rollback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。