I am currently trying to debug my Java servlet which is having problems interfacing with a JDBC connection pool.
Note: my servlet was working fine three days ago but now its stopped working so I am assuming its my Sun One webserver or Database problem but with no error message I have no idea what is happening.
My servlet normally returns an XML response through the browser but with this error that is happening I am getting nothing returned just a black file.
After painstakingly putting breakpoints in my code I have isolated where the error occurs but I have no information being returned on this error.
It occurs at this line in my getData() method.
Statement stmt = conn.createStatement();
Here is the full database class
DatabaseLogic Class
public class DatabaseLogic
{
private static Connection conn;
public static void openDatabase() throws IOException, SQLException,
NamingException
{
Context initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup("java:comp/env");
// servlet looks up for a connection pool called "jdbc/POOL"
DataSource ds = (DataSource) envContext.lookup("jdbc/POOL");
// connection is then made/requests to connection pool
try
{
conn = ds.getConnection();
}
catch (SQLException e)
{
String message = "Unable to connect to archive database : " + e;
XMLBuilder.dbError(message);
}
}
public static String getData(String queryId, int requestNumber)
throws SQLException
{
String result = "";
if (queryId != null)
{
try
{
// prepare a statement for use in query;
try
{
result = "This is where it breaks";
Statement stmt = conn.createStatement();
result = "This message does not get returned";
// query parameratised with queryId
String qry = "SELECT RECORD_ID, USER_ID, OPERATION_CD, BUSCOMP_NAME, OPERATION_DT, FIELD_NAME, OLD_VAL, NEW_VAL, AUDIT_LOG, ROW_ID, BC_BASE_TBL FROM S_AUDIT_ITEM WHERE RECORD_ID='"
+ queryId + "'";
try
{
ResultSet results = stmt.executeQuery(qry);
result = XMLBuilder.xmlBuilder(results, queryId,
requestNumber);
// close the connection
stmt.close();
results.close();
}
catch (SQLException e)
{
String message = "A database error occurred : " + e;
XMLBuilder.dbError(message);
}
}
catch (SQLException e)
{
String message = "A database error occurred : " + e;
XMLBuilder.dbError(message);
}
}
catch (Exception e)
{
// log.error("Cannot connect to database :" + e);
String message = "Unable to query the archive database : " + e;
XMLBuilder.dbError(message);
}
}
else
{
// not sure if ever reached
result = "The query parameter is a null value";
}
return result;
}
All information being returned is sent to XMLBuilder to be packaged into an XML however nothing is being returned. Meaning the function in my class is not being called when dealing with exceptions.
XMLBuilder.dbError()
public static String dbError(String message)
{
//added uniquestring generator for id
String uniqueId = UUID.randomUUID().toString();
String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?> "
+ "<SMessage MessageId=\"" + uniqueId + "\" MessageType=\"Integration Object\" IntObjectName=\"Test\" IntObjectFormat=\"Siebel Hierarchical\""
+ " ReturnCode=\"1\" ErrorMessage=\"Database error" + message + ".\">" +
"</SMessage>";
return result;
}
How can I get an error message returned so I can figure out whether this is a server error or it is my servlet
Thankyou
Merry Christmas and Happy Holidays!
You’re calling this method:
but not using its result. So the
resultvariable remain blank, and that’s what you return to the client.I would rather throw all execptions (that you can’t recover from) out to the top level request handler method (otherwise you’ll duplicate all your XML marshalling invocations). Only then should you:
That would give you a more consistent (less repeated) means of handling errors, and you’re not having to present an XML-wrapped error from the depths of your code.
As an aside, your code isn’t thread-safe. You’re calling ‘static’ methods on the class to set up connections etc. You’ll be better off getting the connection, statement etc. all within one service method.