Total members 11895 |It is currently Tue Dec 03, 2024 8:08 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





I am working on this project for schools where each school is supposed
to have her own database, that contains students and their results ..etc..

i need to connect to these databases through a jsp.. so i created
several classes in an attempt to apply the connection pooling
technique.. its the first time for me to use connection people and I
am having a hard time !!..

here is what i did.. first i created a DBConn class that implements
the Connection interface and I believe I've wrote the necessary code
properly.. then i created a DBConnectionPool class.. to represent a
connection pool to a specific database.. since i will be needing more
than one pool to different schools.. lastly i created a Database class
that has an array list of DBConnectionpools and another array list of
school IDs where for example the first element in the schoolID array
list refers to the school to which the first pool in the
DBCOnnectionpools array list connects to.. and i wrote the code
required to get a connection and release a connection from the pools
..I then compiled the 3 classes and they worked properly.. so far so
good...

the problem occured when i attempted to use the DatabaseConnections in
the jsp file using useBeans..

<jsp:useBean id="myconpools" class="myschool.DatabaseConnections"
scope="application" />

when i later in the page tried to call the (public)method in the
DatabaseConnection class which is called getConnection(int id)

myconpools.getConnection(0);

i get the following error

exception here :

Code:
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)

I am not sure if i should attach the code of my 3 classes.. but i
really do need help.. I've been trying to figure out the problem for 3
days but i have no clue... I will try to paste the imp parts of my
classes ... I would really appreciate the help, thank you in advance


--------------------------------------------------------------
Code:
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
}

----------------------------------------------------

Code:
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)
{

}
}

}





Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

Can I ask why you're not using drivers that already exist? If the answer
is that these databases could use different engines (Oracle, MySQL,
etc.), then I think I may have a solution. Use the Data Access Object
(DAO) design pattern in conjunction with the Factory pattern. Here's how
it works:
1) Create a Factory. The Factory contains constants of the form
public final static int XXX = n;
2) The Factory creates the correct DAO object depending upon the value
of the passed constant.
3) Each DAO interfaces to a single JDBC driver.

An example call:

DAOFactory daof = new DAOFactory ();
DAO dao = daof.getDAO (DAOFactory.ORACLE, "MachineName", "MY_DATABASE");

I'm assuming that you will have to pass the name of the database to the
DAO Factory object.

I would not do this in a JSP page; it'll be too messy. Instead, create
the proper classes and run them on the server.

_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : database connection pooling problem
 database connection     -  
 Catch database connection exception     -  
 HTTP POST msg connection close problem     -  
 Restoring SQL Database from the .BAK File Problem     -  
 Closing connection in jsp     -  
 Connection object in jsp     -  
 How to use connection object in jsp     -  
 establish a connection with MySQL in php     -  
 Mobile to PC connection using Bluetooth     -  
 Bluetooth connection using C# (won't work)     -  



Topic Tags

Java JDBC
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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