• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems with ManagedConnection

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there:
I'm a novice in Java and my application keeps crashing with the same error. It looke I'm not closing something, and I just can't find the fix.
This is a sample of the code. Please, please someone tell me what is wrong with it:

<%@ page import = "javax.naming.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.lang.*"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "javax.sql.*"%>

<%
String sMode = "";
String sSiteID = "";
String sAreaID = "";
String sAreaName = "";
String sJSP_Action= "";

if (request.getParameter("MODE") != null) {
sMode= request.getParameter("MODE").toString();
}else {
sMode= "0";
}

if (request.getParameter("fld_SiteID") != null) {
sSiteID= request.getParameter("fld_SiteID").toString();
}else {
sSiteID= "0";
}

if (request.getParameter("fld_AreaID") != null) {
sAreaID= request.getParameter("fld_AreaID").toString();
}else {
sAreaID= "0";
}


if (request.getParameter("txt_AreaName") != null) {
sAreaName= request.getParameter("txt_AreaName").toString();
}else {
sAreaName= "";
}

if (request.getParameter("JSP_ACTION") != null) {
sJSP_Action= request.getParameter("JSP_ACTION").toString();
}else {
sJSP_Action= "";
}

InitialContext ctxt = new InitialContext();
DataSource ds = (DataSource) ctxt.lookup("java EMO_CARACAS");
Connection con = ds.getConnection();
Connection con0 = ds.getConnection();
Connection con1 = ds.getConnection();
Statement stmt = null;
ResultSet rs = null;
String sSQL = "";
int rows= 0;

try {

if (sJSP_Action.equals("Aceptar") || sJSP_Action.equals("Click_Accept")){
if (sMode.equals("ADDAREA")){
sSQL = "INSERT INTO tbl_Areas (SiteID, AreaName)";
sSQL = sSQL + " VALUES(" + sSiteID + ",'" + sAreaName + "')";
}else{
sSQL = "UPDATE tbl_Areas ";
sSQL = sSQL + " SET AreaName='"+ sAreaName+ "'";
sSQL = sSQL + " WHERE ID="+ sAreaID;
}
out.print(sSQL);
stmt= con.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();

}else if (sJSP_Action.equals("Borrar") || sJSP_Action.equals("Click_Delete")){
sSQL = "DELETE FROM tbl_Areas WHERE ID=" + sAreaID;
stmt= con.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();

sSQL = "DELETE FROM tbl_Rooms WHERE AreaID=" + sAreaID;
stmt= con0.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con0!=null) con0.close();

sSQL = "DELETE FROM tbl_Elements WHERE AreaID=" + sAreaID;
stmt= con1.createStatement();
rows= stmt.executeUpdate (sSQL);
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con1!=null) con1.close();
}

} finally {
con1.close();
con0.close();
con.close();
}
response.sendRedirect("survey_areas_list.jsp?SiteID=" + sSiteID);

%>
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rafael,
Welcome to JavaRanch! What is the error it is crashing with?

One thing that jumps out at me is that you are using three connections instead of 1. Try simplifying the code to reuse the same connection within the page.

Also, see if you can simplify the page (like doing one query, then two queries.) This will help us all further narrow down the problem.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add...

Yes the actual error message would be good.

There are number of strange elements in your code. Like the ResultSet rs which is NEVER used as far as I can see. You do a lot of if(rs!=null) but I can't see any reason for it not to be null.

I also find it odd that you use three seperate connections but reuse the statement. I don't think any of this will cause an error but it sure is odd.

I also notice you seem to have try with no catch but a finally. It might be nice if you caught the exception and did something... printing it out would be good for starters but in future you could take other appropriate action.
 
reply
    Bookmark Topic Watch Topic
  • New Topic