In BalusC answer of this question he said that
you should not be declaring the Connection, Statement and ResultSet as instance variables at all (major threadsafety problem!)
I suppose he is referring to the situation when you create an instance and then use it in two different threads, but if you don't do so and every method/thread you run creates their own instance, I cannot see the problem... but BalusC seems so categorical about it.
I normally create a class DBaccess
with an instance variable conn
, that I initialise in the constructor of the class (for example), and then reuse during all the database calls made by this DBaccess
instance. I cannot see the threadsafety problem there since the conn
variable is not static, but created in each instance of DBaccess
. I have been always reluctant to creating the conn
variable per each database query because this will request a connection from the pool each time, whereas I always thought it is more efficient to get the connection and do the 2 or 3 database queries I do in a row, without having to get 2 or 3 different connections.
Can someone explain if my understanding of BalusC warning is correct and if using his advise will not create more overload because of the request of connections per single database access?