0

We have a servlet to serve many concurrent requests and sometimes some request throw exceptions. I saw this warning from Hibernate guide:

"If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance."

In my real scenario, a request has made many important changes to DB and when we caught an exception, we must rollback all changes have made before.

So it seems we must implement the transaction-per-request pattern instead transaction-per-query to sure all changes are going to be rolled back.

It's right?

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
QuangHD
  • 1
  • 1
  • 3

1 Answers1

0

It depends on what you want, but yes, in general, you want one transaction for the life of a request, and you want all db operations for that request to participate in that one transaction. Usually, this also means there is one session bound to the request.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • How about this pattern performance and scalable in concurrency environment? I see we'll have many queries per transaction. But the Hibernate guide says: "In order to reduce lock contention in the database, a database transaction has to be as short as possible." – QuangHD Jul 28 '12 at 11:31