Search in sources :

Example 56 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project BroadleafCommerce by BroadleafCommerce.

the class PersistenceServiceImpl method identifyTransactionManager.

@Override
public PlatformTransactionManager identifyTransactionManager(String className, TargetModeType targetModeType) {
    String cacheKey = buildManagerCacheKey(targetModeType.getType(), className);
    PlatformTransactionManager txManager = TRANSACTION_MANAGER_CACHE.get(cacheKey);
    if (txManager == null) {
        throw new RuntimeException("Unable to determine the PlatformTransactionManager for the following " + "targetModeType and class pair: " + cacheKey);
    }
    return txManager;
}
Also used : PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 57 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project BroadleafCommerce by BroadleafCommerce.

the class DynamicEntityRemoteService method inspect.

@Override
public PersistenceResponse inspect(final PersistencePackage persistencePackage) throws ServiceException {
    final PersistenceResponse[] response = new PersistenceResponse[1];
    try {
        PlatformTransactionManager transactionManager = identifyTransactionManager(persistencePackage);
        transUtil.runOptionalTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

            @Override
            public void execute() throws Throwable {
                response[0] = nonTransactionalInspect(persistencePackage);
            }
        }, RuntimeException.class, true, TransactionDefinition.PROPAGATION_REQUIRED, TransactionDefinition.ISOLATION_DEFAULT, true, transactionManager);
    } catch (RuntimeException e) {
        if (e.getCause() instanceof ServiceException) {
            throw (ServiceException) e.getCause();
        }
        throw e;
    }
    return response[0];
}
Also used : ServiceException(org.broadleafcommerce.common.exception.ServiceException) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 58 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project BroadleafCommerce by BroadleafCommerce.

the class DynamicEntityRemoteService method remove.

@Override
public PersistenceResponse remove(final PersistencePackage persistencePackage) throws ServiceException {
    final PersistenceResponse[] response = new PersistenceResponse[1];
    try {
        PlatformTransactionManager transactionManager = identifyTransactionManager(persistencePackage);
        transUtil.runTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

            @Override
            public void execute() throws Throwable {
                response[0] = nonTransactionalRemove(persistencePackage);
            }

            @Override
            public boolean shouldRetryOnTransactionLockAcquisitionFailure() {
                return super.shouldRetryOnTransactionLockAcquisitionFailure();
            }
        }, RuntimeException.class, transactionManager);
    } catch (RuntimeException e) {
        if (e.getCause() instanceof ServiceException) {
            throw (ServiceException) e.getCause();
        }
        throw e;
    }
    return response[0];
}
Also used : ServiceException(org.broadleafcommerce.common.exception.ServiceException) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) PersistenceResponse(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 59 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.

the class BeanFactoryTransactionTests method doTestGetsAreNotTransactional.

private void doTestGetsAreNotTransactional(final ITestBean testBean) {
    // Install facade
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    PlatformTransactionManagerFacade.delegate = ptm;
    assertThat(testBean.getAge() == 666).as("Age should not be " + testBean.getAge()).isTrue();
    // Expect no methods
    verifyNoInteractions(ptm);
    // Install facade expecting a call
    final TransactionStatus ts = mock(TransactionStatus.class);
    ptm = new PlatformTransactionManager() {

        private boolean invoked;

        @Override
        public TransactionStatus getTransaction(@Nullable TransactionDefinition def) throws TransactionException {
            if (invoked) {
                throw new IllegalStateException("getTransaction should not get invoked more than once");
            }
            invoked = true;
            if (!(def.getName().contains(DerivedTestBean.class.getName()) && def.getName().contains("setAge"))) {
                throw new IllegalStateException("transaction name should contain class and method name: " + def.getName());
            }
            return ts;
        }

        @Override
        public void commit(TransactionStatus status) throws TransactionException {
            assertThat(status == ts).isTrue();
        }

        @Override
        public void rollback(TransactionStatus status) throws TransactionException {
            throw new IllegalStateException("rollback should not get invoked");
        }
    };
    PlatformTransactionManagerFacade.delegate = ptm;
    // TODO same as old age to avoid ordering effect for now
    int age = 666;
    testBean.setAge(age);
    assertThat(testBean.getAge() == age).isTrue();
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) TransactionException(org.springframework.transaction.TransactionException) TransactionStatus(org.springframework.transaction.TransactionStatus) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 60 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.

the class AbstractTransactionAspectTests method transactionShouldSucceedWithNotNew.

/**
 * Check that a transaction is created and committed.
 */
@Test
public void transactionShouldSucceedWithNotNew() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(getNameMethod, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // expect a transaction
    given(ptm.getTransaction(txatt)).willReturn(status);
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    checkTransactionStatus(false);
    // verification!?
    itb.getName();
    checkTransactionStatus(false);
    verify(ptm).commit(status);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.jupiter.api.Test)

Aggregations

PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)74 Test (org.junit.jupiter.api.Test)24 TransactionStatus (org.springframework.transaction.TransactionStatus)15 Test (org.junit.Test)14 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 BeanFactory (org.springframework.beans.factory.BeanFactory)8 Method (java.lang.reflect.Method)7 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)7 TransactionException (org.springframework.transaction.TransactionException)7 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)7 StreamCapableTransactionalOperationAdapter (org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)6 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 DataSource (javax.sql.DataSource)5 ServiceException (org.broadleafcommerce.common.exception.ServiceException)5 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)5 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)5