Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do simple Spring JDBC transactions outside the IoC container?

Tags:

The project I'm working on uses straight JDBC data access in all its boilerplate glory and doesn't use any transactions. I feel like using transactions and simplifying the way data access methods are written is important, especially with some changes being made currently. The project has been around for quite a while and isn't suited to an ORM framework. It also uses lots of Singletons (ugh) and untangling it to make it able to use dependency injection would be a fair amount of work and I don't think I could convince anyone that we should do that now.

I like the interface of Spring JDBC, specifically through its SimpleJdbcTemplate. My question is about how to enable some simple (per servlet request) transactions for this, without having to set anything programmatically in every data access method or using the Spring IoC container or AOP. I've played around with my own architecture that ends up with an interface similar to SimpleJdbcTemplate's and can use a single request-local connection and transaction when calls to it are made in the context of a request (through a ServletRequestListener with a ThreadLocal). It seems to work well, but I think using a good external library like Spring JDBC would be preferable.

Anyone have any experience with this?

like image 617
ColinD Avatar asked Feb 17 '09 16:02

ColinD


1 Answers

Perhaps you could use TransactionTemplate and TransactionCallback as described in Programmatic Transaction Management?

like image 120
toolkit Avatar answered Sep 30 '22 15:09

toolkit