jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes can resuse the Connection and Resultset Obj Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "can resuse the Connection and Resultset Obj" Watch "can resuse the Connection and Resultset Obj" New topic

can resuse the Connection and Resultset Obj

bhagya patil

Joined: Oct 14, 2007
Posts: 12
can we reuse the only one existing Connection and one Resultset Obj to run more than one sql queries?
or every time when we need to get new connction object n rs object and run different the queries and close connection and resultset?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

The connection can be reused as much as you want, just don't close it. Do keep in mind that if every client has an idle connection it may decrease performance on the database server. There is an increase in performance on the client though since you don't have to reconnect all the time. Whether or not you want to have idle connections is up to you.

As for result sets, these may be shared by the SQL driver, but you shouldn't reuse the objects yourself - always close result sets and statements as soon as you no longer need them. This doesn't mean you can't reuse the reference variables though.

How To Ask Questions How To Answer Questions
Eung maeng
Ranch Hand

Joined: Feb 10, 2002
Posts: 68
I wonder how to keep the connection when external program will request to connect several time.
for example, A.class calls b.class(where JDBC connection to DB2 database) many times per day. In this case, if I don't keep the connection from B.class, i must be connected for every transaction from A.class's request.

Thus, the performance should be slower down.

please recommand what the best way is.

I agree. Here's the link: http://aspose.com/file-tools
subject: can resuse the Connection and Resultset Obj
It's not a secret anymore!