当我尝试通过java progarm访问时,我有mysql database on aws
它会在一段时间内提供异常,有时会提供其工作
这是我的java代码
package com.iseva.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Connectionfactory.ConnectionFactory;
/**
* Servlet implementation class ValidateUser
*/
@WebServlet("/ValidateUser")
public class ValidateUser extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection cn=null;
public void init()
{
cn=ConnectionFactory.getConnection();
System.out.println("connection");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String logintype=request.getParameter("t1");
String username= request.getParameter("t2");
String password= request.getParameter("t3");
PreparedStatement ps=null;
ResultSet rs=null;
String result="";
try {
ps=cn.prepareStatement("select * from mydb.isevausers where logintype=? and passwords=? and username=?");
ps.setString(1,logintype);
ps.setString(2,password);
ps.setString(3,username);
rs=ps.executeQuery();
boolean b=rs.next();
// RequestDispatcher dispatcher=null;
if(b==true)
{
/* response.sendRedirect("form_1_IRCTC_PaymentEntry.html");*/
result="form1";
/*dispatcher =getServletContext().getRequestDispatcher("/form_1_IRCTC_PaymentEntry.html");
dispatcher.forward(request,response);*/
System.out.println("entered");
}
else{
result="notfound";
System.out.println("not found");
}
response.getWriter().write(result);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null)
{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
当我尝试访问数据库时,它会提供以下异常
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1187)
at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1182)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4071)
at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4040)
at com.iseva.Servlet.ValidateUser.doGet(ValidateUser.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1519)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1475)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
如何解决这个问题?
这是database
或我的代码的问题,请帮助我。
我按照网站的建议关闭了finally块中的所有连接,即使它给出相同的。
我们的mysql
数据库idle
在一段时间内可能会发生此异常。是吗?
如果是的话,我该怎么办。或者如何解决它。
当我刷新服务器
时,此异常也会出现java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(Unknown Source)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:64)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
Dec 04, 2017 3:34:01 PM org.apache.catalina.loader.WebappClassLoaderBase checkStateForResourceLoading
INFO: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1353)
at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1029)
at com.mysql.jdbc.AbandonedConnectionCleanupThread.checkContextClassLoaders(AbandonedConnectionCleanupThread.java:90)
at com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:63)
答案 0 :(得分:0)
在一次doGet()调用之后,您的连接对象正在关闭,因此在第一次使用后下次调用它将导致错误,因为您没有打开连接。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//opening a connection before accessing db
cn=ConnectionFactory.getConnection();
String logintype=request.getParameter("t1");
String username= request.getParameter("t2");
String password= request.getParameter("t3");
PreparedStatement ps=null;
ResultSet rs=null;
String result="";
try {
ps=cn.prepareStatement("select * from mydb.isevausers where logintype=? and passwords=? and username=?");
ps.setString(1,logintype);
ps.setString(2,password);
ps.setString(3,username);
rs=ps.executeQuery();
boolean b=rs.next();
// RequestDispatcher dispatcher=null;
if(b==true)
{
/* response.sendRedirect("form_1_IRCTC_PaymentEntry.html");*/
result="form1";
/*dispatcher =getServletContext().getRequestDispatcher("/form_1_IRCTC_PaymentEntry.html");
dispatcher.forward(request,response);*/
System.out.println("entered");
}
else{
result="notfound";
System.out.println("not found");
}
response.getWriter().write(result);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null)
{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}