tomcat服务器宕机解决方案
报错信息:
java.lang.Object.wait(NativeMethod) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
每次出现这个报错都会导致tomcat应用服务器停机,加了下面的java代码后就再也没有停过了。
解决办法:
编写Java代码
packagecn.listener; importjava.sql.Driver; importjava.sql.DriverManager; importjava.sql.SQLException; importjava.util.Enumeration; importjavax.servlet.ServletContextEvent; importjavax.servlet.ServletContextListener; importjavax.servlet.annotation.WebListener; importcom.mysql.jdbc.AbandonedConnectionCleanupThread; @WebListener publicclassContextFinalizerimplementsServletContextListener{ publicvoidcontextInitialized(ServletContextEventsce){ } publicvoidcontextDestroyed(ServletContextEventsce){ Enumerationdrivers=DriverManager.getDrivers(); Driverd=null; while(drivers.hasMoreElements()){ try{ d=drivers.nextElement(); DriverManager.deregisterDriver(d); System.out.println(String.format("ContextFinalizer:Driver%sderegistered",d)); }catch(SQLExceptionex){ System.out.println(String.format("ContextFinalizer:Errorderegisteringdriver%s",d)+":"+ex); } } try{ AbandonedConnectionCleanupThread.shutdown(); }catch(InterruptedExceptione){ System.out.println("ContextFinalizer:SEVEREproblemcleaningup:"+e.getMessage()); e.printStackTrace(); } } }
@WebListener,这个注解相当于在web.xml配置如下内容
cn.listener.ContextFinalizer
解决方案可以参考如下网址
当然还有就是我再参考这个解决方案的时候,发现mysql-connection如果版本过低会导致上述列出的Java代码报错,通过提高mysql-connection.java的版本即可解决该问题
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。