The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

RE: Jdbc Thread

Posted by Kishori Sharan on August 04, 2000 at 11:25 AM

Hi

Here is an example to demostrate the things you want.
1. Create an object which has method isValidConnection ( )
and createConnection ( )
2. Create a class inherting from Thread class whose on eof the method takes an argument of obejct created in step 1. This class will have a mamaber obejct of type as created in step1. In run method of this class keep calling idValidConnection () method the memebr variable and if it returns false call createConnection () if you want to create a new connection. Then call sleep() method for this class for whatever time you want.
I wrote the following example just to see if it can be done. I used SQL Anywhere database and Odbc-Jdbc connection. While my program was running I shutdown the database and the program connected again own its own. I am just displaying recods from a table on console.

3.

Thanx

///////// Example////

import java.sql.* ;


class JdbcWatch extends Thread {
private PfcConnect pfc = null;

public void setConnection ( PfcConnect pfc ) {
this.pfc = pfc ;
}

public void run ( ) {
while ( true ) {
try {
// Check for valid jdbc connection, if necessary create a new connection
if ( !pfc.isValidConnection ( ) )
pfc.createConnection ( );

// sleep for sometime
this.sleep ( 2000 ) ;
} catch ( Exception e ) {
}
}
}

}

public class PfcConnect {


public static final String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver" ;
public static final String dbUrl = "jdbc:odbc:pfc_dsn" ;
public static final String uid = "dba" ;
public static final String pwd = "sql" ;
private static Connection c = null ;

public boolean isValidConnection ( ) {
boolean isclosed = false;
try {
isclosed = ( c.isClosed( ) || c == null ) ;
}
catch ( Exception e ) {
isclosed = true ;
}

return isclosed ;
}


public void createConnection ( ) {
System.out.println ( "Creating a new connection..." ) ;
try {
c = DriverManager.getConnection ( dbUrl, uid, pwd ) ;
} catch ( Exception e ) { }
}

public static void main ( String[] args ) throws Exception {
Statement s = null ;
// Load the driver
try {
Class.forName ( dbDriver ) ;
} catch ( Exception e ) { }

// Create the object which will create the connection
PfcConnect pfc = new PfcConnect ( ) ;
pfc.createConnection ( );
JdbcWatch jdbcW = new JdbcWatch ( );
jdbcW.setConnection ( pfc ) ;
jdbcW.start ( );


for ( int i = 1 ; i < 1000 ; i ++ ) {
try {

s = c.createStatement ( ) ;

ResultSet r = s.executeQuery ( "Select id, name, salary, bdate from person" ) ;

while ( r.next() ) {
System.out.println ( r.getInt ( "id") + ". " + r.getString ( "Name" ) + ", " +
r.getString ( "salary" ) + ", " + r.getString ( "bdate" ) ) ;
}
} catch ( Exception e ) { }

}


s.close ( );
}

}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us