Programmatic transaction demarcation and implementation

Spring allows you to implement and demarcate transactions programmatically in the application by using the TransactionTemplate and a PlatformTransactionManager implementation directly. But declarative transaction management is highly recommended, because it provides a clean code and a very flexible configuration.

Let's see how to implement the transactions in the application programmatically:

    package com.packt.patterninspring.chapter8.bankapp.service; 
 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Service; 
    import org.springframework.transaction.PlatformTransactionManager; 
    import org.springframework.transaction.TransactionStatus; 
    import org.springframework.transaction.support.TransactionCallback; 
    import org.springframework.transaction.support.TransactionTemplate; 
 
    import com.packt.patterninspring.chapter8.bankapp.model.Account; 
    import com.packt.patterninspring.chapter8.bankapp.
repository.AccountRepository; @Service public class AccountServiceImpl implements AccountService { //single TransactionTemplate shared amongst all methods in this
instance private final TransactionTemplate transactionTemplate; @Autowired AccountRepository accountRepository; // use constructor-injection to supply the
PlatformTransactionManager public AccountServiceImpl(PlatformTransactionManager
transactionManager) { this.transactionTemplate = new
TransactionTemplate(transactionManager); } @Override public Double cheeckAccountBalance(Account account) { return transactionTemplate.execute(new
TransactionCallback<Double>() { // the code in this method executes in a transactional
context public Double doInTransaction(TransactionStatus status) { return accountRepository.checkAccountBalance(account); } }); } }

In the preceding application code, we have used TransactionTemplate explicitly to execute the application logic in a transactional context. The TransactionTemplate is also based on the template method design pattern, and it has the same approach as other templates in the Spring Framework, such as the JdbcTemplate. Similar to JdbcTemplate, TransactionTemplate also uses a callback approach, and it makes application code free from having the boilerplate code for managing transactional resources. We constructed an object of the TransactionTemplate class in the Service class construction, and passed an object of PlatformTransactionManager as an argument to the constructor of the TransactionTemplate class. We also wrote a TransactionCallback implementation that contains the business logic code of your application, which shows tight coupling between the application logic and transactional code.

We have seen in this chapter how efficiently Spring manages transactions in an enterprise application. Let's now study some best practices that we have to keep in mind whenever we work on the any enterprise application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset