views:

179

answers:

3

How can I validate that all JDBC access happens on an active transaction, i.e. java.sql.Connection.getAutoCommit() always return false?

I'm using Spring for transaction management ( @Transactional ) and Hibernate for data access.


Update: What happens is that some Hibernate access is performed without the service method being annotated with @Transactional. That's what I want to be notified of.

Update 2: Code sample

It's possible to call the following code:

public ServiceImpl implements Service {

     // missing @Transactional
     public List<String> getSomeIds() {
           return getDao().getSomeIds();
     }
}
+2  A: 

Use PROPAGATION_REQUIRED on all persistent methods.

duffymo
Thanks for the answer. What happens is that some Hibernate access is performed without the service method being annotated with @Transactional. That's what I want to be notified of.
Robert Munteanu
Please give an example of Hibernate access without the service method. It should all happen through the service layer. You can write an aspect to expressly forbid DAO access outside the service layer if you wish.
duffymo
I've added a code sample. Does this clarify the question?
Robert Munteanu
Why is reading outside a transaction a problem? I might be ignorant here, but I thought that write operations were the ones that had to worry about ACID. And if this is so problematic, wouldn't it be better to simply add the annotation than jump through hoops? Write the aspect or add the annotation would be my advice.
duffymo
+1  A: 

You actually shouldn't need to do this, since according to the @Transactional documentation, section 9.5.6.1:

The default @Transactional settings are:

 The propagation setting is PROPAGATION_REQUIRED
 ....

PROPAGATION_REQUIRED means:

Support a current transaction, create a new one if none exists.

EDIT: Your best bet is probably to do what @duffymo said in his comment:

You can write an aspect to expressly forbid DAO access outside the service layer if you wish.

Alex Beardsley
Thanks for the answer. What happens is that some Hibernate access is performed without the service method being annotated with @Transactional. That's what I want to be notified of.
Robert Munteanu
I've added a code sample. Does this clarify the question?
Robert Munteanu
+2  A: 

One way I can think of is to implement and register a dummy JDBC Driver to act as an interceptor between the statements you want to monitor as well as keep a log of when Connection#setAutoCommit() and Connection#commit() is called to know the boundaries of the transaction.

Cue