【问题标题】:unregistering the JDBC driver?注销 JDBC 驱动程序?
【发布时间】:2017-06-17 18:15:16
【问题描述】:

在开发 Web 应用程序时,我在 eclipse 下/内部运行 tomcat。 Web 应用程序通过 hibernate 和 guice 在嵌入式模式下使用 hsqldb。事情似乎工作正常,除了当我停止 tomcat 时。 Eclipse 有一个用于 Tomcat 的绿色启动按钮和一个红色停止按钮。当我单击停止按钮时,它并没有像我在混合中添加 hibernate 和 hsqldb 之前那样立即停止它。现在它等待几秒钟,然后 eclipse 给我一个关于无法停止 tomcat 的对话框并单击 OK 强制它终止。

有人知道我需要做什么来解决这个问题吗?我发现其他一些回复说要将 hsqldb jar 文件放在 tomcat 的 lib 目录中,但我想知道有什么我可以做的不那么激烈的事情。

这是来自 tomcat 的错误输出中的内容(在 Eclipse 控制台窗口中):

Jan 31, 2017 7:04:11 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
WARNING: The web application [basic] registered the JDBC driver [org.hsqldb.jdbc.JDBCDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Jan 31, 2017 7:04:11 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [basic] appears to have started a thread named [HSQLDB Timer @276f0355] 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.hsqldb.lib.HsqlTimer$TaskQueue.park(Unknown Source)
 org.hsqldb.lib.HsqlTimer.nextTask(Unknown Source)
 org.hsqldb.lib.HsqlTimer$TaskRunner.run(Unknown Source)
 java.lang.Thread.run(Thread.java:745)
Jan 31, 2017 7:04:11 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [basic] appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:745)
Jan 31, 2017 7:04:11 PM org.apache.catalina.loader.WebappClassLoaderBase checkThreadLocalMapForLeaks
SEVERE: The web application [basic] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@1d4fc7e8]) and a value of type [org.hibernate.internal.SessionImpl] (value [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

【问题讨论】:

  • 你需要在停止Tomcat时关闭数据库。
  • lumpynose 是正确的,你有两个解决方案: - 运行“SHUTDOWN;”作为应用程序末尾某处的 SQL 命令或在连接字符串中使用 shutdown = true ,如下所示: _Connection = DriverManager.getConnection("jdbc:hsqldb:file:mydb;shutdown=true", "SA", "") ;

标签: eclipse hibernate tomcat guice hsqldb


【解决方案1】:

我有一个愚蠢的答案,但我会告诉你我做了什么。

@JulienR:我的 persistence.xml 文件中已经为 javax.persistence.jdbc.url 值设置了 shutdown=true。

我创建了一个 ServletContextListener 并将其添加到我的 web.xml 文件中;这是代码。使用 EntityManager 发出 SHUTDOWN 命令的第一部分是我的代码。我从here 得到的 ClassLoader 和驱动程序的代码。所以有了这个,它不再抱怨 HSQLDB,但我仍然收到关于两个没有停止的线程的警告(我必须等待 eclipse 超时等待它)。我已经从日志中附加了相关行。

public class BasicServletContextListener implements ServletContextListener {
    private final transient Logger log =
        LoggerFactory.getLogger(BasicServletContextListener.class);

    // private final Provider<EntityManager> entityManagerProvider;

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        this.log.debug("contextEvent: {}", event.toString());

        final EntityManagerFactory entityManagerFactory =
            Persistence.createEntityManagerFactory("ilmp");

        final EntityManager entityManager =
            entityManagerFactory.createEntityManager();

        entityManager.getTransaction().begin();

        final Query query =
            entityManager.createNativeQuery("SHUTDOWN COMPACT;");

        this.log.debug("query: {}", query.executeUpdate());

        entityManager.getTransaction().commit();

        entityManager.close();

        // Now deregister JDBC drivers in this context's ClassLoader:
        // Get the webapp's ClassLoader
        final ClassLoader cl = Thread.currentThread().getContextClassLoader();
        // Loop through all drivers
        final Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            final Driver driver = drivers.nextElement();
            if (driver.getClass().getClassLoader() == cl) {
                // This driver was registered by the webapp's
                // ClassLoader, so deregister it:
                try {
                    this.log.info("Deregistering JDBC driver {}", driver);
                    DriverManager.deregisterDriver(driver);
                }
                catch (final SQLException ex) {
                    this.log.error("Error deregistering JDBC driver {}", driver,
                                   ex);
                }
            }
            else {
                // driver was not registered by the webapp's
                // ClassLoader and may be in use elsewhere
                this.log.trace(
                               "Not deregistering JDBC driver {} as it does not belong to this webapp's ClassLoader",
                               driver);
            }
        }
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.log.debug("contextEvent: {}", event.toString());
    }
}

