Guest_imported
New member
- Jan 1, 1970
- 0
Hi,
I'm new to JAVA, I'm trying to set up a connection pool for my application. First of all, I create a object called traveler for storing the DB collection and its activate time by calling the initPool method.
It can be compiled without any error, However, it always throws with "NullPointerException" when connection is passed to the traveler by setConnection method.
Is there something wrong ? ? why ?? anyone help ??
======================================================
public void initPool( String driverName, String url,
String username, String password )
throws SQLException {
try {
Connection c;
traveler tr[] =new traveler[maxConnections];
freePool=new Vector( maxConnections ) ;
activePool=new Hashtable( maxConnections ) ;
Class.forName( driverName ) ;
for( int i=0 ; i<maxConnections ; i++ ) {
c=DriverManager.getConnection(url ,username ,password );
tr.setConnection(c); <= *** problem
freePool.add(tr);
}
}
catch( Exception ex ) {
activePool=null ;
freePool=null ;
throw new SQLException( ex.toString() ) ;
}
}
==========================================================
// traveler declaration
package db;
import java.lang.*;
import java.sql.*;
import java.util.*;
public class traveler {
private static final long expireDuration=50000;
long retrivalTime;
Connection conn;
public void traveler(){
retrivalTime =0;
conn = null;
}
public void traveler(Connection c){
this.conn=c;
}
public void setRetrievalTime() {
retrivalTime = System.currentTimeMillis();
}
public int expiredSection() {
long currentTime = System.currentTimeMillis();
if ( (currentTime - retrivalTime ) > expireDuration ) {
return 1;
}
return 0;
}
public Connection getConnection() {
return this.conn;
}
public void setConnection(Connection c) {
this.conn=c;
}
}
I'm new to JAVA, I'm trying to set up a connection pool for my application. First of all, I create a object called traveler for storing the DB collection and its activate time by calling the initPool method.
It can be compiled without any error, However, it always throws with "NullPointerException" when connection is passed to the traveler by setConnection method.
Is there something wrong ? ? why ?? anyone help ??
======================================================
public void initPool( String driverName, String url,
String username, String password )
throws SQLException {
try {
Connection c;
traveler tr[] =new traveler[maxConnections];
freePool=new Vector( maxConnections ) ;
activePool=new Hashtable( maxConnections ) ;
Class.forName( driverName ) ;
for( int i=0 ; i<maxConnections ; i++ ) {
c=DriverManager.getConnection(url ,username ,password );
tr.setConnection(c); <= *** problem
freePool.add(tr);
}
}
catch( Exception ex ) {
activePool=null ;
freePool=null ;
throw new SQLException( ex.toString() ) ;
}
}
==========================================================
// traveler declaration
package db;
import java.lang.*;
import java.sql.*;
import java.util.*;
public class traveler {
private static final long expireDuration=50000;
long retrivalTime;
Connection conn;
public void traveler(){
retrivalTime =0;
conn = null;
}
public void traveler(Connection c){
this.conn=c;
}
public void setRetrievalTime() {
retrivalTime = System.currentTimeMillis();
}
public int expiredSection() {
long currentTime = System.currentTimeMillis();
if ( (currentTime - retrivalTime ) > expireDuration ) {
return 1;
}
return 0;
}
public Connection getConnection() {
return this.conn;
}
public void setConnection(Connection c) {
this.conn=c;
}
}