Search in sources :

Example 1 with TransactionDefinition

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

the class DefaultJpaDialectTests method testDefaultBeginTransaction.

@Test
public void testDefaultBeginTransaction() throws Exception {
    TransactionDefinition definition = new DefaultTransactionDefinition();
    EntityManager entityManager = mock(EntityManager.class);
    EntityTransaction entityTx = mock(EntityTransaction.class);
    given(entityManager.getTransaction()).willReturn(entityTx);
    dialect.beginTransaction(entityManager, definition);
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.Test)

Example 2 with TransactionDefinition

use of org.springframework.transaction.TransactionDefinition in project ignite by apache.

the class CacheSpringStoreSessionListener method onSessionStart.

/** {@inheritDoc} */
@Override
public void onSessionStart(CacheStoreSession ses) {
    if (ses.isWithinTransaction() && ses.attachment() == null) {
        try {
            TransactionDefinition def = definition(ses.transaction(), ses.cacheName());
            ses.attach(txMgr.getTransaction(def));
        } catch (TransactionException e) {
            throw new CacheWriterException("Failed to start store session [tx=" + ses.transaction() + ']', e);
        }
    }
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionException(org.springframework.transaction.TransactionException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 3 with TransactionDefinition

use of org.springframework.transaction.TransactionDefinition in project spring-boot by spring-projects.

the class SpringTransactionProvider method begin.

@Override
public void begin(TransactionContext context) {
    TransactionDefinition definition = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_NESTED);
    TransactionStatus status = this.transactionManager.getTransaction(definition);
    context.transaction(new SpringTransaction(status));
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 4 with TransactionDefinition

use of org.springframework.transaction.TransactionDefinition 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;
    assertTrue("Age should not be " + testBean.getAge(), testBean.getAge() == 666);
    // Expect no methods
    verifyZeroInteractions(ptm);
    // Install facade expecting a call
    final TransactionStatus ts = mock(TransactionStatus.class);
    ptm = new PlatformTransactionManager() {

        private boolean invoked;

        @Override
        public TransactionStatus getTransaction(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 {
            assertTrue(status == ts);
        }

        @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);
    assertTrue(testBean.getAge() == age);
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) TransactionException(org.springframework.transaction.TransactionException) TransactionStatus(org.springframework.transaction.TransactionStatus) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 5 with TransactionDefinition

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

the class TransactionAttributeSourceTests method matchAlwaysTransactionAttributeSourceWithNulls.

@Test
public void matchAlwaysTransactionAttributeSourceWithNulls() throws Exception {
    MatchAlwaysTransactionAttributeSource tas = new MatchAlwaysTransactionAttributeSource();
    TransactionDefinition definition = tas.getTransactionAttribute(null, null);
    assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, definition.getPropagationBehavior());
    assertEquals(TransactionDefinition.ISOLATION_DEFAULT, definition.getIsolationLevel());
    assertEquals(TransactionDefinition.TIMEOUT_DEFAULT, definition.getTimeout());
    assertFalse(definition.isReadOnly());
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) Test(org.junit.Test)

Aggregations

TransactionDefinition (org.springframework.transaction.TransactionDefinition)5 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)3 Test (org.junit.Test)2 TransactionException (org.springframework.transaction.TransactionException)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 CacheWriterException (javax.cache.integration.CacheWriterException)1 EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)1