/** * Prepare the given Connection before it is exposed. * <p>The default implementation applies the auto-commit flag, if necessary. * Can be overridden in subclasses. * @param con the Connection to prepare * @see #setAutoCommit */ protected void prepareConnection(Connection con) throws SQLException { Boolean autoCommit = getAutoCommitValue(); if (autoCommit != null && con.getAutoCommit() != autoCommit) { con.setAutoCommit(autoCommit); } }
public void prepare(boolean needsAutoCommit) throws SQLException { toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit(); if ( toggleAutoCommit ) { try { connection.commit(); } catch( Throwable ignore ) { // might happen with a managed connection } connection.setAutoCommit( true ); } }
@Override public void forceCommit() throws SQLException { if (!delegateConnection.isClosed() && !delegateConnection.getAutoCommit()) { delegateConnection.commit(); }; } }
protected <R> R getWithTransactionWithOutErrorPropagation(TransactionalFunction<R> function) { Instant start = Instant.now(); LazyToString callingMethod = getCallingMethod(); logger.trace("{} : starting transaction", callingMethod); try(Connection tx = dataSource.getConnection()) { boolean previousAutoCommitMode = tx.getAutoCommit(); tx.setAutoCommit(false); try { R result = function.apply(tx); tx.commit(); return result; } catch (Throwable th) { tx.rollback(); logger.info(ApplicationException.Code.CONFLICT + " " +th.getMessage()); return null; } finally { tx.setAutoCommit(previousAutoCommitMode); } } catch (SQLException ex) { throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, ex.getMessage(), ex); } finally { logger.trace("{} : took {}ms", callingMethod, Duration.between(start, Instant.now()).toMillis()); } }
Savepoint sp = null; LockRetryController lockRetryController = new LockRetryController(); boolean originalAutoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); conn.rollback(sp); lockRetryController.sleep(lce); conn.setAutoCommit(true);
/** * Closes the physical database connection represented by this PooledConnection. If any client has * a connection based on this PooledConnection, it is forcibly closed as well. */ @Override public void close() throws SQLException { if (last != null) { last.close(); if (!con.isClosed()) { if (!con.getAutoCommit()) { try { con.rollback(); } catch (SQLException ignored) { } } } } try { con.close(); } finally { con = null; } }
if (!conn.getAutoCommit()) conn.commit(); try { if (conn != null) // 高并发时,从数据库连接池获取连接就已经抛错误,所以conn可能为null的 conn.rollback();
public void prepare(boolean needsAutoCommit) throws SQLException { connection = provider.getConnection(); toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit(); if ( toggleAutoCommit ) { try { connection.commit(); } catch( Throwable ignore ) { // might happen with a managed connection } connection.setAutoCommit( true ); } }
try { connection = connectionProvider.getConnection(); boolean autoCommit = connection.getAutoCommit(); if (autoCommit) { connection.setAutoCommit(false); connection.rollback(); throw new RuntimeException("failed at least one sql statement in the batch, operation rolled back."); } else { try { connection.commit(); } catch (SQLException e) { throw new RuntimeException("Failed to commit insert query " + query, e);
/** * Sets this connection's auto-commit mode to the given state, saving * the original mode. The connection's original auto commit mode is restored * when the connection is closed. */ public void setAutoCommit(boolean autoCommit) throws SQLException { boolean currentAutoCommitValue = conn.getAutoCommit(); if (autoCommit != currentAutoCommitValue) { if (overwroteOriginalAutoCommitValue == false) { overwroteOriginalAutoCommitValue = true; originalAutoCommitValue = currentAutoCommitValue; } conn.setAutoCommit(autoCommit); } }
@Override public void done(RegisteredMigrationStep dbMigration) { long migrationNumber = dbMigration.getMigrationNumber(); try (Connection connection = database.getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("insert into schema_migrations(version) values (?)")) { statement.setString(1, String.valueOf(migrationNumber)); statement.execute(); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { throw new IllegalStateException(String.format("Failed to insert row with value %s in table %s", migrationNumber, SCHEMA_MIGRATIONS_TABLE), e); } }
@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(); }
/** * Closes the physical database connection represented by this * PooledConnection. If any client has a connection based on * this PooledConnection, it is forcibly closed as well. */ public void close() throws SQLException { if (last != null) { last.close(); if (!con.getAutoCommit()) { try { con.rollback(); } catch (SQLException e) { } } } try { con.close(); } finally { con = null; } }
if (cx instanceof UoWAttachedConnection) { ((UoWAttachedConnection) cx).forceCommit(); } else if (!cx.isClosed() && !cx.getAutoCommit()) { cx.commit(); Connection cx = u.root().getResource(CONNECTION_RESOURCE_NAME); try { if (!cx.isClosed() && !cx.getAutoCommit()) { cx.rollback();
public void prepare(boolean needsAutoCommit) throws SQLException { serviceRegistry = createServiceRegistry( cfgProperties ); connection = serviceRegistry.getService( ConnectionProvider.class ).getConnection(); if ( needsAutoCommit && ! connection.getAutoCommit() ) { connection.commit(); connection.setAutoCommit( true ); } }
boolean previousAutoCommitMode = tx.getAutoCommit(); tx.setAutoCommit(false); try { R result = function.apply(tx); tx.commit(); return result; } catch (Throwable th) { tx.rollback(); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, th.getMessage(), th); } finally { tx.setAutoCommit(previousAutoCommitMode);
/** * Sets this connection's auto-commit mode to the given state, saving * the original mode. The connection's original auto commit mode is restored * when the connection is closed. */ public void setAutoCommit(boolean autoCommit) throws SQLException { boolean currentAutoCommitValue = conn.getAutoCommit(); if (autoCommit != currentAutoCommitValue) { if (overwroteOriginalAutoCommitValue == false) { overwroteOriginalAutoCommitValue = true; originalAutoCommitValue = currentAutoCommitValue; } conn.setAutoCommit(autoCommit); } }
private void commitIfAutoCommitIsDisabled(TransactionContext c) throws SQLException, IOException { if (!c.getConnection().getAutoCommit()) { c.getConnection().commit(); } }
if (!con.getAutoCommit()) { try { con.rollback(); } catch (SQLException ignored) { con.setAutoCommit(autoCommit);
if (!connection.getAutoCommit()) { connection.rollback();