关闭Tomcat报错The web application [my_login] appears to have started a thread named [mysql-cj-abandoned-connection-cleanup] but has failed to stop it
1.原因是在关闭Tomcat服务时,数据库的(mysql和oracle)驱动此时还无法释放。
2.针对驱动无法释放,可以通过ServlectContextListner事件的contextDestroyed函数里加一句代码,在服务停止前释放所有的驱动Driver
3.释放驱动代码:com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.uncheckedShutdown();
如图下代码33行的 AbandonedConnectionCleanupThread.shutdown();这句可以放在第23行的 while (drivers.hasMoreElements()) while循环外面,度娘说先删除驱动,再释放驱动资源,但是代码可以换过来,先释放或者先关闭服务都可以,本人亲自试过运行ok
- DriverManager.deregisterDriver(d); //关闭tomcat服务
- AbandonedConnectionCleanupThread.shutdown();//是释放驱动资源 列如:mysql-connector-java-8.0.28-bin.jar是项目的驱动连接桥
package reyo.sdk.utils.mysql;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.mysql.jdbc.AbandonedConnectionCleanupThread;
@WebListener
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
}
public void contextDestroyed(ServletContextEvent sce) {
Enumeration<Driver> drivers = DriverManager.getDrivers();
Driver d = null;
while (drivers.hasMoreElements()) {
try {
d = drivers.nextElement();
DriverManager.deregisterDriver(d);
System.out.println(String.format("ContextFinalizer:Driver %s deregistered", d));
} catch (SQLException ex) {
System.out.println(String.format("ContextFinalizer:Error deregistering driver %s", d) + ":" + ex);
}
}
try {
AbandonedConnectionCleanupThread.shutdown();
} catch (InterruptedException e) {
System.out.println("ContextFinalizer:SEVERE problem cleaning up: " + e.getMessage());
e.printStackTrace();
}
}
}
<!--ServletContext监听器-->
<listener>
<listener-class>com.modle.listener.MyServletContextListener</listener-class>
</listener>
如图下所示:
解决Eclipse报错:‘org.eclipse.jst.jee.server:ProName’ did not find a matching property
eclipse中java web.xml报错cvc-complex-type.2.3: Element ‘web-app’ cannot have character [children], be…
web.xml文件添加servlet访问限制后出现如下错误:
cvc-complex-type.2.3: Element ‘web-app’ cannot have character [children], because the type’s content type is element- only.
翻译:
cvc-complex-type.2.3:元素’web-app’不能包含character [children],因为该类型的内容类型是仅包含元素的。
解决办法一:
出错原因为xml头文件中第三行:
xmlns=“http://java.sun.com/xml/ns/javaee”
把javaee改成: j2ee后解决问题。
解决办法二:
由于是粘贴过来的编码可能有问题,将xml中的文本重新手打输入一遍,一般就会解决这种问题。