Home > Uncategorized > Which is better : Spring’s Transaction management or EJB’s Container managed transaction

Which is better : Spring’s Transaction management or EJB’s Container managed transaction

I was a big fan of EJB’s CMT  (Container managed transaction) feature because it reduces the developer of complex task of managing transaction through code. One of the biggest drawback of this was that this feature could only be used with EJB’s and not ordinary Java classes.

Then came the Spring’s Transaction management feature which changed the way applications can manage transactions.  We will explore about Spring’s transactional capabilities and if they are better than EJB’s transaction

Spring provides two types of transaction

a)  Programmatic Transaction : This is used when developer would like to write the transaction related code himself

b) Declarative transaction :  This is enabled by Spring AOP and as the transactional aspects code comes with Spring and may be used in a boilerplate fashion


Most developers prefer declarative transaction as it has least impact on application code

Feature of Spring Transaction :

  • Spring Framework’s declarative transaction management is not tied to JTA therefore works in any environment. It can work with JDBC, JDO, Hibernate or other transactions under the covers, with configuration changes only.

For eg in this class below


public class RecordHibernateDAO implements RecordsDAO {

public void addRecords(Records record ){

//do something


We can mange the transaction by adding this annotation for the method addrecords

@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)


public void addRecords(Records record ){

//do something

  • The Spring Framework enables declarative transaction management to be applied to any class, not merely special classes such as EJBs. For eg in the above example instead of HibernateDAO class we could have a plain java class on which transaction capabilities can be applied. This makes life so much easy for an application developer. Now he is not constrained by “business logic” residing inside EJB only.
  • The Spring Framework offers declarative rollback rules: this is a feature with no EJB equivalent. Both programmatic and declarative support for rollback rules is provided. This especially comes handy when you want transaction to be rolled back for certain exceptions only.
  • The Spring Framework gives you an opportunity to customize transactional behavior, using AOP. For example, if you want to insert custom behavior in the case of transaction rollback, you can. You can also add arbitrary advice, along with the transactional advice. With EJB CMT, you have no way to influence the container’s transaction management other than setRollbackOnly().
  • The Spring Framework does not support propagation of transaction contexts across remote calls, as do high-end application servers. EJB is much better suited for this.

After having said all this , you can clearly decide which one is better. I went with Spring transactions because it gives me the ability to manage transaction decoratively with out having to bear the additional overhead of an Application server

Categories: Uncategorized Tags:
  1. mark
    September 27th, 2011 at 15:26 | #1

    spring transaction management is built on top of jta, jdbc, hybernate… Transaction processing in jta, jdbc, hybernate is pretty slow already and now spring adds new layer of overhead. Would this make performance even worse?

  1. August 11th, 2009 at 16:12 | #1
  2. August 12th, 2009 at 14:53 | #2
  3. August 17th, 2009 at 13:27 | #3
  4. August 20th, 2009 at 17:41 | #4
  5. December 12th, 2010 at 11:51 | #5