Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have multiple transactions within one Hibernate Session?

Can you have multiple transactions within one Hibernate Session?

I'm unclear if this is an allowable desirable. In my code I have a long running thread and takes items from a Blocking Queue, depending on what is on the queue, it may need to create and save a hibernate object, or it may not need to do anything.

Each item is distinct so if item 1 is saved and item 2 fails to save whatever reason I don't want to that to prevent item 1 being added to the database.

So the simplest way to do this is for each item that needs to be created to create a new session, open transaction, save new object, commit transaction, close session

However, that means a new session is created for each item, which seems to go against Hibernates own recommendations to not do Session Per Request Pattern. So my alternative was to create one session in the thread, then just open and commit a new transaction as required when needed to create a new object. But I've seen no examples of this approach and I'm unsure if it actually works.

like image 717
Paul Taylor Avatar asked Sep 17 '14 14:09

Paul Taylor


People also ask

Does Hibernate support nested transactions?

While Hibernate does not explicitly support nested transactions, using a JDBC 3.0 driver that is able to create savepoints can achieve this. Create a Connection at the start of the program when the SessionFactory is created.

How are transactions managed in Hibernate?

Transaction Interface in Hibernate In hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session and instantiated by calling session.

What is Hibernate Session for transaction?

Hibernate Session A session provides all methods to access the database and is at the same time a cache holding all data being used in a transaction. If you load objects using a query or if you save an object or just attach it to the session, than the object will be added to the Persistence Context of the session.

Can we share Session between threads in Hibernate?

If you were sharing a Hibernate Session between two threads, then one thread changes might not be visible to some other thread (without proper synchronization or volatile reads).


1 Answers

The session-per-request pattern uses one JDBC connection per session if you run local transactions. For JTA, the connections are aggressively released after each statement only to be reacquired for the next statement.

The Hibernate transaction API delegates the begin/commit/rollback to the JDBC Connection for local transactions and to the associated UserTransaction for JTA. Therefore, you can run multiple transactions on the same Hibernate Session, but there's a catch. Once an exception is thrown you can no longer reuse that Session.

My advice is to divide-and-conquer. Just split all items, construct a Command object for each of those and send them to an ExecutorService#invokeAll. Use the returned List to iterate and call Future#get() to make sure the original thread waits after all batch jobs to complete.

The ExecutorService will make sure you run all Commands concurrently and each Command should use a Service that uses its own @Transaction. Because transactions are thread-bound you will have all batch jobs run in isolation.

like image 91
Vlad Mihalcea Avatar answered Oct 14 '22 15:10

Vlad Mihalcea