Hi people,
I am herewith attaching my connectionpool code and the code that
implements it. I need to make the user to wait till he gets his
connection from the pool and to notify if a connection is avaiable.
Everything is updated perfectly but when i am notifying i am getting the
following error for a single user itself i tried with 3 users accessing
that stil i am getting that error.
java.lang.IllegalMonitorStateException : current thread not owner
Please help me to solve this error how shall a make user wait if he doesnt
have his connection and to notify him if a connection is got.
Thanks
Srini
#-----------------------------------------------------------------------#
# #
# "ARISE AWAKE and stop not till the GOAL is reached" #
# #
# [EMAIL PROTECTED] #
#-----------------------------------------------------------------------#
/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
*/
import java.util.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class ConnPool extends HttpServlet
{
private int MAX_CONNECTIONS = 1;
private Stack pool;
private String dburl, passwd, name;
private Connection[] con;
ConnPool(String dburl, String passwd, String name)
{
pool = new Stack();
con = new Connection[MAX_CONNECTIONS];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
for
(int i=0; i < MAX_CONNECTIONS; i++)
{
System.out.println("Creating a connection");
con[i] = DriverManager.getConnection(dburl, passwd, name);
pool.push(con[i]);
System.out.println("Successfully pushed");
}
}
catch(ClassNotFoundException
e)
{
getServletContext().log(e, "Class Not found");
}
catch(SQLException sqle)
{
getServletContext().log(sqle, "SQL Error");
}
}
//This is to get the connection first it will check for
//anything that is available if yes it will return
//a connection.
public Connection getConnection()
{
Connection getcon = null;
try
{
while (pool.empty())
{
System.out.println("The current status inside while is " + pool.empty());
System.out.println("I
am entering the wait pool");
wait();
System.out.println("Ur
in wait stage");
}
synchronized(this)
{
try
{
getcon
= (Connection)pool.pop();
System.out.println("The current status of the stack inside " + pool.empty());
}
catch(EmptyStackException ese)
{
System.out.println(ese.getMessage());
}
}
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
return getcon;
}
public void releaseConnection(Connection give)
{
synchronized(this)
{
pool.push(give);
}
}
public void release()
{
notify();
System.out.println("yes u have been notified");
}
}
/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class realproj extends HttpServlet
{
private ConnPool cp;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
cp = new ConnPool("jdbc:odbc:usrpass", "sa", "sa");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
HttpSession session = req.getSession(true);
PrintWriter out = res.getWriter();
res.setContentType("text/html");
Connection con = null;
Statement stmt;
ResultSet rs;
String usrname = req.getParameter("fUsr");
String passwd = req.getParameter("fPaswd");
try
{
con = cp.getConnection();
System.out.println("Got a
connection");
con.setAutoCommit(false);
stmt = con.createStatement();
stmt.executeUpdate
("Insert into usrpastab "+
"VALUES('"+
usrname +"', '"+ passwd +"')");
con.commit();
stmt.close();
con.close();
System.out.println("Releasing this
connection");
cp.releaseConnection(con);
System.out.println("Released the
connetion");
cp.release();
}
catch(SQLException e)
{
System.out.println("There is a sql
error" + e.getMessage());
}
}
}
