• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Problem with Spring Transactional Annotation

 
Greenhorn
Posts: 7
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using annotation based Spring IOC & Transaction in my web application with Hibernate ORM.

During the development I came across a situation where two separate DAO methods perform database persistence operation & this situation must either success or entirely fail, no single operation should be persisted & other remain with some error. For this situation I've put an annotation on Service layer.

Here is a snippet of Service class:


In DAO save operation is performed using Hibernate's Session. After each operation it will save the entry using save() method of Session object. So, it will call save twice, one from locationDao and another from locationMappingDao.

Issue:
It is saving first entry persisted it to the database & sequentially save second entry to the DB. As per Transaction concept, both entry should be persisted to the database at a single time, otherwise ACID property will not be maintained.

Please suggest any way to solve this problem. Also suggest if I am wrong somewhere to declare annotation.

Let me know if require more information regarding the same.

Thanks
 
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you actually meet any problem?

Try forcing an exception while processing the 2nd item (after the first is already saved), all in one call (I assume that is what your post is about; 1 call, 2 processes, all or nothing commit).

If the first is committed to the database and the second is not, then you have a problem.

If that is the case, try encapsulate the processing by making a new method (and annotate it with @Transactional). This new method will take care of calling the 2 other methods concerned. That way, the 2 methods are in 1 single transaction, and ideally backout the whole thing appropriately if any single thing fails. Below is an example.

 
Parth Bhagat
Greenhorn
Posts: 7
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Kathleen.

Earlier in the code not both the DAO methods having @Transactional annotation. So, I've applied the annotation on both the methods & tried out thing again. But there is no change in the result. Still it is saving both entries at different steps. One operation persists first & then second operation persists data to the database.

Here is the DAO code:




Hibernate's SessionFactory object is been injected by IOC container.

 
reply
    Bookmark Topic Watch Topic
  • New Topic