This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes preparedstatement and loss of connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "preparedstatement and loss of connection" Watch "preparedstatement and loss of connection" New topic
Author

preparedstatement and loss of connection

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
What happens to a preparedstatement if the connection is lost and you reconnect? Can you still use a pre-existing preparedstatement, or do you have to recreate it?

Correlation does not prove causality.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

This ultimately depends on the driver, but PreparedStatements are generally tied to the connection, so if you have a different instance of a connection, you most probably need to recreate the statement.

Some connections (again, depending on the driver and/or connection configuration) cache prepared statements, so if you close and recreate it each time, you may actually incur just a small penalty.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Martin Vajsar wrote:This ultimately depends on the driver, but PreparedStatements are generally tied to the connection, so if you have a different instance of a connection, you most probably need to recreate the statement.

Some connections (again, depending on the driver and/or connection configuration) cache prepared statements, so if you close and recreate it each time, you may actually incur just a small penalty.


OK, thanks. I'm dependent on mixed drivers (I guess) because there's more than one db involved and the legacy system is an old one. To be on the safe side, I've written some gets and sets to allow reinitializing the prepared statements from the same helper class that handles the connections. if reconnect then reinitialize ....

re: "I guess" ... I set things up to use Microsoft's jdbc driver version 4 ... did the installation etc. But when connecting to the legacy system, it just didn't work. I had to add the old "Class.forName" statement not required by version 4 and I guess I'm then using a driver that was already installed. Just adding "Class.forName" fixed it.
 
Consider Paul's rocket mass heater.
 
subject: preparedstatement and loss of connection