Reputation: 774
Can anybody explain me that when I should put readOnly
value as true
and when I should set it as false
when using it with @Transactional
?
Upvotes: 1
Views: 595
Reputation: 754
Customizing the isolation level you add more control to your transactions.
If you know that a method will be read-only you should specify it.
With readonly=true you are saying to the transaction manager that one particular method will only read from the DB. This has two advantages:
First, it can be faster than the others, because it allows the DBMS to optimize the transaction (if supported). Secondly it could save you from deadlock problems (when for example a specific table is write-locked), because you are assuring that the method won't execute INSERT or UPDATE.
However, here you can find all the details about it: http://docs.spring.io/spring/docs/2.5.x/reference/transaction.html
Upvotes: 1
Reputation: 47290
When you are only reading/selecting from the database and not changing any data - by performing an update/insert/delete.
If you can specify readOnly you should as its much less resource intensive.
Upvotes: 4
Reputation: 1053
the definition is pretty straight-forward: You may use readonly=true
if and only if you assure no update, insert or delete operation takes place inside the transaction.
This optimizes the locking behaviour of your dbms, if supported.
readonly
is false by default.
best regards,
sam
Edit
/**
* {@code true} if the transaction is read-only.
* Defaults to {@code false}.
* <p>This just serves as a hint for the actual transaction subsystem;
* it will <i>not necessarily</i> cause failure of write access attempts.
* A transaction manager which cannot interpret the read-only hint will
* <i>not</i> throw an exception when asked for a read-only transaction.
* @see org.springframework.transaction.interceptor.TransactionAttribute#isReadOnly()
*/
boolean readOnly() default false;
Upvotes: 2