tags:

views:

33

answers:

1

I just want to make sure that if I use the following, I am opening a separate DB session and not resuing the same one (for testing I need individual sessions).

Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);

each time I do the above code, I run my query, then do a connection.close()

So for example:

while(some condition) {
  Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);

  //now use the connection to generate a ResultSet of some query

  connection.close();
}

So, each iteration of the loop (each query) needs its own session.

Is this properly opening separte sessions as I need (and if not, what would I need to add/change)? thanks

+1  A: 

The javadoc says:

Attempts to establish a connection to the given database URL

Slightly woolly language, and I suspect that this is up to the JDBC driver, but I'd be surprised if this did anything other than open a new connection.

I suppose it's possible for a JDBC driver to perform connection pooling under the hood, but I'd be surprised to see that.

In the case of the Oracle JDBC driver, this will open a new connection every time. This is a relatively slow process in Oracle, you may want to consider using a connection pool (e.g. Apache Commons DBCP, or c3p0) to improve performance.

skaffman
Exactly, that ambiguous language is the reason I wasn't sure..
llm
Oh, ok great, I am using Oracle..so I guess my code is fine then.
llm