我收到如下错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 8,374,094 milliseconds ago. The last packet sent successfully to the server was 8,374,094 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3364)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1983)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at com.mycode.MyLoginDAO.validateUser(MycodeDAO.java:517)
at com.mycode.MyLoginFB.validate(MycodeFB.java:85)
at org.apache.struts.chain.commands.servlet.ValidateActionForm.validate(ValidateActionForm.java:58)
at org.apache.struts.chain.commands.AbstractValidateActionForm.execute(AbstractValidateActionForm.java:120)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:200)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:775)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:704)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:897)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3345)
... 35 more
当我长时间使用下面的代码或向服务器发送请求 n 次时。
我使用以下 context.xml 在 My-sql 上使用 Apache dbcp 配置了我的应用程序
上下文.xml:
<Resource name="jdbc/MYDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="10" maxWait="10000" username="root" password="12345"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/egDB"
removeAbandoned="true" removeAbandonedTimeout="1000"/>
从这个 DBConnection Singleton 类返回到其类和 .jsp 文件中的所有调用方法的连接
DBConnection 类:
package my.code;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConnection implements Serializable {
private static final long serialVersionUID = 1565029633902701330L;
private static DBConnection dbConnection;
private DataSource dataSource;
private Connection connObj;
private Context initContext;
private DBConnection() {
}
public static Connection getDBConnection() {
if (dbConnection == null) {
dbConnection = new DBConnection();
}
Connection connectionObj = dbConnection.getConnection(dbConnection
.getDataSource());
try {
while (connectionObj == null || connectionObj.isClosed())
connectionObj = dbConnection.getConnection(dbConnection
.getDataSource());
} catch (SQLException e) {
e.printStackTrace();
}
return connectionObj;
}
private Connection getConnection(DataSource ds) {
try {
connObj = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connObj;
}
private DataSource getDataSource() {
if (dataSource == null) {
try {
initContext = dbConnection.getContext();
Context envContext = (Context) initContext
.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/NichehireDB");
} catch (NamingException e) {
e.printStackTrace();
}
}
return dataSource;
}
private Context getContext() {
if (initContext == null) {
try {
initContext = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
}
return initContext;
}
}
在 DAO/DTO 类或 jsp 文件中使用 Connection:
package my.code;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class MycodeDAO implements Serializable {
private static final long serialVersionUID = 7146087855230830066L;
private String jobTitle;
private String jobDesc;
public String getJobTitle() {
return jobTitle;
}
public String getJobDesc() {
return jobDesc;
}
public MycodeDAO() {
}
public MycodeDAO(String jobTitle, String jobDesc) {
super();
this.jobTitle = jobTitle;
this.jobDesc = jobDesc;
}
public Vector<MycodeDAO> getSavedJobs(String js_email_id) {
Vector<MycodeDAO> savedJobsVector = new Vector<MycodeDAO>();
Connection connObj = DBConnection.getDBConnection();
PreparedStatement pstmtObj = null;
ResultSet rsObj = null;
PreparedStatement appliedPstmtObj = null;
ResultSet appliedRSObj = null;
try {
pstmtObj = connObj
.prepareStatement(
"select * from jobs where jobid = ?",
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
pstmtObj.setInt(1, 102);
rsObj = pstmtObj.executeQuery();
while (rsObj.next()) {
jobTitle = rsObj.getString("jobTitle");
jobDesc = rsObj.getString("jobShortDesc");
savedJobsVector.add(new MycodeDAO(jobTitle, jobDesc));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connObj != null) {
try {
if (!connObj.isClosed()) {
connObj.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
System.gc();
}
System.out.println("savedjobVector size:\t" + savedJobsVector.size());
return savedJobsVector;
}
}
像上面一样,我有很多 dao、dto 和 jsp 类。在不同的类中,它会引发错误。当我刷新包含 dao/dto 代码的 .jsp 页面时,服务器出现 5xx 错误,当我查看日志文件时,它说使用了 Max active sql connections。
请让我知道我哪里出错了。请做必要的事情来解决这个异常。注意:MySQL 最大连接数:350