Search in sources :

Example 46 with TransactionManager

use of javax.transaction.TransactionManager in project wildfly by wildfly.

the class StatefulBMTInterceptor method checkBadStateful.

private void checkBadStateful() {
    int status = Status.STATUS_NO_TRANSACTION;
    TransactionManager tm = getComponent().getTransactionManager();
    try {
        status = tm.getStatus();
    } catch (SystemException ex) {
        EjbLogger.ROOT_LOGGER.failedToGetStatus(ex);
    }
    switch(status) {
        case Status.STATUS_COMMITTING:
        case Status.STATUS_MARKED_ROLLBACK:
        case Status.STATUS_PREPARING:
        case Status.STATUS_ROLLING_BACK:
            try {
                tm.rollback();
            } catch (Exception ex) {
                EjbLogger.ROOT_LOGGER.failedToRollback(ex);
            }
            EjbLogger.ROOT_LOGGER.transactionNotComplete(getComponent().getComponentName(), statusAsString(status));
    }
}
Also used : SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) SystemException(javax.transaction.SystemException)

Example 47 with TransactionManager

use of javax.transaction.TransactionManager in project wildfly by wildfly.

the class EJBComponent method setRollbackOnly.

public void setRollbackOnly() throws IllegalStateException {
    if (isBeanManagedTransaction()) {
        throw EjbLogger.ROOT_LOGGER.failToCallSetRollbackOnlyOnNoneCMB();
    }
    try {
        // get the transaction manager
        TransactionManager tm = getTransactionManager();
        // check if there's a tx in progress. If not, then it's an error to call setRollbackOnly()
        if (tm.getTransaction() == null) {
            throw EjbLogger.ROOT_LOGGER.failToCallSetRollbackOnlyWithNoTx();
        }
        // set rollback
        tm.setRollbackOnly();
    } catch (SystemException se) {
        EjbLogger.ROOT_LOGGER.setRollbackOnlyFailed(se);
    }
}
Also used : SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager)

Example 48 with TransactionManager

use of javax.transaction.TransactionManager in project wildfly by wildfly.

the class EJBComponent method getRollbackOnly.

public boolean getRollbackOnly() throws IllegalStateException {
    if (isBeanManagedTransaction()) {
        throw EjbLogger.ROOT_LOGGER.failToCallgetRollbackOnly();
    }
    try {
        TransactionManager tm = this.getTransactionManager();
        // The getRollbackOnly method should be used only in the context of a transaction.
        if (tm.getTransaction() == null) {
            throw EjbLogger.ROOT_LOGGER.failToCallgetRollbackOnlyOnNoneTransaction();
        }
        // EJBTHREE-805, consider an asynchronous rollback due to timeout
        // This is counter to EJB 3.1 where an asynchronous call does not inherit the transaction context!
        int status = tm.getStatus();
        EjbLogger.ROOT_LOGGER.tracef("Current transaction status is %d", status);
        switch(status) {
            case Status.STATUS_COMMITTED:
            case Status.STATUS_ROLLEDBACK:
                throw EjbLogger.ROOT_LOGGER.failToCallgetRollbackOnlyAfterTxcompleted();
            case Status.STATUS_MARKED_ROLLBACK:
            case Status.STATUS_ROLLING_BACK:
                return true;
        }
        return false;
    } catch (SystemException se) {
        EjbLogger.ROOT_LOGGER.getTxManagerStatusFailed(se);
        return true;
    }
}
Also used : SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager) ControlPoint(org.wildfly.extension.requestcontroller.ControlPoint)

Example 49 with TransactionManager

use of javax.transaction.TransactionManager in project tomee by apache.

the class StatefulContainerTest method testBusinessLocalInterfaceInTx.

public void testBusinessLocalInterfaceInTx() throws Exception {
    final TransactionManager transactionManager = SystemInstance.get().getComponent(TransactionManager.class);
    transactionManager.begin();
    try {
        testBusinessLocalInterface(inTxExpectedLifecycle);
    } finally {
        transactionManager.commit();
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager)

Example 50 with TransactionManager

use of javax.transaction.TransactionManager in project tomee by apache.

the class ManagedConnectionBehaviorTest method run.

@Test
public void run() throws Exception {
    final GeronimoTransactionManager geronimoTransactionManager = new GeronimoTransactionManager((int) TimeUnit.MINUTES.toMillis(10));
    final TransactionManager mgr = new TransactionManagerWrapper(geronimoTransactionManager, "ManagedConnectionBehaviorTest", new GeronimoTransactionManagerFactory.GeronimoXAResourceWrapper());
    final MyDs myDs = new MyDs();
    final DataSource ds = new ManagedDataSource(myDs, geronimoTransactionManager, geronimoTransactionManager);
    {
        // no tx
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        connection.close();
        assertTrue(myDs.connections.iterator().next().closed);
        myDs.connections.clear();
    }
    {
        // tx
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        mgr.commit();
        assertTrue(myDs.connections.iterator().next().closed);
        assertTrue(myDs.connections.iterator().next().commit);
        assertFalse(myDs.connections.iterator().next().rollback);
        myDs.connections.clear();
    }
    {
        // tx already init
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        for (int i = 0; i < 5; i++) {
            // here the connection is already created, ensure we dont leak other connections
            connection.createBlob();
        }
        assertEquals(1, myDs.connections.size());
        mgr.commit();
        assertTrue(myDs.connections.iterator().next().closed);
        assertTrue(myDs.connections.iterator().next().commit);
        assertFalse(myDs.connections.iterator().next().rollback);
        myDs.connections.clear();
    }
    {
        // multiple tx
        mgr.begin();
        final Connection connection = ds.getConnection();
        // not yet needed
        assertTrue(myDs.connections.isEmpty());
        // just to call something
        connection.createBlob();
        assertFalse(myDs.connections.iterator().next().closed);
        final Transaction previous = mgr.suspend();
        mgr.begin();
        final Connection connection2 = ds.getConnection();
        connection2.createBlob();
        assertEquals(2, myDs.connections.size());
        mgr.commit();
        mgr.resume(previous);
        mgr.commit();
        final Iterator<MyConn> iterator = myDs.connections.iterator();
        final MyConn first = iterator.next();
        assertTrue(first.closed);
        assertTrue(first.commit);
        assertTrue(myDs.connections.iterator().next().commit);
        myDs.connections.clear();
    }
}
Also used : Transaction(javax.transaction.Transaction) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionManagerWrapper(org.apache.openejb.resource.TransactionManagerWrapper) Connection(java.sql.Connection) Iterator(java.util.Iterator) GeronimoTransactionManagerFactory(org.apache.openejb.resource.GeronimoTransactionManagerFactory) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) DataSource(javax.sql.DataSource) ManagedDataSource(org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource) Test(org.junit.Test)

Aggregations

TransactionManager (javax.transaction.TransactionManager)110 Test (org.junit.Test)40 Transaction (javax.transaction.Transaction)24 SystemException (javax.transaction.SystemException)22 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)15 UserTransaction (javax.transaction.UserTransaction)14 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Method (java.lang.reflect.Method)7 EntityManager (javax.persistence.EntityManager)7 NotSupportedException (javax.transaction.NotSupportedException)7 RollbackException (javax.transaction.RollbackException)7 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)6 TestForIssue (org.hibernate.testing.TestForIssue)6 IOException (java.io.IOException)5 InitialContext (javax.naming.InitialContext)5 DataSource (javax.sql.DataSource)5 JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)5