Search in sources :

Example 1 with TransactionStatus

use of org.hibernate.resource.transaction.spi.TransactionStatus in project eap-additional-testsuite by jboss-set.

the class SFSB method createStudent.

// create student
public Student createStudent(String firstName, String lastName, String address, int id) {
    // setupConfig();
    Student student = new Student();
    student.setStudentId(id);
    student.setAddress(address);
    student.setFirstName(firstName);
    student.setLastName(lastName);
    try {
        Session session = sessionFactory.openSession();
        // join the current JTA transaction
        Transaction ormTransaction = session.beginTransaction();
        TransactionStatus status = ormTransaction.getStatus();
        if (status.isNotOneOf(TransactionStatus.ACTIVE)) {
            throw new RuntimeException("Hibernate Transaction is not active after joining Hibernate to JTA transaction: " + status.name());
        }
        session.save(student);
        // trans.commit();
        session.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failure while persisting student entity", e);
    }
    return student;
}
Also used : Transaction(org.hibernate.Transaction) TransactionStatus(org.hibernate.resource.transaction.spi.TransactionStatus) Session(org.hibernate.Session)

Example 2 with TransactionStatus

use of org.hibernate.resource.transaction.spi.TransactionStatus in project eap-additional-testsuite by jboss-set.

the class SFSB method getStudent.

// get student
public Student getStudent(int id) {
    Student student;
    try {
        Session session = sessionFactory.openSession();
        // join the current JTA transaction
        Transaction ormTransaction = session.beginTransaction();
        TransactionStatus status = ormTransaction.getStatus();
        if (status.isNotOneOf(TransactionStatus.ACTIVE)) {
            throw new RuntimeException("Hibernate Transaction is not active after joining Hibernate to JTA transaction: " + status.name());
        }
        student = session.load(Student.class, id);
        session.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Failure while loading student entity", e);
    }
    return student;
}
Also used : Transaction(org.hibernate.Transaction) TransactionStatus(org.hibernate.resource.transaction.spi.TransactionStatus) Session(org.hibernate.Session)

Example 3 with TransactionStatus

use of org.hibernate.resource.transaction.spi.TransactionStatus in project hibernate-orm by hibernate.

the class TransactionImpl method rollback.

@Override
public void rollback() {
    if (jpaCompliance.isJpaTransactionComplianceEnabled()) {
        if (!isActive()) {
            throw new IllegalStateException("JPA compliance dictates throwing IllegalStateException when #rollback " + "is called on non-active transaction");
        }
    }
    TransactionStatus status = getStatus();
    if (status == TransactionStatus.ROLLED_BACK || status == TransactionStatus.NOT_ACTIVE) {
        // Allow rollback() calls on completed transactions, just no-op.
        LOG.debug("rollback() called on an inactive transaction");
        return;
    }
    if (!status.canRollback()) {
        throw new TransactionException("Cannot rollback transaction in current status [" + status.name() + "]");
    }
    LOG.debug("rolling back");
    if (status != TransactionStatus.FAILED_COMMIT || allowFailedCommitToPhysicallyRollback()) {
        internalGetTransactionDriverControl().rollback();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) TransactionStatus(org.hibernate.resource.transaction.spi.TransactionStatus)

Aggregations

TransactionStatus (org.hibernate.resource.transaction.spi.TransactionStatus)3 Session (org.hibernate.Session)2 Transaction (org.hibernate.Transaction)2 TransactionException (org.hibernate.TransactionException)1