Reputation: 1009
I have a web application that uses Spring NamedJDBCTemplate
, and all the calls to database are select statements.
In this case should i use @Transactional
in my service class that calls the DAO class that inturn fires select statements to DB.
According to Transaction statergies listing 10 suggests to not use @Transactional
for reads. Will i be bringing an overhead by using @Transactional
and also i dont want to miss the AOP advises that i can bring in for @Transactional
in future.
Upvotes: 3
Views: 3362
Reputation: 79
I think the best way is to use @Transactional and set it as Supported not Required for such service in this way if your service call outside a transaction it will not start a Transaction and if it calls from other service that is Transactional and already start a transaction it will participate in that transaction.
for example think that one service required to call two services in first one some data will inserted or updated and other one is just a select that return those data if these two service don't participate in single transaction the second service will not return data because the transaction start in calling service not committed yet .
Upvotes: 0
Reputation: 692073
Yes, you should always access the database from inside a transaction. Not doing it will in fact create a transaction for every select statements.
Transactions aren't just useful for atomicity of updates. They also provide isolation guarantees. For example, (depending on the isolation level) reading the same row twice in a single transaction can return you the same data, and thus make sure you don't have incoherences in the read data. Doing it with multiple transactions won't provide any such guarantee.
Upvotes: 3