Example usage for org.springframework.transaction.support TransactionTemplate getTransactionManager

List of usage examples for org.springframework.transaction.support TransactionTemplate getTransactionManager

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionTemplate getTransactionManager.

Prototype

@Nullable
public PlatformTransactionManager getTransactionManager() 

Source Link

Document

Return the transaction management strategy to be used.

Usage

From source file:com.opensymphony.able.filter.TransactionServletFilter.java

public void doFilter(final ServletRequest request, final ServletResponse response,
        final FilterChain filterChain) throws IOException, ServletException {
    // TODO we could get clever and figure out what URIs are read only transactions etc
    TransactionTemplate transactionTemplate = (TransactionTemplate) context.getBean("transactionTemplate");
    transactionTemplate.setReadOnly(false);

    if (log.isDebugEnabled()) {
        log.debug("Starting a transaction");
    }/*from  w w  w  .  j  a  va  2 s . co  m*/

    PlatformTransactionManager transactionManager = transactionTemplate.getTransactionManager();
    TransactionStatus status = transactionManager.getTransaction(transactionTemplate);
    TransactionOutcome outcome = new TransactionOutcome(status, transactionTemplate);
    request.setAttribute(TRANSACTION_OUTCOME, outcome);

    Exception exception = null;
    try {
        filterChain.doFilter(request, response);
        status = transactionManager.getTransaction(transactionTemplate);
    } catch (Exception e) {
        exception = e;
        log.warn("Caught: " + e, e);
    }

    if (outcome.isRollbackOnly() || exception != null) {
        status.setRollbackOnly();
    }

    try {
        if (status.isRollbackOnly()) {
            log.debug("Rolling back transaction");
            transactionManager.rollback(status);
        } else {
            log.debug("Committing transaction");
            transactionManager.commit(status);
        }
    } catch (TransactionException e) {
        if (exception == null) {
            exception = e;
        } else {
            log.debug("Failed to rollback transaction: " + e, e);
        }
    }

    if (exception instanceof IOException) {
        throw (IOException) exception;
    } else if (exception instanceof ServletException) {
        throw (ServletException) exception;
    } else if (exception != null) {
        throw new ServletException(exception);
    }
}

From source file:org.jspresso.hrsample.backend.JspressoUnitOfWorkTest.java

/**
 * Tests the use of nested transactions.
 *///from  www. j a v a  2 s . c om
@Test
public void testNestedTransactions() {
    final HibernateBackendController hbc = (HibernateBackendController) getBackendController();
    final TransactionTemplate tt = hbc.getTransactionTemplate();

    Serializable empId = tt.execute(new TransactionCallback<Serializable>() {

        @Override
        public Serializable doInTransaction(TransactionStatus status) {
            TransactionTemplate nestedTT = new ControllerAwareTransactionTemplate(tt.getTransactionManager());
            nestedTT.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            Serializable id = nestedTT.execute(new TransactionCallback<Serializable>() {

                @Override
                public Serializable doInTransaction(TransactionStatus nestedStatus) {
                    DetachedCriteria empCrit = DetachedCriteria.forClass(Employee.class);
                    Employee emp = hbc.findFirstByCriteria(empCrit, null, Employee.class);
                    emp.setFirstName("Committed");
                    return emp.getId();
                }
            });
            // asserts that UOW is still active after the end of the nested transaction.
            assertTrue("UOW should still be active since outer TX is ongoing.", hbc.isUnitOfWorkActive());
            // forces rollback of outer TX.
            status.setRollbackOnly();
            return id;
        }
    });
    DetachedCriteria empById = DetachedCriteria.forClass(Employee.class);
    empById.add(Restrictions.eq(IEntity.ID, empId));
    Employee emp = hbc.findFirstByCriteria(empById, EMergeMode.MERGE_CLEAN_EAGER, Employee.class);
    assertTrue("Inner transaction should have been committed", "Committed".equals(emp.getFirstName()));

    Serializable emp2Id = tt.execute(new TransactionCallback<Serializable>() {

        @Override
        public Serializable doInTransaction(TransactionStatus status) {
            TransactionTemplate nestedTT = new ControllerAwareTransactionTemplate(tt.getTransactionManager());
            nestedTT.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
            Serializable id = nestedTT.execute(new TransactionCallback<Serializable>() {

                @Override
                public Serializable doInTransaction(TransactionStatus nestedStatus) {
                    DetachedCriteria empCrit = DetachedCriteria.forClass(Employee.class);
                    Employee emp2 = hbc.findFirstByCriteria(empCrit, null, Employee.class);
                    emp2.setFirstName("Rollbacked");
                    return emp2.getId();
                }
            });
            // forces rollback of outer TX.
            status.setRollbackOnly();
            return id;
        }
    });
    DetachedCriteria emp2ById = DetachedCriteria.forClass(Employee.class);
    emp2ById.add(Restrictions.eq(IEntity.ID, emp2Id));
    Employee emp2 = hbc.findFirstByCriteria(empById, EMergeMode.MERGE_CLEAN_EAGER, Employee.class);
    assertFalse("Inner transaction should have been rollbacked", "Rollbacked".equals(emp2.getFirstName()));

    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            City newCity = hbc.getEntityFactory().createEntityInstance(City.class);
            newCity.setName("Test City");

            TransactionTemplate nestedTT = new ControllerAwareTransactionTemplate(tt.getTransactionManager());
            nestedTT.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            String testZip = nestedTT.execute(new TransactionCallback<String>() {

                @Override
                public String doInTransaction(TransactionStatus nestedStatus) {
                    return "12345";
                }
            });
            newCity.setZip(testZip);
            hbc.registerForUpdate(newCity);
        }
    });

    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            final City randomCity = hbc.findFirstByCriteria(DetachedCriteria.forClass(City.class),
                    EMergeMode.MERGE_KEEP, City.class);
            TransactionTemplate nestedTT = new ControllerAwareTransactionTemplate(tt.getTransactionManager());
            nestedTT.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            nestedTT.execute(new TransactionCallbackWithoutResult() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus nestedStatus) {
                    DetachedCriteria cityById = DetachedCriteria.forClass(City.class);
                    cityById.add(Restrictions.eq(IEntity.ID, randomCity.getId()));
                    City innerRandomCity = (City) cityById.getExecutableCriteria(hbc.getHibernateSession())
                            .uniqueResult();
                    // If we reach this point without exception, there is no mix between the inner TX and the outer UOW.
                    // See bug #1118
                }
            });
        }
    });
}