在stackoverflow上发现了一些类似的问题但没有一个似乎是我的问题或者对我有用的答案。第一次使用它,所以我确定我错过了一些愚蠢的东西...
在Tomcat 8中使用Jersey和Hibernate运行Web应用程序。目前,我在Eclipse中这样做。
这是我的ServletContextListener:
package com.anwinity.projects.chrono.services;
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 org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ChronoDatabase implements ServletContextListener
{
private static SessionFactory sessionFactory;
@Override
public void contextInitialized(ServletContextEvent event)
{
System.out.println("INIT");
sessionFactory = buildSessionFactory();
}
@Override
public void contextDestroyed(ServletContextEvent event)
{
System.out.println("DESTROY");
SessionFactory sessionFactory = getSessionFactory();
if(sessionFactory == null)
{
System.out.println("Session Factory is null");
}
else
{
System.out.println("Closing Session Factory");
sessionFactory.close();
}
//IOUtils.closeQuietly(sessionFactory);
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Enumeration<Driver> drivers = DriverManager.getDrivers();
while(drivers.hasMoreElements())
{
Driver driver = drivers.nextElement();
if(driver.getClass().getClassLoader() == cl)
{
try
{
System.out.printf("Deregistering JDBC driver %s%n", driver);
DriverManager.deregisterDriver(driver);
}
catch(SQLException ex)
{
System.out.printf("Error deregistering JDBC driver %s%n", driver);
}
}
else
{
System.out.printf("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader%n", driver);
}
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
private static SessionFactory buildSessionFactory()
{
System.out.println("Building Session Factory");
try
{
return new Configuration().configure().buildSessionFactory();
}
catch(Throwable e)
{
throw new ExceptionInInitializerError(e);
}
finally
{
System.out.println("Session Factory Build Complete");
}
}
}
日志:
INFO: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
Oct 13, 2015 7:49:39 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-nio-8080"]
Oct 13, 2015 7:49:39 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-nio-8009"]
Oct 13, 2015 7:49:39 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
DESTROY
Closing Session Factory
Deregistering JDBC driver org.h2.Driver@63485bee
Oct 13, 2015 7:49:39 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [chrono] appears to have started a thread named [MVStore background writer nio:C:/Users/Andrew/chrono.mv.db] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.h2.mvstore.MVStore$BackgroundWriterThread.run(MVStore.java:2684)
Oct 13, 2015 7:49:39 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-nio-8080"]
Oct 13, 2015 7:49:39 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["ajp-nio-8009"]
Oct 13, 2015 7:49:39 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-nio-8080"]
Oct 13, 2015 7:49:39 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-nio-8009"]
Exception in thread "Thread-3" java.lang.IllegalStateException: Can't overwrite cause with java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [org.h2.mvstore.MVMap$2]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at java.lang.Throwable.initCause(Unknown Source)
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1324)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1203)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1164)
at org.h2.mvstore.MVMap.entrySet(MVMap.java:865)
at org.h2.store.LobStorageMap.removeAllForTable(LobStorageMap.java:320)
at org.h2.engine.Database.removeOrphanedLobs(Database.java:1319)
at org.h2.engine.Database.close(Database.java:1240)
at org.h2.engine.DatabaseCloser.run(DatabaseCloser.java:63)
Caused by: java.lang.ClassNotFoundException
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1323)
... 7 more