Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating that all JDBC calls happen within a transaction

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();
     }
}
like image 249
Robert Munteanu Avatar asked Feb 02 '26 13:02

Robert Munteanu


2 Answers

Use PROPAGATION_REQUIRED on all persistent methods.

like image 93
duffymo Avatar answered Feb 04 '26 03:02

duffymo


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.

like image 25
qnoid Avatar answered Feb 04 '26 04:02

qnoid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!