Link to home
Start Free TrialLog in
Avatar of mwari
mwari

asked on

getting error java.sql.SQLException No available resource. Wait-time expired. when trying to save into db.

I am getting the following exception when I try to save info into a db.
Basically what happens is I save information from a csv file into a csvfile table, if the operation is succesfull then I retrieve the information from csvfile table and save it to daceaccount table, thereafter to get the total I read the daceaccount table and load the Reference number and the total, It takes about a long time to execute about 4mins and when it does, it does not save anything. When I look in the logs I see the exception:

 java.sql.SQLException No available resource. Wait-time expired

This is done inside a servlet here is the code snippet of the code that does this:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        CsvDateUtil csvdate= new CsvDateUtil();
        out.println("<input type=hidden name=proc value=proc>");
        /* checking if file saved file exists, if so read the
         file using CSVFileReader process method, which will
         read the file and write it to the database.*/
       
       
        today= Calendar.getInstance();
        if(request.getParameter("files")!=null){
            if(request.getParameter("files").endsWith(".csv")){  
                try{
                    con= c.getJndiConnection();
                    pstmt = con.createStatement();
                   
                    csv.setCSVDirectory(request.getParameter("pathtocsv"));
                    csv.setSkippedRows(3);
                    List two= csv.getResults(2, request.getParameter("csvfilename").replaceAll(".csv", ""));
                    List four= csv.getResults(4, request.getParameter("csvfilename").replaceAll(".csv", ""));
                    List five= csv.getResults(5, request.getParameter("csvfilename").replaceAll(".csv", ""));
                    List six= csv.getResults(6, request.getParameter("csvfilename").replaceAll(".csv", ""));
                    PropertyReader reader =new PropertyReader();
                   
                    for (int i=0; i < two.size(); i++){
                        String insvalues="insert into csvfile values ('"+new CsvDateUtil().date(two, i, "")+"', '"+four.get(i)+"', '"+five.get(i)+"', '"+six.get(i).toString().trim()+"')";
                        chk= pstmt.executeUpdate(insvalues);
                        System.out.println("---status of insert csvfile table "+chk);
                    }
                   
                    if(chk > 0){
                        m = pstmt.executeUpdate("insert into daceaccount select Dates, Amount, Transaction, Reference from csvfile WHERE NOT EXISTS (SELECT daceaccount.Dates, daceaccount.Amount, daceaccount.Transaction, daceaccount.Reference from daceaccount where daceaccount.Reference = csvfile.Reference and daceaccount.Dates = csvfile.Dates and csvfile.Amount = daceaccount.Amount)");
                        if(m > 0 ){
                            try{
                                k= pstmt.executeUpdate("delete from total");
                                k= pstmt.executeUpdate(" insert into total select Reference, sum(Amount) from daceaccount group by Reference");
                                if(k > 0){
                                    System.out.println("---Successfully inserted "+k+" rows in total");
                                }
                            }catch(Exception e){
                                e.printStackTrace();
                            }
                            System.out.println("--Successfully modified " + m + " rows.\n");
                           
                        }else{
                            System.out.println("---(Class Test), No updates required");
                        }
                    }else{
                        System.out.println("unable to save info to csvfile table");
                    }
                } catch (SQLException se) {
                    System.out.println("We got an exception while executing our query:" +
                    "that probably means our SQL is invalid");
                    se.printStackTrace();
                    System.exit(1);
                } finally {
                    try { pstmt.close(); } catch(SQLException e) { e.printStackTrace(); }
                    try { con.close(); } catch(SQLException e) { e.printStackTrace(); }
                }
               
            }else if(!request.getParameter("files").endsWith(".csv")){                
                out.println("<center><font color=red size=2 face=Tahoma>Wrong file type (must be csv file)</font></center>");                
            }
        }
        if(request.getAttribute("filename")!=null){
            out.println("<html>");
            out.println("<body>");
            out.println("<center><P>&nbsp;</P>");
            out.println("<P><FONT face=Tahoma size=2>The following file has been saved to directory<FONT color=#0000ff> /tmp</FONT> click continue to proceed.</FONT></P>");
            out.println("<FORM METHOD=POST ACTION=/accounting/servlet/utilities.InterceptorServlet>");
            out.println("<input type=hidden name=uploads value=uploads>");
            out.println("<P><FONT face=Tahoma size=2></FONT><INPUT TYPE=text NAME=files value='"+request.getAttribute("filepath")+"/"+request.getAttribute("filename")+"'><INPUT type=submit value=continue name=continue></P>");
            out.println("<input type=hidden name=pathtocsv value='"+request.getAttribute("filepath")+"'>");
            out.println("<input type=hidden name=csvfilename value='"+request.getAttribute("filename")+"'>");
            out.println("</FORM>");
            out.println("<P>&nbsp;</P></center>");
            out.println("</body>");
            out.println("</html>");
           
        }        
        out.close();
    }
   


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What other output are you getting - of your own debug statements i mean?
Avatar of mwari
mwari

ASKER

no other debugging information of mine is printed.
Go back then. Put something before and after this:

>>con= c.getJndiConnection();
Avatar of mwari

ASKER

I have done that and I even went on to use a lookup from my servlet as follows. I get the following error.

