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 a
There are two types of Transaction management that Spring supports:
1. Programmatic Transaction Management: Transaction is managed with the help of programming and provides extreme flexibility, but it is difficult to maintain.
2. Declarative Transaction Management: Transaction management is separated from business code and only annotations or XML based configurations are used to manage the transactions.
Spring offers both programmatic and declarative transactions.
Programmatic means you have transaction management code surrounding your business code. This gives extreme flexibility, but is difficult to maintain and, well, boilerplate.
Declarative means you separate transaction management from the business code. You can use annotations or XML based configuration.
programmatic management is more flexible during development time but less flexible during application life
declarative management is less flexible during development time but more flexible during application life
http://docs.spring.io/spring/docs/3.0.x/reference/transaction.html
Declarative Transaction Management allows to eliminate any dependencies on the transaction framework from the Java code. The four participants to provide the transaction support are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.
Suggest to use Declarative Transaction Management, Alternative for HibernateTemplates either NamedJDBCTemplate or simpleJDBCTemplate
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.
Programmatic Transaction Management
Declarative Transaction Management
Programmatic transaction management is usually a good idea only if you have a small number of transactional operations. For example, if you have a web application that require transactions only for certain update operations, you may not want to set up transactional proxies using Spring or any other technology. In this case, using the TransactionTemplate may be a good approach. 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. It keeps transaction management out of business logic, and is not difficult to configure.
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#tx-decl-vs-prog
and
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html
in general.
examples:
http://www.springbyexample.org/examples/hibernate-transaction-annotation-config.html
New features: I suggest using DI with SessionFactory. Also take a look at 3.1 new feature: Hibernate 4 support. see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/new-in-3.1.html#d0e1385