Search in sources :

Example 1 with TransactionControl

use of org.osgi.service.transaction.control.TransactionControl in project aries by apache.

the class XATxContextBindingEntityManagerTest method testActiveTransactionWithPreviousCommonTxControl.

@Test
public void testActiveTransactionWithPreviousCommonTxControl() throws SQLException {
    TransactionControl previous = Mockito.mock(TransactionControl.class);
    Connection con = Mockito.mock(Connection.class, withSettings().extraInterfaces(XAConnection.class));
    Mockito.when(((XAConnection) con).getXAResource()).thenReturn(xaResource);
    Mockito.when(rawEm.unwrap(Connection.class)).thenReturn(con);
    setupActiveTransaction();
    commonTxControl.set(previous);
    em.isOpen();
    em.isOpen();
    Mockito.verify(rawEm, times(2)).isOpen();
    Mockito.verify(rawEm).joinTransaction();
    checkPostCompletion(previous);
}
Also used : TransactionControl(org.osgi.service.transaction.control.TransactionControl) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 2 with TransactionControl

use of org.osgi.service.transaction.control.TransactionControl in project aries by apache.

the class OpenJPATxControlPlatform method doNonTransactionalWork.

@Override
public void doNonTransactionalWork(Runnable arg0) throws NotSupportedException {
    TransactionControl transactionControl = getTxControl();
    transactionControl.notSupported(() -> {
        arg0.run();
        return null;
    });
}
Also used : TransactionControl(org.osgi.service.transaction.control.TransactionControl)

Example 3 with TransactionControl

use of org.osgi.service.transaction.control.TransactionControl in project aries by apache.

the class XATxContextBindingEntityManager method getRealEntityManager.

@Override
protected final EntityManager getRealEntityManager() {
    TransactionContext txContext = txControl.getCurrentContext();
    if (txContext == null) {
        throw new TransactionException("The resource " + provider + " cannot be accessed outside of an active Transaction Context");
    }
    EntityManager existing = (EntityManager) txContext.getScopedValue(resourceId);
    if (existing != null) {
        return existing;
    }
    TransactionControl previous = commonTxStore.get();
    commonTxStore.set(txControl);
    EntityManager toReturn;
    EntityManager toClose;
    try {
        if (txContext.getTransactionStatus() == NO_TRANSACTION) {
            toClose = provider.createEntityManager();
            toReturn = new ScopedEntityManagerWrapper(toClose);
        } else if (txContext.supportsXA()) {
            toClose = provider.createEntityManager();
            toReturn = new TxEntityManagerWrapper(toClose);
            toClose.joinTransaction();
        } else {
            throw new TransactionException("There is a transaction active, but it does not support local participants");
        }
    } catch (Exception sqle) {
        commonTxStore.set(previous);
        throw new TransactionException("There was a problem getting hold of a database connection", sqle);
    }
    txContext.postCompletion(x -> {
        try {
            toClose.close();
        } catch (PersistenceException sqle) {
        }
        commonTxStore.set(previous);
    });
    txContext.putScopedValue(resourceId, toReturn);
    return toReturn;
}
Also used : EntityManager(javax.persistence.EntityManager) ScopedEntityManagerWrapper(org.apache.aries.tx.control.jpa.common.impl.ScopedEntityManagerWrapper) TransactionException(org.osgi.service.transaction.control.TransactionException) TxEntityManagerWrapper(org.apache.aries.tx.control.jpa.common.impl.TxEntityManagerWrapper) TransactionContext(org.osgi.service.transaction.control.TransactionContext) TransactionControl(org.osgi.service.transaction.control.TransactionControl) PersistenceException(javax.persistence.PersistenceException) PersistenceException(javax.persistence.PersistenceException) TransactionException(org.osgi.service.transaction.control.TransactionException)

Aggregations

TransactionControl (org.osgi.service.transaction.control.TransactionControl)3 Connection (java.sql.Connection)1 EntityManager (javax.persistence.EntityManager)1 PersistenceException (javax.persistence.PersistenceException)1 XAConnection (javax.sql.XAConnection)1 ScopedEntityManagerWrapper (org.apache.aries.tx.control.jpa.common.impl.ScopedEntityManagerWrapper)1 TxEntityManagerWrapper (org.apache.aries.tx.control.jpa.common.impl.TxEntityManagerWrapper)1 Test (org.junit.Test)1 TransactionContext (org.osgi.service.transaction.control.TransactionContext)1 TransactionException (org.osgi.service.transaction.control.TransactionException)1