How to pass around JDBC connection without using Spring/JPA/Hibernate?

You could use a ThreadLocal. Have the entry point set it up and than the DAOs.

You could use a ThreadLocal. Have the entry point set it up and than the DAOs class ConnectionUtil { public static final ThreadLocal connection = new ThreadLocal(); } public Return method(Args arg) { ConnectionUtil.connection. Set(newConnection()); try { ... } finally { ConnectionUtil.connection.remove(); } } Pretty ugly, but that seems to be what your boss wants.

This might work really well for us. Are there any major performance issues doing it this way? – Shane Dec 10 '10 at 19:32 ThreadLocal's have been a target of performance improvements from Sun for a while.

Compared to DB operations, it'll be a rounding error. – sblundy Dec 16 '10 at 1:11.

Use Apache Commons DBCP. It's the Connection Pool project from Apache, and what's used internally in many engines.

I understand that Apache Commons DBCP helps maintain a pool of connections, but I need each individual DAO insert/update/delete method to be able to grab the SAME connection. Can DBCP help with that? Is there an example somewhere?

I tried several Google searched and couldn't find anything. – Shane Dec 9 '10 at 20:53.

We have done that before (5 years ago or so on an IBM WebSphere). We wrote an own pool and stored the jdbc-connections in a hashtable with the sessionID. The only pitfall was to close the connection on sessionend and return it to the pool (we did that with a sessionlistener).

If one user session connects only to one jdbc-connection, the thread safety is inherited. So the singleton approach does definitly work. Our performance gain was awful.

Additionally what I have done for DAOs in this is not to pass the connection in the method signature but in the Constructor instead. Then I hold the object that originally creates the connection responsible to close it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions