public void executeBatch(List<Entity> entities) throws SQLException { try (Connection connection = dataSource.getConnection()) { connection.setAutoCommit(false); try (PreparedStatement statement = connection.prepareStatement(SQL)) { // ... try { connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } } } }
try(Connection con = getConnection()) { try (PreparedStatement prep = con.prepareConnection("Update ...")) { //prep.doSomething(); //... //etc con.commit(); } catch (SQLException e) { //any other actions necessary on failure con.rollback(); //consider a re-throw, throwing a wrapping exception, etc } }
@Override public synchronized void endTransaction() { try { if (transactionSuccessful) connection.commit(); else connection.rollback(); connection.setAutoCommit(true); } catch (SQLException e) { throw new RuntimeException(e); } }
/** * 通知事务 * * @param state transactionState * @return RpcResponseState RpcResponseState */ public RpcResponseState notify(int state) { try { if (state == 1) { log.debug("commit transaction type[lcn] proxy connection:{}.", this); connection.commit(); } else { log.debug("rollback transaction type[lcn] proxy connection:{}.", this); connection.rollback(); } connection.close(); log.debug("transaction type[lcn] proxy connection:{} closed.", this); return RpcResponseState.success; } catch (Exception e) { log.error(e.getLocalizedMessage(), e); return RpcResponseState.fail; } }
/** * For testing only, do not use. */ @VisibleForTesting public int numLocksInLockTable() throws SQLException, MetaException { Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED); stmt = dbConn.createStatement(); String s = "select count(*) from HIVE_LOCKS"; LOG.debug("Going to execute query <" + s + ">"); rs = stmt.executeQuery(s); rs.next(); int rc = rs.getInt(1); // Necessary to clean up the transaction in the db. dbConn.rollback(); return rc; } finally { close(rs, stmt, dbConn); } }
stmt.execute(QUERY_CHANNEL_SIZE); ResultSet rset = stmt.getResultSet(); if (!rset.next()) { throw new JdbcChannelException("Failed to determine channel size: " + "Query (" + QUERY_CHANNEL_SIZE connection.commit(); } catch (SQLException ex) { try { connection.rollback(); } catch (SQLException ex2) { LOGGER.error("Unable to rollback transaction", ex2);
connection.prepareStatement(createMetadataTableQuery).execute(); created = true; LOGGER.fine("updating mosaic table"); final PreparedStatement ps = connection.prepareStatement(insertTileQuery); connection.commit(); } catch (SQLException e) { if (LOGGER.isLoggable(Level.SEVERE)) { connection.rollback(); } catch (SQLException ex) { if (LOGGER.isLoggable(Level.FINE)) {
Collection<SQLException> releaseConnections(final boolean forceRollback) { Collection<SQLException> result = new LinkedList<>(); for (Connection each : cachedConnections.values()) { try { if (forceRollback && stateHandler.isInTransaction()) { each.rollback(); } each.close(); } catch (final SQLException ex) { result.add(ex); } } cachedConnections.clear(); methodInvocations.clear(); return result; }
@Test public void testTransactionWithExceptionOnRollback() throws Exception { given(con.getAutoCommit()).willReturn(true); willThrow(new SQLException("Cannot rollback")).given(con).rollback(); TransactionTemplate tt = new TransactionTemplate(tm); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException { status.setRollbackOnly(); } }); fail("Should have thrown TransactionSystemException"); } catch (TransactionSystemException ex) { // expected } assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).rollback(); ordered.verify(con).setAutoCommit(true); verify(con).close(); }
try (PreparedStatement statement = conn.prepareStatement(queryLine.getResultQuery())) { this.statement = statement; int n = 1; while (rs.next()) { if (rs.getMetaData().getColumnCount() != 0) { List<Object> row = new ArrayList<Object>(); conn.rollback(); } catch (Exception e1) {
/** * 回滚事务 * * @throws SQLException SQL执行异常 */ public void rollback() throws SQLException { try { getConnection().rollback(); } catch (SQLException e) { throw e; } finally { try { getConnection().setAutoCommit(true); // 事务结束,恢复自动提交 } catch (SQLException e) { log.error(e); } } }
conn.setAutoCommit(false); rsPK = stPK.executeQuery(selectPKSQL); } else { pstPK = statementProxy.getConnection().prepareStatement(selectPKSQL); for (int i = 0; i < paramAppender.size(); i++) { pstPK.setObject(i + 1, paramAppender.get(i)); conn.rollback(sp); lockRetryController.sleep(lce); conn.setAutoCommit(true);
Properties props = new Properties(); Connection conn = DriverManager.getConnection(getUrl(), props); PreparedStatement statement = conn.prepareStatement(dmlStmt); statement.execute(); statement = conn.prepareStatement(upsertStmt); statement.setString(1, orgId); statement.setString(2, keyPrefix2); conn.rollback(); // to clear the list of mutations for the next
public void executeSomeQuery() throws SQLException { try (Connection connection = dataSource.getConnection()) { connection.setAutoCommit(false); try (PreparedStatement statement = connection.prepareStatement(SOME_SQL)) { // Fire transactional queries here. connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } } }
private static void exec(Connection cxn, Migration migration) throws SQLException { cxn.setAutoCommit(false); try { Instant start = Instant.now(); migration.run(cxn); cxn.commit(); LOGGER.info("Data migration took {} ms", Duration.between(start, Instant.now()).toMillis()); } catch (SQLException e) { LOGGER.error("Data migration failed: {}", e.getMessage(), e); cxn.rollback(); throw e; } }
LOG.debug("Going to execute query <" + s + ">"); rs = stmt.executeQuery(s); if (!rs.next()) { LOG.debug("No compactions found ready to compact"); dbConn.rollback(); return null; int updCount = updStmt.executeUpdate(s); if(updCount == 1) { dbConn.commit(); return info; dbConn.rollback(); return null; } while( rs.next()); dbConn.rollback(); return null; } catch (SQLException e) {
LOGGER.fine("Looking for mosaic table already created"); final PreparedStatement ps = connection.prepareStatement(selectMetadataTableQuery); ResultSet set = ps.executeQuery(); final boolean allTablesArePresent = checkTileTables(set, connection); connection.commit(); return allTablesArePresent; } catch (SQLException e) { connection.rollback(); } catch (SQLException ex) { if (LOGGER.isLoggable(Level.FINE)) {
Collection<SQLException> releaseConnections(final boolean forceRollback) { Collection<SQLException> result = new LinkedList<>(); for (Connection each : cachedConnections.values()) { try { if (forceRollback && stateHandler.isInTransaction()) { each.rollback(); } each.close(); } catch (final SQLException ex) { result.add(ex); } } cachedConnections.clear(); methodInvocations.clear(); return result; }