Thu Oct 23, 2008 3:39 pm
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
myschool.DatabaseConnections.getConnection(DatabaseConnections.java:86)
org.apache.jsp.Register_005f1_jsp._jspService(org.apache.jsp.Register_005f1_jsp:\
78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
public class DBConn implements Connection
{
private Connection conn;
private boolean isfree;
private long timeStamp;
public DBConn(Connection conn)
{
this.conn = conn;
isfree = true;
timeStamp =0;
}
public boolean valid()
{
try{
conn.getMetaData();
return true;
}
catch(SQLException se) {
return false;
}
}
public boolean isFree()
{
return isfree;
}
public boolean lease()
{
if(isfree) {
isfree=false;
timeStamp=System.currentTimeMillis();
return true;
}
else{
return false;
}
}
public void release()
{
isfree = true;
}
public long getTimestamp() {
return timeStamp;
}
..../// the rest of the neccessary code to implement the interface methods
}
public class DBConnectionPool
{
private String username;
private String password;
private String dbURL;
protected ArrayList pool;
private int poolsize;
final private long timeout = 60000;
private Connection createConnection() throws SQLException
{
return
DriverManager.getConnection(this.dbURL,this.username,this.password);
}
public DBConnectionPool(String dbURL,String username,String
password) throws SQLException {
this.dbURL = dbURL;
this.username = username;
this.password = password;
pool = new ArrayList(poolsize);
for(int i=0; i <pool.size() ; i++)
{
pool.add(new DBConn(createConnection()));
}
}
public DBConn getConnection() throws SQLException {
DBConn con;
for(int i=0; i<pool.size();i++)
{
con = (DBConn)pool.get(i);
if(con.lease())
{
return con;
}
}
// if all connections are in use create a new one
con = new DBConn(createConnection());
con.lease();
pool.add(con);
return con;
}
public void returnConnection(DBConn con)
{
con.release();
}
.....
}
---------------------------------------------------
public class DatabaseConnections extends Thread
{
private static String username;
private static String password;
private static String dbURL;
private static String dbDriver;
private static int maxConNo;
private static ArrayList dbconnectionpools;
private static ArrayList schID;
private static long delay;
public void run() {
while(true) {
try {
sleep(delay);
} catch( InterruptedException e) { }
/// need to write some more code here later
}
}
private static void readProperties()throws Exception
{
// data stored on a file
FileInputStream fis = new FileInputStream("properties.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
dbDriver = br.readLine();
dbURL = br.readLine();
username = br.readLine();
password = br.readLine();
maxConNo = Integer.parseInt(br.readLine());
delay = Integer.parseInt(br.readLine());
br.close();
isr.close();
fis.close();
}
private void createConnectionPools() throws ClassNotFoundException,
InstantiationException,
IllegalAccessException,
FileNotFoundException,
IOException, SQLException
{
Class.forName(dbDriver).newInstance();
FileInputStream fis = new FileInputStream("dbnames.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
dbconnectionpools = new ArrayList();
schID = new ArrayList();
schID.add(0);
dbconnectionpools.add(new DBConnectionPool(dbURL +
"schools",username,password));
String s;
while((s=br.readLine())!= null)
{
String[] ars = s.split(",");
schID.add(Integer.parseInt(ars[0]));
String sdburl = dbURL + ars[1];
dbconnectionpools.add(new
DBConnectionPool(sdburl,username,password));
}
br.close();
isr.close();
fis.close();
}
public DBConn getConnection(int schoolID) throws SQLException {
for(int i=0; i<dbconnectionpools.size() ; i++) {
if((Integer)schID.get(i)==schoolID) {
return
((DBConnectionPool)dbconnectionpools.get(i)).getConnection();
}
}
return null;
}
public DatabaseConnections()
{
try
{
readProperties();
createConnectionPools();
// run();
}
catch(Exception e)
{
}
}
}
Thu Oct 23, 2008 3:41 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.