aspose file tools*
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 Soft Skills this week in the Jobs Discussion 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: 399
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: 3611
    
  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: 399
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: preparedstatement and loss of connection