各位大虾:我们有一个项目需要使用连接池,我写了一个连接池但使用连接池后会造成applet频繁无法找到,搞不清什么原因,请教高手
各位大虾:我们有一个项目需要使用连接池,我写了一个连接池但使用连接池后会造成applet频繁无法找到(在javascript中调用时变为undefine)频率比直接例用connection高出很多,以下是连接池代码,请教高手,看看能不能查出原因:
import java.util.*;
import java.sql.*;
/**
* A Singleton pool of connections to the database
*/
public class PMConnectionPool
{
private static PMConnectionPool myPool = null;
private Vector locked = null;
private Vector unlocked = null;
private long expirationTime;
private String sUrl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=PathMaker;User=sa;Password=";
/**
* Default constructor
*/
private PMConnectionPool()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch (Exception e)
{
e.printStackTrace();
}
locked = new Vector();
unlocked = new Vector();
expirationTime = 30000;
System.out.println("Pool created");
}
/**
* Get the only pool
*/
public static PMConnectionPool getInstance()
{
if (myPool == null)
{
myPool = new PMConnectionPool();
}
return myPool;
}
/**
* Check out a connection from the pool. If the pool is empty, create a new connection
*
* @return a connection to the database
*/
public synchronized Connection checkOut()
{
System.out.print("Enter checkout ");
long now = System.currentTimeMillis();
Connection con = null;
if (unlocked.size() > 0)
{
Enumeration enum = unlocked.elements();
while (enum.hasMoreElements())
{
con = (Connection)(enum.nextElement());
if (validate (con))
{
//System.out.println("!!!!!!!!!!!!GOOD");
//Good connection
unlocked.remove(con);
locked.addElement(con);
break;
}
else
{
//System.out.println("!!!!!!!!!!!!BAD");
//Bad connection
unlocked.remove(con);
expire(con);
con = null;
}
}
}
else
{
//System.out.println("!!!!!!!!!!!!EMPTY");
}
//no connections available, create a new one
if (con == null)
{
System.out.print("Checkout -> Connecting before ");
con = create();
locked.add(con);
}
System.out.print("Checkout -> Connecting End ");
return con;
}
/**
* Check in a connection to the pool. If the pool has ten or more connections, close the connection
*
* @param a connection to check in
*/
public synchronized void checkIn(Connection con)
{
locked.remove(con);
unlocked.add(con);
}
/**
* Obtain a connection to the database.
*
* @return a connection to the database.
*/
private Connection create()
{
try
{
Connection con = DriverManager.getConnection(sUrl);
//System.out.println("create connection.........................");
return con;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
/**
* Expire a connection to the database.
*
* @param con the connection to be closed
*/
private void expire(Connection con)
{
//System.out.println("*************TO CLOSE");
try
{
if (!con.isClosed())
{
con.close();
//System.out.println("close connection.........................");
}
else
{
//System.out.println("connection has been closed........................");
}
}
catch (SQLException sqle)
{
System.err.println("-------------------------------------");
System.out.println("An error occurred while closing connection. sqle=" + sqle.getMessage());
while (sqle != null)
{
sqle.printStackTrace();
System.err.println("Previous............");
sqle = sqle.getNextException();
}
System.err.println("-------------------------------------");
}
}
/**
* Validate a connection
*
* @param con the connection
* @return true if it's a good connection, false otherwise
*/
private boolean validate(Connection con)
{
boolean bRetVal = true;
try
{
if (con == null || con.isClosed())
bRetVal = false;
}
catch(SQLException e)
{
e.printStackTrace();
bRetVal = false;
}
return bRetVal;
}
}