我有一个使用 JNDI 进行连接轮询的 Web 应用程序(数据库服务器是 MySql),
对 Web 应用程序有很多请求,并且它有一个 Web 服务被另一个 Web 应用程序使用。
它工作正常,但一段时间后不起作用,并且在 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 Service request for”字符串,然后将运行“SELECT”命令并在输出中打印,但是当系统崩溃时,收到了对Web服务的请求,但没有继续。
因此查找“+++++=====>>>>”和“+++++=====>>>>”并注意“%%%”之间的差异%>>>>> 开头和结尾的“Web 服务请求”。
输出如下:
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() 。
关于Java连接池(JNDI)半天后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36954056/