I need help to solve the errors that I am getting for my connection pooling code:-
Generated
servlet error:
Syntax error on token "import", delete this token
Generated servlet error:
Syntax error on tokens, delete these tokens
Please do help, thanks!
THE CODE:
<%@ page import="javax.naming.*,java.sql.*,java.io.*,java.lang.*,java.text.*,java.util.*"%>
<%
public class ConnectionPool implements Runnable
{
private int m_InitialConnectionCount = 5; // Number of initial connections to make.
private Vector m_AvailableConnections = new Vector();// A list of available connections for use.
private Vector m_UsedConnections = new Vector();// A list of connections being used currently.
private
String m_URLString = null;// The URL string used to connect to the database
private String m_UserName = null; // The username used to connect to the database
private String m_Password = null;// The password used to connect to the database
private
Thread m_CleanupThread = null;// The cleanup thread
//Constructor
public ConnectionPool(String urlString, String user, String passwd) throws SQLException
{
// Initialize the required parameters
m_URLString = urlString;
m_UserName = user;
m_Password = passwd;
for(int cnt=0; cnt<m_InitialConnectionCount; cnt++)
{
m_AvailableConnections.addElement(getConnection());// Add a new connection to the available list.
}
// Create the cleanup thread
m_CleanupThread = new Thread(this);
m_CleanupThread.start();
}
private Connection getConnection() throws SQLException
{
return DriverManager.getConnection(m_URLString, m_UserName, m_Password);
}
public synchronized Connection checkout() throws SQLException
{
Connection newConnxn = null;
if(m_AvailableConnections.size() == 0)
{
newConnxn = getConnection();// Im out of connections. Create one more.
m_UsedConnections.addElement(newConnxn);// Add this connection to the "Used" list.
// We dont have to do anything else since this is
// a new connection.
}
else
{
// Connections exist !
newConnxn = (Connection)m_AvailableConnections.lastElement();// Get a connection object
m_AvailableConnections.removeElement(newConnxn);// Remove it from the available list.
m_UsedConnections.addElement(newConnxn); // Add it to the used list.
}
return newConnxn;// Either way, we should have a connection object now.
}
public synchronized void checkin(Connection c)
{
if(c != null)
{
m_UsedConnections.removeElement(c);// Remove from used list.
m_AvailableConnections.addElement(c); // Add to the available list
}
}
public int availableCount()
{
return m_AvailableConnections.size();
}
public void run()
{
try
{
while(true)
{
synchronized(this)
{
while(m_AvailableConnections.size() > m_InitialConnectionCount)
{
// Clean up extra available connections.
Connection c = (Connection)m_AvailableConnections.lastElement();
m_AvailableConnections.removeElement(c);
c.close(); // Close the connection to the database.
}
// Clean up is done
}
out.print("CLEANUP : Available Connections : " + availableCount());
// Now sleep for 1 minute
Thread.sleep(60000 * 1);
}
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
// Example code using ConnectionPool.
import java.sql.*;
public class Main
{
public static void main (String[] args)
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E)
{
out.print("Unable to load driver.");
E.printStackTrace();
}
try
{
ConnectionPool cp = new ConnectionPool("
jdbc:mysql://localhost/publications", "books", "books");
Connection []connArr = new Connection[7];
for(int i=0; i<connArr.length;i++)
{
connArr[i] = cp.checkout();
out.print("Checking out..." + connArr[i]);
out.print("Available Connections ... " + cp.availableCount());
}
for(int i=0; i><connArr.length;i++)
{
cp.checkin(connArr[i]);
out.print("Checked in..." + connArr[i]);
out.print("Available Connections ... " + cp.availableCount());
}
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
%>