• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Transaction management in Hibernate

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ladies and Gents,

In a recently held interview I was given a situation where I was asked how would I do the transaction management using hibernate when I need to book a flight ticket from New Delhi to New York in the following format
1) New Delhi to London ,
2) London to New York

Would you please tell me what are the different ways of achieving this transaction management.


Regards

Guy
 
Author
Posts: 33
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably they wanted you to say that JTA is needed because each airline can be considered as a different Resource Adapter while you have a single global transaction.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guy Emerson wrote:Would you please tell me what are the different ways of achieving this transaction management.


A very simple way to achieve this would be something like this. First create a book method in a FlightDao class which takes a Flight object as a parameter (and a Flight object has at least two instance variables: from and to). Then create a book method in a FlightService class which takes a list of Flight objects as a parameter. In this method I would start a transaction and try to book each flight. If all flights are booked successfully, the transaction will be committed. But if one of the book method invocations fails with an exception, the transaction will be rollbacked. The code could look like thisAnd then you can easily invoke the book method in the FlightService class with the appropriate flightsBut maybe you want to have a look at the Transactions and concurrency control chapter of the Hibernate User Guide to have a better understanding about transaction, (anti-)patterns and common issues.

Hope it helps!
Kind regards,
Roel
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic