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?
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.
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.
Programmatic transaction management. Spring provides two means of programmatic transaction management: Using the TransactionTemplate. Using a PlatformTransactionManager implementation directly.
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.
Programmatic Transaction Management
Declarative Transaction Management
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With