类org.hibernate.TransactionException源码实例Demo

下面列出了怎么用org.hibernate.TransactionException的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: uyuni   文件: TestCaseHelper.java
/**
 * 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();
}
 
@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 );
	}
}
 
@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 );
	}
}
 
@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 );
	}
}
 
@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 );
	}
}
 
@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 );
	}
}
 
@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 );
	}
}
 
源代码8 项目: cacheonix-core   文件: CMTTransaction.java
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);
}
 
源代码9 项目: cacheonix-core   文件: CMTTransaction.java
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;

}
 
源代码10 项目: cacheonix-core   文件: CMTTransaction.java
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;

}
 
源代码11 项目: cacheonix-core   文件: CMTTransaction.java
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;
		}
	}
 
源代码12 项目: cacheonix-core   文件: CMTTransaction.java
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);
		}
	}
 
源代码13 项目: cacheonix-core   文件: CMTTransaction.java
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;
		}
	}
 
源代码14 项目: cacheonix-core   文件: JTATransaction.java
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");
}
 
源代码15 项目: cacheonix-core   文件: JTATransaction.java
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);
		}
	}
 
源代码16 项目: cacheonix-core   文件: JTATransaction.java
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;
		}
	}
 
源代码17 项目: cacheonix-core   文件: JTATransaction.java
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;
		}
	}
 
源代码18 项目: spacewalk   文件: TestCaseHelper.java
/**
 * 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();

}
 
源代码19 项目: tutorials   文件: HibernateExceptionUnitTest.java
@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);
    }
}
 
@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);
		}
	}
}
 
@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);
		}
	}
}
 
源代码24 项目: lams   文件: ConfigurableJtaPlatform.java
public boolean canRegisterSynchronization() {
	try {
		return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE);
	}
	catch (SystemException ex) {
		throw new TransactionException("Could not determine JTA transaction status", ex);
	}
}
 
源代码25 项目: lams   文件: ConfigurableJtaPlatform.java
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);
		}
	}
}
 
源代码26 项目: lams   文件: ConfigurableJtaPlatform.java
@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);
	}
}
 
源代码27 项目: lams   文件: ConfigurableJtaPlatform.java
@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);
		}
	}
}
 
@Override
public TransactionStatus getStatus() {
	try {
		return StatusTranslator.translate( transactionManager.getStatus() );
	}
	catch (SystemException e) {
		throw new TransactionException( "JTA TransactionManager#getStatus failed", e );
	}
}
 
@Override
public void markRollbackOnly() {
	try {
		transactionManager.setRollbackOnly();
	}
	catch (SystemException e) {
		throw new TransactionException( "Could not set transaction to rollback only", e );
	}
}
 
@Override
public TransactionStatus getStatus() {
	try {
		return StatusTranslator.translate( userTransaction.getStatus() );
	}
	catch (SystemException e) {
		throw new TransactionException( "JTA TransactionManager#getStatus failed", e );
	}
}
 
 类所在包
 同包方法