Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction management in Hibernate

 
Guy Emerson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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
 
Vlad Mihalcea
Author
Ranch Hand
Posts: 32
6
  • Mark post as helpful
  • send pies
  • 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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic