Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declarative or Programmatic Transaction in Spring

Tags:

spring

What type of transaction management strategy we should use in Spring? Declarative or Programmatic? Which one is better and under what situation one should use it? Can you give any proper examples or tutorial about it.

Also want to know that what is the latest things we should use while write database codes in spring? What is the alternative for HibernateTemplate?

like image 320
RickDavis Avatar asked Jun 27 '12 08:06

RickDavis


People also ask

Does Spring support declarative transactions?

Spring supports both programmatic and declarative transaction management. EJBs require an application server, but Spring transaction management can be implemented without the need of an application server.

What is declarative transaction in Spring?

Declarative transaction management approach allows you to manage the transaction with the help of configuration instead of hard coding in your source code. This means that you can separate transaction management from the business code. You only use annotations or XML-based configuration to manage the transactions.

Does Spring provide programmatic transaction management?

Programmatic transaction management. Spring provides two means of programmatic transaction management: Using the TransactionTemplate. Using a PlatformTransactionManager implementation directly.

Why is declarative transaction management more efficient than programmatic transaction?

Being able to set the transaction name explicitly is also something that can only be done using the programmatic approach to transaction management. On the other hand, if your application has numerous transactional operations, declarative transaction management is usually worthwhile.


2 Answers

Programmatic Transaction Management

  1. Allows us to manage transactions through programming in our source code.
  2. This means hardcoding transaction logic between our business logic.
  3. We use programming to manage transactions
  4. Flexible, but difficult to maintain with large amount of business logic. Introduces boilerplate between business logic.
  5. Preferred when relative less transaction logic is to be introduced.

Declarative Transaction Management

  1. Allows us to manage transactions through configuration.
  2. This means separating transaction logic with business logic.
  3. We use annotations (Or XML files) to manage transactions.
  4. Easy to maintain. Boilerplate is kept away from business logic.
  5. Preferred when working with large amount of Transaction logic.
like image 61
Raman Sahasi Avatar answered Nov 16 '22 02:11

Raman Sahasi


They are not mutually exclusive.

You can use decalrative transaction management (@Transactional) in most of cases, and fall back to programmatic transaction management (TransactionTemplate) when you face limitations of Spring AOP (see 11.5.1 Understanding the Spring Framework's declarative transaction implementation) or need to control transactions in more complex ways.

like image 44
axtavt Avatar answered Nov 16 '22 04:11

axtavt