E M wrote:
> Ok. Is this the way to do it? I could could not
> find a Connection.reconnect(), so I had to create
> a new one through the DriverManager. Will my
> PreparedStatements using that connection need to
> be recreated or will they still be intact? In the
> finally I closed the connection like you suggested
> and refreshed it. Is that ok?
>
>
>
> // my connection properties
> private String dbUrl;
> private String dbUser;
> private String dbPassword;
> // my connection
> private Connection con;
>
>
> private PreparedStatement pstmtSelect;
> public ResultSet selectClient(int id){
> try {
> // make sure connection is refreshed
> refresh();
> // make SELECT
> pstmtSelect.clearParameters() ;
> pstmtSelect.setInt(1,id);
> return pstmtSelect.executeQuery();
> }catch(Exception e){
> System.out.println("Select failed:"+e);
> }finally{
> // close and re-open connection
> con.close();
> refresh();
> }
> }
>
> public void refresh(){
> if(con.isClosed()){
> con=DriverManager.getConnection( dbUrl,
> dbUser, dbPassword);
> }
> }
>
E M,
Connection.isClosed() isn't going to help you much. Take a look at the following section from the docs that shows one possible way of doing this...Also, note that in most applications, you probably don't want to be using DriverManager, as it has static synchronization issues...If you're only using MySQL, just use an instance of com.mysql.jdbc.Driver and call the .connect() method to get a new connection:
http://dev.mysql.com/doc/connector/j/en/cj-troubleshooting.html#id2629885
-Mark
Mark Matthews
Consulting Member Technical Staff - MySQL Enterprise Tools
Oracle
http://www.mysql.com/products/enterprise/monitor.html