Official information on exactly what is 'correct' seems scant. I don't have the
desire to dig into the java SDK source for information on the Connection object
:-)
From my work with JDBC, it has always been safest (but not fastest) to ensure a
connection is only used by one thread at a time. If you are sharing the
Connection object amongst threads, then you need to make sure that access to
the Connection object is serial. The other (easier) way is to create a new
Connection for each thread.
The project I am working on at the moment maintains a Connection pool. The way
this works is that the thread gets a free Connection object, does some work
with it, then releases it back to the pool. For performance this is definitely
the way to do it, otherwise you have the latency of connecting with the RDBMS
every time you create a new Connection.
Here's an article on connection pooling:
http://www.webdevelopersjournal.com/columns/connection_pool.html
Official information on exactly what is 'correct' seems scant. I don't have the desire to dig into the java SDK source for information on the Connection object :-)
From my work with JDBC, it has always been safest (but not fastest) to ensure a connection is only used by one thread at a time. If you are sharing the Connection object amongst threads, then you need to make sure that access to the Connection object is serial. The other (easier) way is to create a new Connection for each thread.
The project I am working on at the moment maintains a Connection pool. The way this works is that the thread gets a free Connection object, does some work with it, then releases it back to the pool. For performance this is definitely the way to do it, otherwise you have the latency of connecting with the RDBMS every time you create a new Connection.
Here's an article on connection pooling: http://www.webdevelopersjournal.com/columns/connection_pool.html
Hope this helps.
Brendon.