Hi team,
I apologize for attaching my code along with this mail it was
attached with a sole intention that i will give a clue of what i was
looking for.
Everything is good for the first time meaning if i have a client
and he access the servlet everything is okay and good, but when the same
client access the servlet again eventhough a connection is available it is
not giving it a Application Error is occuring in "java.exe". When i try
the 3rd time it is giving me a connection but values are not updated in
the database. A sql error "There is a sql error general error" occurred
and i was forced to reboot the system to get my things done. Please help
me to solve this error.
i am using jdk1.2, servletrunner iis4.0. is this a bug with the
serlvetrunner i can get what i want if i try with any servlet engines??
Thanks and apology for attaching the code,
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 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");
stmt = con.createStatement();
System.out.println("Created
Statement");
stmt.executeUpdate
("Insert into usrpastab "+
"VALUES('"+
usrname +"', '"+ passwd +"')");
System.out.println("Values updated");
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());
}
}
}
/*
// header - edit "Data/yourJavaHeader" to customize
// contents - edit "EventHandlers/Java file/onCreate" to customize
//
*/
import java.util.*;
import java.sql.*;
public class ConnPool
{
private int MAX_CONNECTIONS = 1;
private Stack pool;
private String dburl, passwd, name;
private Connection[] con;
private boolean status = false;
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)
{
System.out.println(e.getMessage());
}
catch(SQLException sqle)
{
System.out.println("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 synchronized boolean checkStatus()
{
if (pool.isEmpty())
{
status = true;
}
else
{
status = false;
}
return status;
}
public Connection getConnection()
{
Connection getcon = null;
System.out.println("The current status of the stack before
connection outside try" + pool.empty());
try
{
synchronized(con)
{
System.out.println("Returning
the status code "+status);
while (status)
{
System.out.println("Entering the while block");
wait();
System.out.println("Waiting..");
}
}
try
{
System.out.println("Entering the
connection block");
getcon = (Connection)pool.pop();
}
catch(EmptyStackException ese)
{
System.out.println(ese.getMessage());
}
}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
return getcon;
}
public void releaseConnection(Connection give)
{
synchronized(this)
{
System.out.println("Entering the
release area");
System.out.println("The current status
of the stack before releasing " + pool.empty());
pool.push(give);
System.out.println("The current status
of the stack after releasing " + pool.empty());
if (!status)
{
System.out.println("Total no of connections available" + pool.size());
notify();
}
System.out.println("The current status of the
stack after releasing connection " + pool.empty());
System.out.println("Total no of connections
available" + pool.size());
System.out.println("Leaving the notify area");
}
}
/* public void release()
{
if (status)
{
notify();
status = false;
System.out.println("yes u have been
notified");
System.out.println("The current status
of the stack in release " + pool.empty());
}
}*/
}
