David Parks
David Parks

Reputation: 32081

Spring & JDBC Transactions: How to ensure isolation level SERIALIZABLE in a DAO object?

Transactions are defined at the service level, as is typical.

But upon occasion we have a DAO method which requires a higher, SERIALIZABLE, isolation level.

But knowledge of whether the SERIALIZABLE isolation level is necessary is encapsulated in the DAO method, the service method need not know about this.

How can I enforce SERIALIZABLE isolation level at the DAO method level? I can't even find a way to identify what the isolation level is in Spring.

Upvotes: 3

Views: 5818

Answers (1)

les2
les2

Reputation: 14479

The service layer typically defines the transactional semantics.

But ... you can add the following annotation to achieve the serializable isolation level on the DAO implemenation method to achieve the effect:

@Transactional(isolation=Isolation.SERIALIZABLE,propagation=Propagation.MANDATORY)

The MANDATORY is so that the DAO method will not create a transaction if none exists. This will force all invocations to start from a service method. You can change that if desired.

Let me know how it works out.

Upvotes: 1

Related Questions