How does transactional propagation work when using Open Session In View?

I am really confused about distributing transactions in Spring with Hibernate. I use Spring annotations @Transactionalfor my service level methods. Some are marked as "read-only = true". If one of my read-only methods calls a method that is not read-only, how can I handle this?

I think I can tag all my read and write methods to support distribution REQUIRES_NEW, but this will lead to behavior that I cannot want - i.e. I want only a new transaction in the case where the read-only method is called the read-write method. If the read-write method calls another read-write method, I don't need a new transaction.

Given all this, I don’t understand how Open Session In View (OSIV) works! Of course, using OSIV in Spring, you OpenSessionInViewFiltermust start the transaction before calling the service methods. In this case, you must determine if the transaction is read-only or read-write. BUT, how can he know this? He does not know what will happen under the covers of the service level.

I am completely in the dark on all this and would like someone to explain it to me!

+5
source share
3 answers

Hibernate session life cycle is different from transaction cycle. These two can overlap each other.

OpenSessionInViewFilter , Hibernate . Spring -transactional, hibernate, / . . / .

read-only ( , ), , , . , - , , , - .

+6

In View , , , Hibernate , , , - , , , , , .

OSIV. .

, -, , , , . , , -, , ( ).

, , , (OSIV). , .

+2

- , . . , -, -.

REQUIRES_NEW, Spring . .

A Hibernate session may contain several consecutive transactions. Therefore, the OSIV filter does not need to know what transactions it will contain. The OSIV filter creates a session with FlushMode = MANUAL, and the read and write transaction methods will temporarily change to FlushMode = AUTO. Each transaction will use a read-only or read-write JDBC connection.

+2
source

All Articles