开发者

JDBC opening a new database session

开发者 https://www.devze.com 2022-12-30 02:44 出处:网络
I just want to make sure that if I use the following, I am opening a separate DB session and not resuing the same o开发者_开发知识库ne (for testing I need individual sessions).

I just want to make sure that if I use the following, I am opening a separate DB session and not resuing the same o开发者_开发知识库ne (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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消