Please see my following code snippet.
@Transactional
public void saveMembersService(List<Member> list1, List<Member> list2)
{
saveMembersDAO(list1); // does not perform commit. Why?
saveMembersDAO(list2); // does not perform commit. Why?
} // does perform commit only here!
@PersistenceContext
private EntityManager em;
@Transactional
public void saveMembersDAO(List<Member> members)
throws HibernateException
{
Iterator<Member> it = members.iterator();
while (it.hasNext())
{
Member wsBean = it.next();
em.persist(wsBean); // overall commit will be made after method exit
}
}
I use MySQL as DB. I watch MySql logs after above method execution. But I can see only single commit after saveMembersService method execution. I expect to see two commits for each transactional method invocation. Where am I wrong ?
UPDATE 1: Sorry, I have forgot to add @Transactional to my service method. It fixed now
UPDATE 2: I’ve checked with Propagation.REQUIRES_NEW attribute for DAO – result is the same (with and with out @Transactional at service method)
When @Transactional is used, Spring creates a proxy around your class and methods called internally does not pass in the proxy.
You have to do this instead. You can clean it by creating your TransactionTemplate in your Spring context.
If you are using AspectJ and you want to have 2 commits. You must use a @Transactional(propagation = Propagation.REQUIRES_NEW) on saveMembersDAO