Search code examples
javamysqlweb-servicesjndiconnection-pooling

Java connection pooling (JNDI) not work after half a day


I have a web app which uses JNDI for connection polling (DB server is MySql),

There is many request to web application and it has a web service used by one other web application.

It work fine but after a while in not works and after some no-processed commands in web service, the error below appears:

java.sql.SQLException: Cannot get a connection, general error javax.naming.NameNotFoundException: Name [java:/comp/env] is not bound in this Context. Unable to find [java:].

I copy app aout put in below.

please notice i force app to print "+++++=====>>>> number of connection X"

when for new connection ( if it was new connection X increased)

and after closing connection "------=====>>>> number of connection X"

note 2: Whenever web service is called a "%%%%>>>>> web Service request for" string will be printed in the out put and then a "SELECT" command will run and print in the out put,but when system crashed the request to the web service received, but not proceed.

So look up for "+++++=====>>>>" and "+++++=====>>>>" and pay attention in defrence between "%%%%>>>>> web Service request for" in the beginning and at the end.

The out put is like below:

java out put file

public void close() throws SQLException {
    connection.close();
    counter--;
    ServerLog.Print("------=====>>>> number of connection " + counter);
    ServerLog.Print("Database connection is closed ...");
}

This are some methods in below:

public jjDatabaseWeb() throws SQLException, NamingException {
    ctx = new InitialContext();
    Context initCtx = (Context) ctx.lookup("java:/comp/env");
    DataSource ds = (DataSource) initCtx.lookup("jdbc/MyDB");
    connection = ds.getConnection();
    counter++;
    ServerLog.Print("+++++=====>>>> number of connection " + counter);
}

And in below is web service method that which calls whit other services in LAN :

/**
 * this Method returns a string result; if(result =="" OR result==null){ do
 * your work }else{ show user result as message (result is HTML ) }
 *
 * @param stdNubmer
 * @param nationalId Is not important
 * @return String result (as HTML )
 * @throws javax.naming.NamingException
 */
@WebMethod(operationName = "studentCheck")
public String studentCheck(@WebParam(name = "stdNubmer") String stdNubmer, @WebParam(name = "nationalId") String nationalId) {

    try {
        ServerLog.Print("%%%%>>>>> web Service request for :" + stdNubmer);
        try {
            jjDatabaseWeb db = new jjDatabaseWeb();
            DefaultTableModel dtm = db.Select(StdInfo.tableName, StdInfo._step + "," + StdInfo._alerts + "," + StdInfo._lastAertDate + "," + StdInfo._name + "," + StdInfo._family, StdInfo._stdNumber + "=" + stdNubmer);
            List<Map<String, Object>> row = jjDatabaseWeb.separateRow(dtm);
            db.close();//
            if (row.size() == 1) {
                if (!row.get(0).get(StdInfo._step).toString().equalsIgnoreCase("5")) {
                    int date = new jjCalendar_IR().getDBFormat_8length();
                    int lastAlertDate = Integer.parseInt(row.get(0).get(StdInfo._lastAertDate).toString());
                    int alerts = Integer.parseInt(row.get(0).get(StdInfo._alerts).toString());
                    if ((date - 5) <= lastAlertDate) {
                        return "";
                    }
                    StringBuilder html = new StringBuilder("");
                    html.append("<HTML>...</HTML>");
                    return html.toString();
                }
            }
            return "";
        } catch (NamingException ex) {
            Logger.getLogger(csaWebService.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (SQLException ex) {
        Logger.getLogger(csaWebService.class.getName()).log(Level.SEVERE, null, ex);
        ServerLog.Print(ex);
        Server.ErrorHandler(ex);
        return "";
    }
    return "";
}

}


Solution

  • There was so many connections that witch opened and not closed .

    Be careful about close() for connections.