Java 半天后连接池 (JNDI) 不工作

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

我有一个使用 JNDI 进行连接轮询的 Web 应用程序(数据库服务器是 MySql),

有很多对网络应用程序的请求,它有一个网络服务被另一个网络应用程序使用。

它工作正常,但过了一会儿就不能工作了,在 web 服务中一些未处理的命令后,出现以下错误:

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:].

我把应用程序复制到下面。

请注意我强制应用程序打印“++++++=====>>>> 连接数 X”

新连接时(如果是新连接X增加)

关闭连接后“------=====>>>>连接数X”

注意 2:每当调用 web 服务时,“%%%%>>>>> web 服务请求”字符串将打印在输出中,然后 "SELECT" 命令将 运行 并在输出中打印,但是当系统崩溃时,收到了对 Web 服务的请求,但没有继续。

所以查找“+++++=====>>>>”和“+++++=====>>>>”并注意“%”之间的差异%%%>>>>> Web 服务请求在开头和结尾。

输出如下:

java out put file

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

下面是一些方法:

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);
}

下面是调用 LAN 中其他服务的 Web 服务方法:

/**
 * 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 "";
}

}

连接太多, 打开但没有关闭。

注意连接的 close()。