Search in sources :

Example 21 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project ddd-cqrs-sample by BottegaIT.

the class AddSampleProductsOnStartup method addSampleProductsToRepo.

@PostConstruct
public void addSampleProductsToRepo() {
    TransactionStatus tx = transactionManager.getTransaction(new DefaultTransactionDefinition());
    for (int i = 1; i < 21; i++) {
        em.persist(product(String.format("Electronic Gizmo %02d", i), 0.99));
        em.persist(product(String.format("Cell Phone with 32GB flash memory %02d", i), 299.99));
        em.persist(food(String.format("Software Engineering Audiobook %02d", i), 17.50));
        em.persist(drug(String.format("PC Game including Zombies Part %02d", i), 39.89));
        em.persist(product(String.format("Tablet with Keyboard %02d", i), 459.99));
    }
    em.persist(new Client());
    transactionManager.commit(tx);
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus) Client(pl.com.bottega.erp.sales.domain.Client) PostConstruct(javax.annotation.PostConstruct)

Example 22 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project ignite by apache.

the class CacheSpringStoreSessionListener method definition.

/**
     * Gets DB transaction isolation level based on ongoing cache transaction isolation.
     *
     * @return DB transaction isolation.
     */
private TransactionDefinition definition(Transaction tx, String cacheName) {
    assert tx != null;
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setName("Ignite Tx [cache=" + (cacheName != null ? cacheName : "<default>") + ", id=" + tx.xid() + ']');
    def.setIsolationLevel(isolationLevel(tx.isolation()));
    long timeoutSec = (tx.timeout() + 500) / 1000;
    if (timeoutSec > 0 && timeoutSec < Integer.MAX_VALUE)
        def.setTimeout((int) timeoutSec);
    return def;
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition)

Example 23 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project grails-core by grails.

the class ChainedTransactionManagerTests method createAndRollbackTransaction.

private void createAndRollbackTransaction() {
    MultiTransactionStatus transaction = tm.getTransaction(new DefaultTransactionDefinition());
    tm.rollback(transaction);
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition)

Example 24 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project jOOQ by jOOQ.

the class TransactionTest method testExplicitTransactions.

@Test
public void testExplicitTransactions() {
    boolean rollback = false;
    TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition());
    try {
        // constraint violation exception
        for (int i = 0; i < 2; i++) dsl.insertInto(BOOK).set(BOOK.ID, 5).set(BOOK.AUTHOR_ID, 1).set(BOOK.TITLE, "Book 5").execute();
        Assert.fail();
    }// Upon the constraint violation, we explicitly roll back the transaction.
     catch (DataAccessException e) {
        txMgr.rollback(tx);
        rollback = true;
    }
    assertEquals(4, dsl.fetchCount(BOOK));
    assertTrue(rollback);
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 25 with DefaultTransactionDefinition

use of org.springframework.transaction.support.DefaultTransactionDefinition in project jOOQ by jOOQ.

the class TransactionalMethodInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    TransactionStatus transaction = transactionManager.getTransaction(transactionDefinition);
    try {
        Object result = invocation.proceed();
        try {
            if (transaction.isNewTransaction())
                transactionManager.commit(transaction);
        } catch (UnexpectedRollbackException ignore) {
        }
        return result;
    } catch (Exception e) {
        if (transaction.isNewTransaction())
            transactionManager.rollback(transaction);
        throw e;
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException)

Aggregations

DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)45 TransactionStatus (org.springframework.transaction.TransactionStatus)32 Test (org.junit.Test)28 TransactionCallback (org.springframework.transaction.support.TransactionCallback)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 Connection (javax.jms.Connection)7 ConnectionFactory (javax.jms.ConnectionFactory)7 Session (javax.jms.Session)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)7 UserTransaction (javax.transaction.UserTransaction)5 SessionCallback (org.springframework.jms.core.SessionCallback)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)4 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)3 UOWException (com.ibm.wsspi.uow.UOWException)2 DataAccessException (org.springframework.dao.DataAccessException)2 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)2 NestedTransactionNotSupportedException (org.springframework.transaction.NestedTransactionNotSupportedException)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2