Search in sources :

Example 11 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project camel by apache.

the class JpaPollingConsumer method receive.

@Override
public Exchange receive() {
    // resolve the entity manager before evaluating the expression
    final EntityManager entityManager = getTargetEntityManager(null, entityManagerFactory, getEndpoint().isUsePassedInEntityManager(), getEndpoint().isSharedEntityManager(), true);
    Object out = transactionTemplate.execute(new TransactionCallback<Object>() {

        public Object doInTransaction(TransactionStatus status) {
            if (getEndpoint().isJoinTransaction()) {
                entityManager.joinTransaction();
            }
            Query query = getQueryFactory().createQuery(entityManager);
            configureParameters(query);
            if (getEndpoint().isConsumeLockEntity()) {
                query.setLockMode(getLockModeType());
            }
            LOG.trace("Created query {}", query);
            Object answer;
            try {
                List<?> results = query.getResultList();
                if (results != null && results.size() == 1) {
                    // we only have 1 entity so return that
                    answer = results.get(0);
                } else {
                    // we have more data so return a list
                    answer = results;
                }
                // commit
                LOG.debug("Flushing EntityManager");
                entityManager.flush();
                // must clear after flush
                entityManager.clear();
            } catch (PersistenceException e) {
                LOG.info("Disposing EntityManager {} on {} due to coming transaction rollback", entityManager, this);
                entityManager.close();
                throw e;
            }
            return answer;
        }
    });
    Exchange exchange = createExchange(out, entityManager);
    exchange.getIn().setBody(out);
    return exchange;
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) JpaHelper.getTargetEntityManager(org.apache.camel.component.jpa.JpaHelper.getTargetEntityManager) Query(javax.persistence.Query) PersistenceException(javax.persistence.PersistenceException) TransactionStatus(org.springframework.transaction.TransactionStatus) List(java.util.List)

Example 12 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project camel by apache.

the class JpaUseMergeTest method produceExistingEntity.

@Test
public void produceExistingEntity() throws Exception {
    setUp("jpa://" + Customer.class.getName() + "?usePersist=false");
    final Customer customer = createDefaultCustomer();
    transactionTemplate.execute(new TransactionCallback<Object>() {

        public Object doInTransaction(TransactionStatus status) {
            entityManager.joinTransaction();
            entityManager.persist(customer);
            entityManager.flush();
            return null;
        }
    });
    assertEntitiesInDatabase(1, Customer.class.getName());
    assertEntitiesInDatabase(1, Address.class.getName());
    // do detach the persisted entity first before modifying it as we intend to merge it later on below
    entityManager.detach(customer);
    customer.setName("Max Mustermann");
    customer.getAddress().setAddressLine1("Musterstr. 1");
    customer.getAddress().setAddressLine2("11111 Enterhausen");
    Customer receivedCustomer = template.requestBody(endpoint, customer, Customer.class);
    assertEquals(customer.getName(), receivedCustomer.getName());
    assertNotNull(receivedCustomer.getId());
    assertEquals(customer.getAddress().getAddressLine1(), receivedCustomer.getAddress().getAddressLine1());
    assertEquals(customer.getAddress().getAddressLine2(), receivedCustomer.getAddress().getAddressLine2());
    assertNotNull(receivedCustomer.getAddress().getId());
    List<?> results = entityManager.createQuery("select o from " + Customer.class.getName() + " o").getResultList();
    assertEquals(1, results.size());
    Customer persistedCustomer = (Customer) results.get(0);
    assertEquals(receivedCustomer.getName(), persistedCustomer.getName());
    assertEquals(receivedCustomer.getId(), persistedCustomer.getId());
    assertEquals(receivedCustomer.getAddress().getAddressLine1(), persistedCustomer.getAddress().getAddressLine1());
    assertEquals(receivedCustomer.getAddress().getAddressLine2(), persistedCustomer.getAddress().getAddressLine2());
    assertEquals(receivedCustomer.getAddress().getId(), persistedCustomer.getAddress().getId());
}
Also used : Address(org.apache.camel.examples.Address) Customer(org.apache.camel.examples.Customer) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 13 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project camel by apache.

the class MultipleProcessesTest method sendBMessages.

protected void sendBMessages() {
    TransactionTemplate transaction = getMandatoryBean(TransactionTemplate.class, "transactionTemplate");
    transaction.execute(new TransactionCallbackWithoutResult() {

        protected void doInTransactionWithoutResult(TransactionStatus status) {
            template.sendBody("seda:b", "<hello id='123'>A</hello>");
            template.sendBody("seda:b", "<hello id='125'>C</hello>");
        }
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 14 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project camel by apache.

the class MultipleProcessesTest method sendAMessages.

protected void sendAMessages() {
    TransactionTemplate transaction = getMandatoryBean(TransactionTemplate.class, "transactionTemplate");
    transaction.execute(new TransactionCallbackWithoutResult() {

        protected void doInTransactionWithoutResult(TransactionStatus status) {
            template.sendBody("seda:a", "<hello id='123'>A</hello>");
            template.sendBody("seda:a", "<hello id='124'>B</hello>");
            template.sendBody("seda:a", "<hello id='125'>C</hello>");
        }
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 15 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project jOOQ by jOOQ.

the class SpringTransactionProvider method begin.

@Override
public void begin(TransactionContext ctx) {
    log.info("Begin transaction");
    // This TransactionProvider behaves like jOOQ's DefaultTransactionProvider,
    // which supports nested transactions using Savepoints
    TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition(PROPAGATION_NESTED));
    ctx.transaction(new SpringTransaction(tx));
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus)

Aggregations

TransactionStatus (org.springframework.transaction.TransactionStatus)359 Test (org.junit.Test)186 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)154 TransactionCallback (org.springframework.transaction.support.TransactionCallback)91 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)72 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)32 List (java.util.List)28 ArrayList (java.util.ArrayList)27 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)21 Date (java.util.Date)18 PreparedStatement (java.sql.PreparedStatement)17 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)15 OnmsNode (org.opennms.netmgt.model.OnmsNode)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)12 EntityManager (javax.persistence.EntityManager)11 DataSource (javax.sql.DataSource)11