CORE3282: stdout: ---proc4---
[21/Aug/2005:15:54:21] SEVERE (12005):       ApplicationDispatcher[/accounting] Servlet.service() for servlet CVSProcessServlet threw exception
      java.lang.NullPointerException
      at com.alindigo.costrecovery.jdbcUtilities.CSVProcessServlet.processRequest(CSVProcessServlet.java:237)
      at com.alindigo.costrecovery.jdbcUtilities.CSVProcessServlet.doPost(CSVProcessServlet.java:279)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)

here is the changed code:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        CsvDateUtil csvdate= new CsvDateUtil();
        out.println("<input type=hidden name=proc value=proc>");
        /* checking if file saved file exists, if so read the
         file using CSVFileReader process method, which will
         read the file and write it to the database.*/
       
         try{
        ic = new InitialContext();
        ds = (DataSource)ic.lookup("jdbc/GDACEapplications");
        con = ds.getConnection();
        }catch(Exception e){
         e.printStackTrace();  
        }
       
       
        System.out.println("---proc1---");
        today= Calendar.getInstance();
        System.out.println("---proc2---");
        if(request.getParameter("files")!=null){
            System.out.println("---proc3---");
            if(request.getParameter("files").endsWith(".csv")){
                try{
                    System.out.println("---proc4---");
                    con= c.getJndiConnection();
                    System.out.println("---proc5---");
                    pstmt = con.createStatement();
                    System.out.println("---proc6---");
                   
                    csv.setCSVDirectory(request.getParameter("pathtocsv"));
                    System.out.println("---proc7---");
                    csv.setSkippedRows(3);
                    System.out.println("---proc8---");
                    List two= csv.getResults(2, request.getParameter("csvfilename").replaceAll(".csv", ""));
                    List four= csv.getResults(4, request.getParameter("csvfilename").replaceAll(".csv", ""));
                    List five= csv.getResults(5, request.getParameter("csvfilename").replaceAll(".csv", ""));
                    List six= csv.getResults(6, request.getParameter("csvfilename").replaceAll(".csv", ""));
                   
                    PropertyReader reader =new PropertyReader();
                   
                    System.out.println("---proc7---");
                   
                    for (int i=0; i < two.size(); i++){
                        System.out.println("---proc8---");
                        String insvalues="insert into csvfile values ('"+new CsvDateUtil().date(two, i, "")+"', '"+four.get(i)+"', '"+five.get(i)+"', '"+six.get(i).toString().trim()+"')";
                        chk= pstmt.executeUpdate(insvalues);
                        System.out.println("---status of insert csvfile table "+chk);
                    }
                   
                    if(chk > 0){
                        System.out.println("---proc9---");
                        m = pstmt.executeUpdate("insert into daceaccount select Dates, Amount, Transaction, Reference from csvfile WHERE NOT EXISTS (SELECT daceaccount.Dates, daceaccount.Amount, daceaccount.Transaction, daceaccount.Reference from daceaccount where daceaccount.Reference = csvfile.Reference and daceaccount.Dates = csvfile.Dates and csvfile.Amount = daceaccount.Amount)");
                        if(m > 0 ){
                            System.out.println("---proc10---");
                            try{
                                System.out.println("---proc11---");
                                k= pstmt.executeUpdate("delete from total");
                                k= pstmt.executeUpdate(" insert into total select Reference, sum(Amount) from daceaccount group by Reference");
                                if(k > 0){
                                    System.out.println("---Successfully inserted "+k+" rows in total");
                                }
                            }catch(Exception e){
                                e.printStackTrace();
                            }
                            System.out.println("--Successfully modified " + m + " rows.\n");
                           
                        }else{
                            System.out.println("---(Class Test), No updates required");
                        }
                    }else{
                        System.out.println("unable to save info to csvfile table");
                    }
                } catch (SQLException se) {
                    System.out.println("We got an exception while executing our query:" +
                    "that probably means our SQL is invalid");
                    se.printStackTrace();
                    System.exit(1);
                } finally {
                    try { pstmt.close(); } catch(SQLException e) { e.printStackTrace(); }
                    try { con.close(); } catch(SQLException e) { e.printStackTrace(); }
                }
               
            }else if(!request.getParameter("files").endsWith(".csv")){
                out.println("<center><font color=red size=2 face=Tahoma>Wrong file type (must be csv file)</font></center>");
            }
        }
        if(request.getAttribute("filename")!=null){
            out.println("<html>");
            out.println("<body>");
            out.println("<center><P>&nbsp;</P>");
            out.println("<P><FONT face=Tahoma size=2>The following file has been saved to directory<FONT color=#0000ff> /tmp</FONT> click continue to proceed.</FONT></P>");
            out.println("<FORM METHOD=POST ACTION=/accounting/servlet/utilities.InterceptorServlet>");
            out.println("<input type=hidden name=uploads value=uploads>");
            out.println("<P><FONT face=Tahoma size=2></FONT><INPUT TYPE=text NAME=files value='"+request.getAttribute("filepath")+"/"+request.getAttribute("filename")+"'><INPUT type=submit value=continue name=continue></P>");
            out.println("<input type=hidden name=pathtocsv value='"+request.getAttribute("filepath")+"'>");
            out.println("<input type=hidden name=csvfilename value='"+request.getAttribute("filename")+"'>");
            out.println("</FORM>");
            out.println("<P>&nbsp;</P></center>");
            out.println("</body>");
            out.println("</html>");
           
        }
        out.close();
    }
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mwari

ASKER

you have just nailed the problem c refers my JNDIConnectionBean, it looks like I would have to rethink this class..
After removing it and doing a Direct lookup it now works well, Thanks you ;-)
:-)