• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in simplifying transaction based code

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! I'm really doubtful about this code here but it's the only thing that works for me as of now. I wanted this to be in a single transaction but I'm not sure how to do it.



If you look at the code, you'll notice that I made an early commit, which I didn't want to do but I had no choice because I wanted to get the generated ID to be able to produce a controlId String.


You'll also notice that I made many commit statements which makes the rollback in the try-catch block useless because changes are already reflected to the database when an exception is thrown. I really want to avoid this so I want to put them all in a single transaction and rollback everything if an exception occurs. Many thanks!
 
author
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Timothy,

The problem of obtaining a generated id before commit can be solved by issuing a flush call, so I would normally recommend that you do that, however, looking at your code I have to say (and no offence intended) that you might really want to so some more reading on JPA before going any further. I think you would find that a small investment of a day or two would really pay dividends. Pick up any book on JPA (I can recommend one ;-) that takes your fancy and learn when and how to use which EntityManager operations, etc.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! It worked! I adjust my code like the following:



I can see that you have written a book? My friend whom I ask to order for me isn't with me anymore so I won't be able to get stuff on the internet anytime soon. Thanks a lot for the help BTW!
 
Grow your own food... or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic