Skizzo
Skizzo

Reputation: 2983

@Transactional and Time taken for execution

I'm developing a java web application that uses spring like application container. Now while I'm was using the transaction support to spring, I noticed that the time processing of the my annotated method is doubled. Try to descibe the method in a better way:

@Service
public class MyServiceImpl implements MyService{

    @Autowired
    UtilService utilService;

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void loadContracts(File fileToProcess,UtilDTO dto){

            List<MyObject> objects = utilService.readSomethings("xxx","yyy")

            //I modify Them

           //I save or update  them


    }

}


@Service
public class UtilServiceImpl implements UtilService{

    @PersistenceContext
    EntityManager entityManager;

    public List<MyObject> readSomethings(String p1,String p2){

    String queryString =    "from MyObject o where o.param1 = :param1 "
                    + " and o.param2 = :param2 ";

    Query q = entityManager.createQuery(queryString);
    q.setParameter("param1", p1);
    q.setParameter("param2", p2);

    return q.getResultList();   


    }

}

For example:

The method readSomething is too late while If I remove the annotation its time processing improves.

Why is there this difference?

Upvotes: 1

Views: 1099

Answers (1)

Makoto
Makoto

Reputation: 106490

The performance is likely due to you creating a new transaction and suspending the existing transactions every time that method is invoked.

From the documentation on Propagation.REQUIRES_NEW, emphasis mine:

Create a new transaction, and suspend the current transaction if one exists. Analogous to the EJB transaction attribute of the same name.

Unless you have specific requirements about creating a new transaction for this method, I would recommend letting it fall through to default behavior - that is, Propagation.REQUIRED.

Upvotes: 1

Related Questions