Yesterday i was profiling an application to test out the yourkit profiler. After firing a request on certain pages within the application i noticed that that there were 36 calls to get a connection from the datasource.
This asstonished me because a lot of those calls where made from methods where a caching mechanism was in place. After studying some more i noticed that there were far less then 36 SQL statements executed .
In this application we have all services annotated with sping’s @Transactional annotation. After some asking around and studying i found out that everytime a transaction was started the transaction manager would directly try to get a connection from the pool, even when no real database call was being made.
After some hints from a collegue and some research on the Internet i found spring’s LazyConnectionDataSourceProxy class. A class that can proxy a datasource and fetches a connection only when a statement is created. This class is verry simple to use, you just configure it around your normal datasource and pass this one in to the transactionmanager, hibernate etc.
For my applciation this increased performance for a single threaded jmeter test with around 10%, not a huge difference but still worth it.
For another application that was being load tested by one of my colleague’s the difference was much bigger. He was running a test with a lot of threads within jmeter, and soon the server could not handle it anymore because there were no more connection within the pool. After putting in this proxy the throughput of his application was a couple of times higher then without it.
Ofcourse another way to make sure this does not happen is to put only @Transactional on a method when you are sure that it will be executing some sort of statement on the database.
i will try to update this post soon with some sample and stats.

I do have a question. Does it matter that you use @Transactional or configure the transaction using xml and annotations? Is there a difference for read-only transactions?
Hi Jettro,
i don’t think it would matter if you use declarative transaction, i think it is spring’s transactionmanager that is getting a database connection while opening a connection, but i will try it out and let you no.
As for as i good see for the read only transactions, there is no difference, it still gets the connection even when not executing a statement.