Example usage for javax.persistence RollbackException RollbackException

List of usage examples for javax.persistence RollbackException RollbackException

Introduction

In this page you can find the example usage for javax.persistence RollbackException RollbackException.

Prototype

public RollbackException(Throwable cause) 

Source Link

Document

Constructs a new RollbackException exception with the specified cause.

Usage

From source file:org.castor.jpa.CastorEntityTransaction.java

/**
 * Commit the current transaction, writing any unflushed changes to the
 * database./*from  w w w . jav a2  s . co  m*/
 * 
 * @throws IllegalStateException
 *             if {@link #isActive()} is false.
 * @throws RollbackException
 *             if the commit fails.
 */
public void commit() {
    verifyActive();
    verifyEntityManagerIsOpen();

    // Verify whether transaction was marked as roll back only.
    if (getRollbackOnly()) {
        log.error("Could not commit changes. Entity transaction was marked for rollback only.");
        throw new RollbackException(
                "Could not commit changes. Entity transaction was marked for rollback only.");
    }

    try {

        // Commit changes.
        this.database.commit();
        // Set transaction inactive.
        this.active = false;
        // Discard transaction-scoped persistence context.
        this.entityManager.invalidatePersistenceContext();

    } catch (TransactionNotInProgressException e) {

        // Set transaction to roll back only.
        this.rollbackOnly = true;

        log.error("Could not commit changes. Castor transaction is inactive.", e);
        throw new RollbackException("Could not commit changes. Castor transaction is inactive.", e);
    } catch (TransactionAbortedException e) {

        // Set transaction to roll back only.
        this.rollbackOnly = true;

        log.error("Could not commit changes. Transaction was aborted.", e);
        throw new RollbackException("Could not commit changes. Transaction was aborted.", e);
    }
}