Search in sources :

Example 1 with MockXAResource

use of org.jboss.as.quickstarts.ejb.mock.MockXAResource in project quickstart by wildfly.

the class StatelessBean method successOnCall.

/**
 * Stateless remote ejb method to be called from the client side.
 *
 * @return information about the host that this EJB resides on
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String successOnCall() {
    log.debugf("Called '%s.successOnCall()' with transaction status %s", this.getClass().getName(), InfoUtils.getTransactionStatus());
    // transaction enlists XAResource #1
    try {
        tm.getTransaction().enlistResource(new MockXAResource());
    } catch (SystemException | RollbackException sre) {
        throw new IllegalStateException("Cannot enlist a " + MockXAResource.class.getName() + " to the current transaction", sre);
    }
    // transaction enlists XAResource #2
    em.persist(new CalleeUser("Bard", "The Bowman"));
    return InfoUtils.getHostInfo();
}
Also used : SystemException(javax.transaction.SystemException) MockXAResource(org.jboss.as.quickstarts.ejb.mock.MockXAResource) RollbackException(javax.transaction.RollbackException) CalleeUser(org.jboss.as.quickstarts.ejb.entity.CalleeUser) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 2 with MockXAResource

use of org.jboss.as.quickstarts.ejb.mock.MockXAResource in project quickstart by wildfly.

the class StatelessBean method failOnCall.

/**
 * Failure during the commit processing does not mean an exception is thrown by transaction manager processing.
 * The business method is marked as succesfully finished.
 * The uncommitted {@link javax.transaction.xa.XAResource}s will be committed later by periodic recovery.
 */
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String failOnCall() {
    log.debugf("Called '%s.failOnCall()' with transaction status %s", this.getClass().getName(), InfoUtils.getTransactionStatus());
    // transaction enlists XAResource #1 with forcing to fail
    try {
        tm.getTransaction().enlistResource(new MockXAResource(MockXAResource.TestAction.COMMIT_THROW_XAER_RMFAIL));
    } catch (SystemException | RollbackException sre) {
        throw new IllegalStateException("Cannot enlist a " + MockXAResource.class.getName() + " to the current transaction", sre);
    }
    // transaction enlists XAResource #2
    em.persist(new CalleeUser("Bard", "The Bowman"));
    return InfoUtils.getHostInfo();
}
Also used : SystemException(javax.transaction.SystemException) MockXAResource(org.jboss.as.quickstarts.ejb.mock.MockXAResource) RollbackException(javax.transaction.RollbackException) CalleeUser(org.jboss.as.quickstarts.ejb.entity.CalleeUser) TransactionAttribute(javax.ejb.TransactionAttribute)

Aggregations

TransactionAttribute (javax.ejb.TransactionAttribute)2 RollbackException (javax.transaction.RollbackException)2 SystemException (javax.transaction.SystemException)2 CalleeUser (org.jboss.as.quickstarts.ejb.entity.CalleeUser)2 MockXAResource (org.jboss.as.quickstarts.ejb.mock.MockXAResource)2