Search in sources :

Example 1 with TransactionContext

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

the class TxContextBindingConnection method getDelegate.

@Override
protected final Connection getDelegate() {
    TransactionContext txContext = txControl.getCurrentContext();
    if (txContext == null) {
        throw new TransactionException("The resource " + provider + " cannot be accessed outside of an active Transaction Context");
    }
    Connection existing = (Connection) txContext.getScopedValue(resourceId);
    if (existing != null) {
        return existing;
    }
    Connection toReturn;
    Connection toClose;
    try {
        if (txContext.getTransactionStatus() == NO_TRANSACTION) {
            toClose = provider.getConnection();
            toReturn = new ScopedConnectionWrapper(toClose);
        } else if (txContext.supportsLocal()) {
            toClose = provider.getConnection();
            toReturn = new TxConnectionWrapper(toClose);
            txContext.registerLocalResource(getLocalResource(toClose));
        } else {
            throw new TransactionException("There is a transaction active, but it does not support local participants");
        }
    } catch (Exception sqle) {
        throw new TransactionException("There was a problem getting hold of a database connection", sqle);
    }
    txContext.postCompletion(x -> {
        try {
            toClose.close();
        } catch (SQLException sqle) {
        }
    });
    txContext.putScopedValue(resourceId, toReturn);
    return toReturn;
}
Also used : TransactionException(org.osgi.service.transaction.control.TransactionException) SQLException(java.sql.SQLException) TransactionContext(org.osgi.service.transaction.control.TransactionContext) TxConnectionWrapper(org.apache.aries.tx.control.jdbc.common.impl.TxConnectionWrapper) Connection(java.sql.Connection) ScopedConnectionWrapper(org.apache.aries.tx.control.jdbc.common.impl.ScopedConnectionWrapper) SQLException(java.sql.SQLException) TransactionException(org.osgi.service.transaction.control.TransactionException)

Example 2 with TransactionContext

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

the class TxContextBindingEntityManager 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;
    }
    EntityManager toReturn;
    EntityManager toClose;
    try {
        if (txContext.getTransactionStatus() == NO_TRANSACTION) {
            toClose = provider.createEntityManager();
            toReturn = new ScopedEntityManagerWrapper(toClose);
        } else if (txContext.supportsLocal()) {
            toClose = provider.createEntityManager();
            toReturn = new TxEntityManagerWrapper(toClose);
            txContext.registerLocalResource(getLocalResource(toClose));
            toClose.getTransaction().begin();
        } else {
            throw new TransactionException("There is a transaction active, but it does not support local participants");
        }
    } catch (Exception sqle) {
        throw new TransactionException("There was a problem getting hold of a database connection", sqle);
    }
    txContext.postCompletion(x -> {
        try {
            toClose.close();
        } catch (PersistenceException sqle) {
        }
    });
    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) PersistenceException(javax.persistence.PersistenceException) PersistenceException(javax.persistence.PersistenceException) TransactionException(org.osgi.service.transaction.control.TransactionException)

Example 3 with TransactionContext

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

the class TransactionControlStatusTest method testInheritNotSupportedRequired.

@Test
public void testInheritNotSupportedRequired() {
    txControl.notSupported(() -> {
        TransactionContext currentContext = txControl.getCurrentContext();
        txControl.required(() -> {
            assertNotSame(currentContext, txControl.getCurrentContext());
            return null;
        });
        assertSame(currentContext, txControl.getCurrentContext());
        return null;
    });
}
Also used : TransactionContext(org.osgi.service.transaction.control.TransactionContext) Test(org.junit.Test)

Example 4 with TransactionContext

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

the class OpenJPATxControlPlatform method registerSynchronization.

@Override
public void registerSynchronization(Synchronization synch) throws IllegalStateException, RollbackException, SystemException {
    TransactionContext currentContext = getTxControl().getCurrentContext();
    currentContext.preCompletion(synch::beforeCompletion);
    currentContext.postCompletion(status -> synch.afterCompletion(toIntStatus(status)));
}
Also used : TransactionContext(org.osgi.service.transaction.control.TransactionContext)

Example 5 with TransactionContext

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

the class TransactionControlStatusTest method testInheritNotSupportedRequired.

@Test
public void testInheritNotSupportedRequired() {
    txControl.notSupported(() -> {
        TransactionContext currentContext = txControl.getCurrentContext();
        txControl.required(() -> {
            assertNotSame(currentContext, txControl.getCurrentContext());
            return null;
        });
        assertSame(currentContext, txControl.getCurrentContext());
        return null;
    });
}
Also used : TransactionContext(org.osgi.service.transaction.control.TransactionContext) Test(org.junit.Test)

Aggregations

TransactionContext (org.osgi.service.transaction.control.TransactionContext)9 Test (org.junit.Test)4 TransactionException (org.osgi.service.transaction.control.TransactionException)4 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 EntityManager (javax.persistence.EntityManager)2 PersistenceException (javax.persistence.PersistenceException)2 ScopedConnectionWrapper (org.apache.aries.tx.control.jdbc.common.impl.ScopedConnectionWrapper)2 TxConnectionWrapper (org.apache.aries.tx.control.jdbc.common.impl.TxConnectionWrapper)2 ScopedEntityManagerWrapper (org.apache.aries.tx.control.jpa.common.impl.ScopedEntityManagerWrapper)2 TxEntityManagerWrapper (org.apache.aries.tx.control.jpa.common.impl.TxEntityManagerWrapper)2 TransactionControl (org.osgi.service.transaction.control.TransactionControl)1