Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in simplifying transaction based code

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • 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!
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic