Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Transactional Annotation

I'm trying to get a better handle on the use of Spring's @Transactional attribute. I understand that it basically wraps the contents of the method marked as @Transactional in a transaction. Would it be appropriate to mark a service/business layer method as transactional, as opposed to the actual DAO method, as I have done here?

Service Implementation

public class UserServiceImpl implements UserServiceInt{
   @Autowired
   private UserServiceDAO serviceDAO;


   @Override
   public User getUser(int id){
      return serviceDAO.getUser(id);
   }

   @Override
   @Transactional
   public void updateUserFirstName(int id, String firstName) throws SomeException{
      User userToUpdate = getUser(id);
      if(userToUpdate == null){
         throw new SomeException("User does not exist");
      }
      userToUpdate.setFirstName(firstName);
      serviceDAO.updateUser(userToUpdate);
   }

}

DAO Implementation

public class UserServiceDAOImpl implements UserServiceDAOInt{
   @PersistenceContext(unitName="myUnit")
   private EntityManager entityManager;

   @Override
   public void updateUser(User user){
      entityManager.merge(user);
   }

}

I'm not even sure if the call to merge is even necessary. How does Spring know which EntityManager to use since there isn't an EntityManager declare in the UserServiceImpl class?

like image 931
user1154644 Avatar asked Jun 05 '15 13:06

user1154644


People also ask

What is Spring transactional annotation?

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".

How does @transactional work in Spring?

So when you annotate a method with @Transactional , Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.

What is @transactional annotation in Java?

Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.

Is @transactional mandatory?

MANDATORY PropagationWhen the propagation is MANDATORY, if there is an active transaction, then it will be used. If there isn't an active transaction, then Spring throws an exception: @Transactional(propagation = Propagation.


Video Answer


1 Answers

We mark a Service layer with @Transactional when a method in a Service class is having multiple database calls and we want either all calls should happen or no one should happen or we can say if any call fail then whole transaction should rollback. If we are not falling under this criteria then we can opt for @Transactional on DAO layer also.

How does Spring know which EntityManager to use since there isn't an EntityManager declare in the UserServiceImpl class?

Spring is referring the EntityManager from persistence.xml(from classpath), whose structure is similar to below:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
    <persistence-unit name="myUnit">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/YourDatasource</jta-data-source>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>
like image 64
Arpit Aggarwal Avatar answered Sep 19 '22 13:09

Arpit Aggarwal