>

 INFO: 2017-Feb-01 19:05:36.023 [localhost-startStop-2] org.hibernate.hql.internal.QueryTranslatorFactoryInitiator.initiateService.47: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: SHUTDOWN COMPACT;
 INFO: 2017-Feb-01 19:05:36.226 [localhost-startStop-2] sun.reflect.NativeMethodAccessorImpl.invoke0.-2: Database closed
 INFO: 2017-Feb-01 19:05:36.273 [localhost-startStop-2] sun.reflect.NativeMethodAccessorImpl.invoke0.-2: open start - state not modified
 INFO: 2017-Feb-01 19:05:36.351 [localhost-startStop-2] sun.reflect.NativeMethodAccessorImpl.invoke0.-2: Database closed
DEBUG: 2017-Feb-01 19:05:36.460 [localhost-startStop-2] com.objecteffects.basic.persist.BasicServletContextListener.contextDestroyed.42: query: 0
 INFO: 2017-Feb-01 19:05:36.460 [localhost-startStop-2] com.objecteffects.basic.persist.BasicServletContextListener.contextDestroyed.59: Deregistering JDBC driver org.hsqldb.jdbc.JDBCDriver@3cd32e8d
Feb 01, 2017 7:05:36 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [basic] appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:745)
Feb 01, 2017 7:05:36 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [basic] appears to have started a thread named [pool-2-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 java.lang.Thread.run(Thread.java:745)
Feb 01, 2017 7:05:36 PM org.apache.catalina.loader.WebappClassLoaderBase checkThreadLocalMapForLeaks
SEVERE: The web application [basic] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@1bc6eba0]) and a value of type [org.hibernate.internal.SessionImpl] (value [SessionImpl(PersistenceContext[entityKeys=[EntityKey[com.objecteffects.basic.persist.TumblrSecretsEntity#1], EntityKey[com.objecteffects.basic.persist.TumblrSecretsEntity#2]],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Feb 01, 2017 7:05:36 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-nio-8080"]
Feb 01, 2017 7:05:36 PM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["ajp-nio-8009"]
Feb 01, 2017 7:05:36 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-nio-8080"]
Feb 01, 2017 7:05:36 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-nio-8009"]

愚蠢的答案是,我查看了我几年前写的东西的设置(我退休了,只是四处闲逛试图为自己写东西)并将我以前使用的 c3p0 配置行添加到这种持久性中.xml 可以立即关闭它,尽管我仍然收到一堆关于僵尸线程的警告。以下是相关行(仍被注释掉)。

<!--             <property -->
<!--                 name="hibernate.c3p0.min_size" -->
<!--                 value="5" /> -->

<!--             <property -->
<!--                 name="hibernate.c3p0.max_size" -->
<!--                 value="20" /> -->

<!--             <property -->
<!--                 name="hibernate.c3p0.timeout" -->
<!--                 value="1800" /> -->

<!--             <property -->
<!--                 name="hibernate.c3p0.max_statements" -->
<!--                 value="50" /> -->

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-12
    • 2015-01-11
    • 1970-01-01
    • 2017-01-16
    • 2013-10-25
    • 1970-01-01
    • 2014-06-26
    • 2020-05-22
    相关资源
    最近更新 更多