@Override public void begin() throws HibernateException { if ( !valid ) { throw new TransactionException( "Transaction instance is no longer valid" ); } if ( localStatus == LocalStatus.ACTIVE ) { throw new TransactionException( "nested transactions not supported" ); } if ( localStatus != LocalStatus.NOT_ACTIVE ) { throw new TransactionException( "reuse of Transaction instances not supported" ); } LOG.debug( "begin" ); doBegin(); localStatus = LocalStatus.ACTIVE; afterTransactionBegin(); }
@Override public void commit() throws HibernateException { if ( localStatus != LocalStatus.ACTIVE ) { throw new TransactionException( "Transaction not successfully started" ); } LOG.debug( "committing" ); beforeTransactionCommit(); try { doCommit(); localStatus = LocalStatus.COMMITTED; afterTransactionCompletion( Status.STATUS_COMMITTED ); } catch (Exception e) { localStatus = LocalStatus.FAILED_COMMIT; afterTransactionCompletion( Status.STATUS_UNKNOWN ); throw new TransactionException( "commit failed", e ); } finally { invalidate(); afterAfterCompletion(); } }
@Override protected void doBegin() { try { if ( managedConnection != null ) { throw new TransactionException( "Already have an associated managed connection" ); } managedConnection = transactionCoordinator().getJdbcCoordinator().getLogicalConnection().getConnection(); wasInitiallyAutoCommit = managedConnection.getAutoCommit(); LOG.debugv( "initial autocommit status: {0}", wasInitiallyAutoCommit ); if ( wasInitiallyAutoCommit ) { LOG.debug( "disabling autocommit" ); managedConnection.setAutoCommit( false ); } } catch( SQLException e ) { throw new TransactionException( "JDBC begin transaction failed: ", e ); } isDriver = transactionCoordinator().takeOwnership(); }
@Override protected void doBegin() { LOG.debug( "begin" ); userTransaction = locateUserTransaction(); try { if ( userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION ) { userTransaction.begin(); isInitiator = true; LOG.debug( "Began a new JTA transaction" ); } } catch ( Exception e ) { throw new TransactionException( "JTA transaction begin failed", e ); } }
@Override protected void afterAfterCompletion() { // this method is a noop if there is a Synchronization! try { if ( isDriver ) { if ( !isInitiator ) { LOG.setManagerLookupClass(); } try { transactionCoordinator().afterTransaction( this, userTransaction.getStatus() ); } catch (SystemException e) { throw new TransactionException( "Unable to determine UserTransaction status", e ); } } } finally { isInitiator = false; } }
@Override protected void doRollback() { try { if ( isInitiator ) { // failed commits automatically rollback the transaction per JTA spec if ( getLocalStatus() != LocalStatus.FAILED_COMMIT ) { userTransaction.rollback(); LOG.debug( "Rolled back JTA UserTransaction" ); } } else { markRollbackOnly(); } } catch ( Exception e ) { throw new TransactionException( "JTA rollback failed", e ); } }
public void begin() throws HibernateException { if (begun) { return; } log.debug("begin"); boolean synchronization = jdbcContext.registerSynchronizationIfPossible(); if ( !synchronization ) { throw new TransactionException("Could not register synchronization for container transaction"); } begun = true; jdbcContext.afterTransactionBegin(this); }
public void commit() throws HibernateException { if (!begun) { throw new TransactionException("Transaction not successfully started"); } log.debug("commit"); boolean flush = !transactionContext.isFlushModeNever() && !transactionContext.isFlushBeforeCompletionEnabled(); if (flush) { transactionContext.managedFlush(); //if an exception occurs during flush, user must call rollback() } begun = false; }
public void rollback() throws HibernateException { if (!begun) { throw new TransactionException("Transaction not successfully started"); } log.debug("rollback"); try { getTransaction().setRollbackOnly(); } catch (SystemException se) { log.error("Could not set transaction to rollback only", se); throw new TransactionException("Could not set transaction to rollback only", se); } begun = false; }
public boolean isActive() throws TransactionException { if (!begun) return false; final int status; try { status = getTransaction().getStatus(); } catch (SystemException se) { log.error("Could not determine transaction status", se); throw new TransactionException("Could not determine transaction status: ", se); } if (status==Status.STATUS_UNKNOWN) { throw new TransactionException("Could not determine transaction status"); } else { return status==Status.STATUS_ACTIVE; } }
public boolean wasRolledBack() throws TransactionException { if (!begun) return false; final int status; try { status = getTransaction().getStatus(); } catch (SystemException se) { log.error("Could not determine transaction status", se); throw new TransactionException("Could not determine transaction status", se); } if (status==Status.STATUS_UNKNOWN) { throw new TransactionException("Could not determine transaction status"); } else { return JTAHelper.isRollback(status); } }
public boolean wasCommitted() throws TransactionException { if ( !begun ) return false; final int status; try { status = getTransaction().getStatus(); } catch (SystemException se) { log.error("Could not determine transaction status", se); throw new TransactionException("Could not determine transaction status: ", se); } if (status==Status.STATUS_UNKNOWN) { throw new TransactionException("Could not determine transaction status"); } else { return status==Status.STATUS_COMMITTED; } }
public JTATransaction( InitialContext context, String utName, JDBCContext jdbcContext, TransactionFactory.Context transactionContext ) { this.jdbcContext = jdbcContext; this.transactionContext = transactionContext; log.debug("Looking for UserTransaction under: " + utName); try { ut = (UserTransaction) context.lookup(utName); } catch (NamingException ne) { log.error("Could not find UserTransaction in JNDI", ne); throw new TransactionException("Could not find UserTransaction in JNDI: ", ne); } if (ut==null) { throw new AssertionFailure("A naming service lookup returned null"); } log.debug("Obtained UserTransaction"); }
public boolean wasRolledBack() throws TransactionException { //if (!begun) return false; //if (commitFailed) return true; final int status; try { status = ut.getStatus(); } catch (SystemException se) { log.error("Could not determine transaction status", se); throw new TransactionException("Could not determine transaction status", se); } if (status==Status.STATUS_UNKNOWN) { throw new TransactionException("Could not determine transaction status"); } else { return JTAHelper.isRollback(status); } }
public boolean wasCommitted() throws TransactionException { //if (!begun || commitFailed) return false; final int status; try { status = ut.getStatus(); } catch (SystemException se) { log.error("Could not determine transaction status", se); throw new TransactionException("Could not determine transaction status: ", se); } if (status==Status.STATUS_UNKNOWN) { throw new TransactionException("Could not determine transaction status"); } else { return status==Status.STATUS_COMMITTED; } }
public boolean isActive() throws TransactionException { if (!begun || commitFailed || commitSucceeded) return false; final int status; try { status = ut.getStatus(); } catch (SystemException se) { log.error("Could not determine transaction status", se); throw new TransactionException("Could not determine transaction status: ", se); } if (status==Status.STATUS_UNKNOWN) { throw new TransactionException("Could not determine transaction status"); } else { return status==Status.STATUS_ACTIVE; } }
/** * shared logic for tearing down resources used in our unit tests */ public static void tearDownHelper() { TransactionException rollbackException = null; if (HibernateFactory.inTransaction()) { try { HibernateFactory.rollbackTransaction(); //HibernateFactory.commitTransaction(); } catch (TransactionException e) { rollbackException = e; } } HibernateFactory.closeSession(); if (rollbackException != null) { throw rollbackException; } // In case someone disabled it and forgot to // renable it. RhnBaseTestCase.enableLocalizationServiceLogging(); }
public boolean canRegisterSynchronization() { try { return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE); } catch (SystemException ex) { throw new TransactionException("Could not determine JTA transaction status", ex); } }
public void registerSynchronization(Synchronization synchronization) { if (this.transactionSynchronizationRegistry != null) { this.transactionSynchronizationRegistry.registerInterposedSynchronization(synchronization); } else { try { this.transactionManager.getTransaction().registerSynchronization(synchronization); } catch (Exception ex) { throw new TransactionException("Could not access JTA Transaction to register synchronization", ex); } } }
@Override public void rollback() throws HibernateException { if ( localStatus != LocalStatus.ACTIVE && localStatus != LocalStatus.FAILED_COMMIT ) { throw new TransactionException( "Transaction not successfully started" ); } LOG.debug( "rolling back" ); beforeTransactionRollBack(); if ( localStatus != LocalStatus.FAILED_COMMIT || allowFailedCommitToPhysicallyRollback() ) { try { doRollback(); localStatus = LocalStatus.ROLLED_BACK; afterTransactionCompletion( Status.STATUS_ROLLEDBACK ); } catch (Exception e) { afterTransactionCompletion( Status.STATUS_UNKNOWN ); throw new TransactionException( "rollback failed", e ); } finally { invalidate(); afterAfterCompletion(); } } }
@Override protected void doCommit() throws TransactionException { try { managedConnection.commit(); LOG.debug( "committed JDBC Connection" ); } catch( SQLException e ) { throw new TransactionException( "unable to commit against JDBC connection", e ); } finally { releaseManagedConnection(); } }
@Override protected void doRollback() throws TransactionException { try { managedConnection.rollback(); LOG.debug( "rolled JDBC Connection" ); } catch( SQLException e ) { throw new TransactionException( "unable to rollback against JDBC connection", e ); } finally { releaseManagedConnection(); } }
@Override protected void afterTransactionBegin() { if ( ! transactionCoordinator().isSynchronizationRegistered() ) { throw new TransactionException("Could not register synchronization for container transaction"); } transactionCoordinator().sendAfterTransactionBeginNotifications( this ); transactionCoordinator().getTransactionContext().afterTransactionBegin( this ); }
@Override public void markRollbackOnly() { try { getTransactionManager().setRollbackOnly(); } catch ( SystemException se ) { throw new TransactionException("Could not set transaction to rollback only", se); } }
@Override public boolean isJoinableJtaTransaction(TransactionCoordinator transactionCoordinator, CMTTransaction transaction) { try { final int status = transactionCoordinator .getTransactionContext() .getTransactionEnvironment() .getJtaPlatform() .getCurrentStatus(); return JtaStatusHelper.isActive( status ); } catch( SystemException se ) { throw new TransactionException( "Unable to check transaction status", se ); } }
private UserTransaction locateUserTransaction() { final UserTransaction userTransaction = jtaPlatform().retrieveUserTransaction(); if ( userTransaction == null ) { throw new TransactionException( "Unable to locate JTA UserTransaction" ); } return userTransaction; }
private void applyTimeout() { if ( getTimeout() > 0 ) { if ( userTransaction != null ) { try { userTransaction.setTransactionTimeout( getTimeout() ); } catch ( SystemException e ) { throw new TransactionException( "Unable to apply requested transaction timeout", e ); } } else { LOG.debug( "Unable to apply requested transaction timeout; no UserTransaction. Will try later" ); } } }
@Override protected void doCommit() { try { if ( isInitiator ) { userTransaction.commit(); LOG.debug( "Committed JTA UserTransaction" ); } } catch ( Exception e ) { throw new TransactionException( "JTA commit failed: ", e ); } }
@Override public boolean isActive() throws HibernateException { if ( getLocalStatus() != LocalStatus.ACTIVE ) { return false; } final int status; try { status = userTransaction.getStatus(); } catch ( SystemException se ) { throw new TransactionException( "Could not determine transaction status: ", se ); } return JtaStatusHelper.isActive( status ); }
/** * Extract the status code from a {@link UserTransaction} * * @param userTransaction The {@link UserTransaction} from which to extract the status. * * @return The transaction status * * @throws TransactionException If the {@link UserTransaction} reports the status as unknown */ public static int getStatus(UserTransaction userTransaction) { try { final int status = userTransaction.getStatus(); if ( status == Status.STATUS_UNKNOWN ) { throw new TransactionException( "UserTransaction reported transaction status as unknown" ); } return status; } catch ( SystemException se ) { throw new TransactionException( "Could not determine transaction status", se ); } }
/** * Extract the status code from the current {@link javax.transaction.Transaction} associated with the * given {@link TransactionManager} * * @param transactionManager The {@link TransactionManager} from which to extract the status. * * @return The transaction status * * @throws TransactionException If the {@link TransactionManager} reports the status as unknown */ public static int getStatus(TransactionManager transactionManager) { try { final int status = transactionManager.getStatus(); if ( status == Status.STATUS_UNKNOWN ) { throw new TransactionException( "TransactionManager reported transaction status as unknwon" ); } return status; } catch ( SystemException se ) { throw new TransactionException( "Could not determine transaction status", se ); } }
@Override public int determineRemainingTransactionTimeOutPeriod() { if ( transactionTimeOutInstant < 0 ) { return -1; } final int secondsRemaining = (int) ((transactionTimeOutInstant - System.currentTimeMillis()) / 1000); if ( secondsRemaining <= 0 ) { throw new TransactionException( "transaction timeout expired" ); } return secondsRemaining; }
private QProperty persistProp(QProperty prop, boolean save) { final Exception res; try { res = (Exception) Spring.getInstance().getTt().execute((TransactionStatus status) -> { try { if (save) { Spring.getInstance().getHt().saveOrUpdate(prop); } else { Spring.getInstance().getHt().delete(prop); } } catch (Exception ex) { QLog.l().logger().error( "Ошибка при " + (save ? "сохранении" : "удалении") + " \n" + ex.toString() + "\n" + Arrays.toString(ex.getStackTrace())); status.setRollbackOnly(); return ex; } return null; }); } catch (TransactionException ex) { throw new ServerException( "Ошибка выполнения операции изменения данных в БД(JDBC). Возможно введенные вами параметры не могут быть сохранены.\n(" + ex.toString() + ")"); } if (res != null) { throw new ServerException( "Ошибка выполнения операции изменения данных в БД(JDBC). Возможно введенные вами параметры не могут быть сохранены.\n[" + res.getLocalizedMessage() + "]\n(" + res.toString() + ")\nSQL: "); } return prop; }
public void saveAllProperties() { final Collection<QProperty> col = new ArrayList<>(); properties.values().forEach(sec -> { col.addAll(sec.properties.values()); }); final Exception res; try { res = (Exception) Spring.getInstance().getTt().execute((TransactionStatus status) -> { try { Spring.getInstance().getHt().saveOrUpdateAll(col); } catch (Exception ex) { QLog.l().logger() .error("Ошибка при сохранении \n" + ex.toString() + "\n" + Arrays .toString(ex.getStackTrace())); status.setRollbackOnly(); return ex; } return null; }); } catch (TransactionException ex) { throw new ServerException( "Ошибка выполнения операции изменения данных в БД(JDBC). Возможно введенные вами параметры не могут быть сохранены.\n(" + ex.toString() + ")"); } if (res != null) { throw new ServerException( "Ошибка выполнения операции изменения данных в БД(JDBC). Возможно введенные вами параметры не могут быть сохранены.\n[" + res.getLocalizedMessage() + "]\n(" + res.toString() + ")\nSQL: "); } }
@Override public boolean canRegisterSynchronization() { try { return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE); } catch (SystemException ex) { throw new TransactionException("Could not determine JTA transaction status", ex); } }
@Override public void registerSynchronization(Synchronization synchronization) { if (this.transactionSynchronizationRegistry != null) { this.transactionSynchronizationRegistry.registerInterposedSynchronization(synchronization); } else { try { this.transactionManager.getTransaction().registerSynchronization(synchronization); } catch (Exception ex) { throw new TransactionException("Could not access JTA Transaction to register synchronization", ex); } } }