org.hibernate.TransactionException Java Examples

The following examples show how to use org.hibernate.TransactionException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JtaTransactionAdapterUserTransactionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void rollback() {
	try {
		if ( initiator ) {
			initiator = false;
			log.trace( "Calling UserTransaction#rollback" );
			userTransaction.rollback();
			log.trace( "Called UserTransaction#rollback" );
		}
		else {
			markRollbackOnly();
		}
	}
	catch (Exception e) {
		throw new TransactionException( "JTA UserTransaction#rollback failed", e );
	}
}
 
Example #2
Source File: CMTTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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);
}
 
Example #3
Source File: CMTTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;

}
 
Example #4
Source File: CMTTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;

}
 
Example #5
Source File: CMTTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
		}
	}
 
Example #6
Source File: CMTTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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);
		}
	}
 
Example #7
Source File: CMTTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
		}
	}
 
Example #8
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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");
}
 
Example #9
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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);
		}
	}
 
Example #10
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
		}
	}
 
Example #11
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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;
		}
	}
 
Example #12
Source File: JtaTransactionAdapterUserTransactionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void commit() {
	try {
		if ( initiator ) {
			initiator = false;
			log.trace( "Calling UserTransaction#commit" );
			userTransaction.commit();
			log.trace( "Called UserTransaction#commit" );
		}
		else {
			log.trace( "Skipping TransactionManager#commit due to not being initiator" );
		}
	}
	catch (Exception e) {
		throw new TransactionException( "JTA UserTransaction#commit failed", e );
	}
}
 
Example #13
Source File: JtaTransactionAdapterUserTransactionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void begin() {
	try {
		if ( getStatus() == TransactionStatus.NOT_ACTIVE ) {
			log.trace( "Calling UserTransaction#begin" );
			userTransaction.begin();
			initiator = true;
			log.trace( "Called UserTransaction#begin" );
		}
		else {
			log.trace( "Skipping TransactionManager#begin due to already active transaction" );
		}
	}
	catch (Exception e) {
		throw new TransactionException( "JTA UserTransaction#begin failed", e );
	}
}
 
Example #14
Source File: HibernateExceptionUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenTxnMarkedRollbackOnly_whenCommitted_thenTransactionException() {
    thrown.expect(isA(TransactionException.class));

    Session session = null;
    Transaction transaction = null;
    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();

        Product product1 = new Product();
        product1.setId(15);
        product1.setName("Product1");
        session.save(product1);
        transaction.setRollbackOnly();

        transaction.commit();
    } catch (Exception e) {
        rollbackTransactionQuietly(transaction);
        throw (e);
    } finally {
        closeSessionQuietly(session);
    }
}
 
Example #15
Source File: JtaTransactionAdapterTransactionManagerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void rollback() {
	try {
		if ( initiator ) {
			initiator = false;
			log.trace( "Calling TransactionManager#rollback" );
			transactionManager.rollback();
			log.trace( "Called TransactionManager#rollback" );
		}
		else {
			markRollbackOnly();
		}
	}
	catch (Exception e) {
		throw new TransactionException( "JTA TransactionManager#rollback failed", e );
	}
}
 
Example #16
Source File: TestCaseHelper.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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();

}
 
Example #17
Source File: JtaTransactionAdapterTransactionManagerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void commit() {
	try {
		if ( initiator ) {
			initiator = false;
			log.trace( "Calling TransactionManager#commit" );
			transactionManager.commit();
			log.trace( "Called TransactionManager#commit" );
		}
		else {
			log.trace( "Skipping TransactionManager#commit due to not being initiator" );
		}
	}
	catch (Exception e) {
		throw new TransactionException( "JTA TransactionManager#commit failed", e );
	}
}
 
Example #18
Source File: JtaTransactionAdapterTransactionManagerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void begin() {
	try {
		if ( getStatus() == TransactionStatus.NOT_ACTIVE ) {
			log.trace( "Calling TransactionManager#begin" );
			transactionManager.begin();
			initiator = true;
			log.trace( "Called TransactionManager#begin" );
		}
		else {
			log.trace( "Skipping TransactionManager#begin due to already active transaction" );
		}
	}
	catch (Exception e) {
		throw new TransactionException( "JTA TransactionManager#begin failed", e );
	}
}
 
Example #19
Source File: TestCaseHelper.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #20
Source File: JDBCTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void rollback() throws HibernateException {

		if (!begun && !commitFailed) {
			throw new TransactionException("Transaction not successfully started");
		}

		log.debug("rollback");

		if (!commitFailed) {

			/*notifyLocalSynchsBeforeTransactionCompletion();
			if ( callback ) {
				jdbcContext.notifyLocalSynchsBeforeTransactionCompletion( this );
			}*/

			try {
				rollbackAndResetAutoCommit();
				log.debug("rolled back JDBC Connection");
				rolledBack = true;
				notifyLocalSynchsAfterTransactionCompletion(Status.STATUS_ROLLEDBACK);
			}
			catch (SQLException e) {
				log.error("JDBC rollback failed", e);
				notifyLocalSynchsAfterTransactionCompletion(Status.STATUS_UNKNOWN);
				throw new TransactionException("JDBC rollback failed", e);
			}
			finally {
				if ( callback ) {
					jdbcContext.afterTransactionCompletion( false, this );
				}
				closeIfRequired();
			}
		}
	}
 
