@Transactional Annotation in Spring

Spring transactions are implemented using @Transactional Annotation.

The annotation can be used with the class or a single method.

If used with a class, all methods of the class becomes transactional. If it is used only with a method, only that method becomes transactional.

//Class level
@Transactional
Class Employee{

    public void saveEmployee(Employee emp) {
       .......
       .......
    }

    public void updateEmployee(Employee emp) {
       .......
       .......
    }

}

\\Method Level
Class Employee{

    @Transactional
    public void saveEmployee(Employee emp) {
       .......
       .......
    }

    public void updateEmployee(Employee emp) {
       .......
       .......
    }

}

Now, those methods which are annotated might hit a single database or multiple databases i.e; distributed databases.

Composition of @Transactional:

1)Transaction Manager: Spring transactions are implemented using Transaction manager.

Check the below example. @Transactional annotation uses the propagation property.The vaue can be REQUIRED or REQUIRES_NEW. Let’s see what it is.

public class EmployeeService {
    private EmpDao eDao;


    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void saveEmp(Employee e) {
        eDao.save(e);

    }
}
  • REQUIRED: Code will always run in a transaction. Transaction Manager creates a new transaction or reuses one if available.
  • REQUIRES_NEW: Code will always run in a new transaction. Transaction Manager suspends the current transaction if one exists.

In Spring, @Transactional works by creating a proxy of your class (either a Java or cglib proxy) and intercepting the annotated method. This means that @Transactional doesn’t work if you are calling the annotated method from another method of the same class.

The proxy class contains or you can say inserts before and after advices for the transactional method. as part of the AOP. We will discuss the same in detail in another tutorial.

When a new transaction is created, new entity manager would also be created for jpa persistence if we are using jpa to persist data into database.

If it is Multithreaded for example say 10 threads. 10 transaction would be created with respective entity managers.

Transaction tx = TransactionManager.getTransaction();

So Transaction Manager is something that co-ordinates and executes a transaction.

  • Begin
  • Commit
  • Rollback

Enity Manger is used for persisting objects into DB. They are not thread safe hence for each transaction,new entity manager is created.

Final Notes:

  • Each Transaction manager, points to a particular database.
  • If there are multiple transaction managers, we need to mention the name of the transaction manager when using the @Transactional annotation.

@Transactional(bean=txtMgr1)

public void saveEmployee(Employee emp)

  • To execute multiple databases or you can say data sources as a single transaction, we have to use chained transaction manager.