Spring @ Rollback read-only transaction mode

I have a service layer method with @Transactional(readOnly=true)

and this method often raises some RuntimeException

(say this is an exception NotFoundException

).

I am using ORM Hibernate also for DB interaction process.

Is this a legal pattern?

What is the default behavior in this case in terms of the "rollback" behavior? Could this somehow strongly affect the state of the connections or lead to any problems?


It's not like "why not try it yourself?" I have a suspicion that this might lead to an error Transaction rolled back because it has been marked as rollback-only

in the same method after a number of exceptions. This can be a very specific bug in the PostgreSQL JDBC driver. This is why I'm curious about this design in general: is it legal or illegal to do this?

+3


source to share


1 answer


So, as I understand it, you are concerned about the rollback. In this case, a readOnly

is select statement

, and there is usually no need to roll back from read

. The only place where it is convenient is when you are reading under a lock and when the transaction completes, you release that lock.

AFAIK will readOnly

install flash mod on FlushMode.NEVER

and it will be good and bad at the same time. Good, because there will be no dirty check as described here . Bad because if you call a read / write transaction in a transaction readOnly

, the transaction will silently execute the transaction because the session is not flushed. This is easy to test by the way, and I hope this hasn't changed since I tried it.

Then there is the connection pool. I know the C3P0

default policy is to rollback any work in progress. The flag to control this autoCommitOnClose

.



Then there is this link about readOnly

and postgres

- with which I have not worked and cannot say my opinion.

Now, about your topic on Transaction rolled back because it has been marked as rollback-only

. There readOnly

can be no rollback for a transaction like I said, so it really depends on how you bind your methods @Transactional

to IMO.

+1


source







All Articles