Example #21
Source File: NestedTransactionFactoryTest.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
public void aTestNesting() throws HibernateException {
    //System.out.println("XXX BEGIN testNesting");
    Session session = HibernateFactory.getSession();
    try {
        //System.out.println("XXX beginTransaction1");
        session.beginTransaction();
        //System.out.println("XXX END testNesting, fail");
        fail("Created nested transaction, which is verboten");
    }
    catch (TransactionException e) {
        // Expected
        //System.out.println("XXX expected transaction");
    }
    //System.out.println("XXX END testNesting");
}
 
Example #22
Source File: JTAHelper.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Return true if a JTA transaction is in progress
 * and false in *every* other cases (including in a JDBC transaction).
 */
public static boolean isTransactionInProgress(SessionFactoryImplementor factory) {
	TransactionManager tm = factory.getTransactionManager();
	try {
		return tm != null && isTransactionInProgress( tm.getTransaction() );
	}
	catch (SystemException se) {
		throw new TransactionException( "could not obtain JTA Transaction", se );
	}
}
 
Example #23
Source File: AbstractBatcher.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void setTimeout(PreparedStatement result) throws SQLException {
	if ( isTransactionTimeoutSet ) {
		int timeout = (int) ( transactionTimeout - ( System.currentTimeMillis() / 1000 ) );
		if (timeout<=0) {
			throw new TransactionException("transaction timeout expired");
		}
		else {
			result.setQueryTimeout(timeout);
		}
	}
}
 
Example #24
Source File: CMTTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void registerSynchronization(Synchronization sync) throws HibernateException {
	try {
		getTransaction().registerSynchronization(sync);
	}
	catch (Exception e) {
		throw new TransactionException("Could not register synchronization", e);
	}
}
 
Example #25
Source File: CMTTransactionFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public boolean isTransactionInProgress(
		JDBCContext jdbcContext,
        Context transactionContext,
        Transaction transaction) {
	try {
		return JTAHelper.isTransactionInProgress(
				transactionContext.getFactory().getTransactionManager().getTransaction()
		);
	}
	catch( SystemException se ) {
		throw new TransactionException( "Unable to check transaction status", se );
	}

}
 
Example #26
Source File: CacheSynchronization.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void beforeCompletion() {
	log.trace("transaction before completion callback");

	boolean flush;
	try {
		flush = !ctx.isFlushModeNever() &&
		        ctx.isFlushBeforeCompletionEnabled() && 
		        !JTAHelper.isRollback( transaction.getStatus() ); 
				//actually, this last test is probably unnecessary, since 
				//beforeCompletion() doesn't get called during rollback
	}
	catch (SystemException se) {
		log.error("could not determine transaction status", se);
		setRollbackOnly();
		throw new TransactionException("could not determine transaction status in beforeCompletion()", se);
	}
	
	try {
		if (flush) {
			log.trace("automatically flushing session");
			ctx.managedFlush();
		}
	}
	catch (RuntimeException re) {
		setRollbackOnly();
		throw re;
	}
	finally {
		jdbcContext.beforeTransactionCompletion(hibernateTransaction);
	}
}
 
Example #27
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setTimeout(int seconds) {
	try {
		ut.setTransactionTimeout(seconds);
	}
	catch (SystemException se) {
		throw new TransactionException("could not set transaction timeout", se);
	}
}
 
Example #28
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void afterCommitRollback() throws TransactionException {
	
	begun = false;

	if (callback) { // this method is a noop if there is a Synchronization!

		if (!newTransaction) {
			log.warn("You should set hibernate.transaction.manager_lookup_class if cache is enabled");
		}
		int status=NULL;
		try {
			status = ut.getStatus();
		}
		catch (Exception e) {
			log.error("Could not determine transaction status after commit", e);
			throw new TransactionException("Could not determine transaction status after commit", e);
		}
		finally {
			/*if (status!=Status.STATUS_COMMITTED && status!=Status.STATUS_ROLLEDBACK) {
				log.warn("Transaction not complete - you should set hibernate.transaction.manager_lookup_class if cache is enabled");
				//throw exception??
			}*/
			jdbcContext.afterTransactionCompletion(status==Status.STATUS_COMMITTED, this);
		}

	}
}
 
Example #29
Source File: JDBCTransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void commit() throws HibernateException {
	if (!begun) {
		throw new TransactionException("Transaction not successfully started");
	}

	log.debug("commit");

	if ( !transactionContext.isFlushModeNever() && callback ) {
		transactionContext.managedFlush(); //if an exception occurs during flush, user must call rollback()
	}

	notifyLocalSynchsBeforeTransactionCompletion();
	if ( callback ) {
		jdbcContext.beforeTransactionCompletion( this );
	}

	try {
		commitAndResetAutoCommit();
		log.debug("committed JDBC Connection");
		committed = true;
		if ( callback ) {
			jdbcContext.afterTransactionCompletion( true, this );
		}
		notifyLocalSynchsAfterTransactionCompletion( Status.STATUS_COMMITTED );
	}
	catch (SQLException e) {
		log.error("JDBC commit failed", e);
		commitFailed = true;
		if ( callback ) {
			jdbcContext.afterTransactionCompletion( false, this );
		}
		notifyLocalSynchsAfterTransactionCompletion( Status.STATUS_UNKNOWN );
		throw new TransactionException("JDBC commit failed", e);
	}
	finally {
		closeIfRequired();
	}
}
 
Example #30
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void registerSynchronization(Synchronization sync) throws HibernateException {
	if (getTransactionManager()==null) {
		throw new IllegalStateException("JTA TransactionManager not available");
	}
	else {
		try {
			getTransactionManager().getTransaction().registerSynchronization(sync);
		}
		catch (Exception e) {
			throw new TransactionException("could not register synchronization", e);
		}
	}
}