Hibernate4 + C3P0 +德比 - 内存泄漏在Tomcat停止或Web应用程序重装(Hi

2019-08-31 09:43发布

我有一个非常简单的应用程序模板,其中包括通过Hibernate嵌入式Apache Derby数据库。

我有以下配置:

    <property name="hibernate.dialect"> org.hibernate.dialect.DerbyTenSevenDialect</property>
    <property name="hibernate.connection.driver_class"> org.apache.derby.jdbc.EmbeddedDriver </property>
    <property name="hibernate.connection.username"></property>
    <property name="hibernate.connection.password"></property>
    <property name="current_session_context_class">thread</property>
    <property name="connection.pool_size">10</property>
    <property name="connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
    <property name="hibernate.c3p0.acquire_increment">1</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="cache.use_second_level_cache">false</property>
    <property name="cache.use_query_cache">false</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.format_sql">false</property>

和为SessionFactory一个init方法是:

public static void init(String hibernateConfigurationResource,String connectionUrl) {
    try {
        configuration = new Configuration();
        configuration.configure(hibernateConfigurationResource);
        if (connectionUrl == null || connectionUrl.length() == 0) {
            connectionUrl = "myDb";
        } else {
            connectionUrl += File.separator + "myDb";
        }
        File f = new File(connectionUrl);
        if (logger.isDebugEnabled()) {
            logger.debug("Using db path: " + f.getAbsolutePath());
        }
        if (!f.exists()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Database does not exist: creating");
            }
            connectionUrl += ";create=true";
        }
        configuration
                .setProperty("hibernate.connection.url", "jdbc:derby:"
                    + connectionUrl);

        serviceRegistry =
            new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }
}

并在破坏阶段,这种方法被称为:

public static void destroy() throws SQLException {
    String connectionUrl = configuration
            .getProperty("hibernate.connection.url").replace(";create=true", "");
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Shutting down database: " + connectionUrl);
        }
        DriverManager.getConnection(connectionUrl + ";shutdown=true");
    } catch (SQLNonTransientConnectionException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(e.getMessage());
        }
        String message = e.getMessage();
        if (!(message.startsWith("Database") && message
                .endsWith("shutdown."))) {
            throw e;
        }
    }
    try {
        Session s = sessionFactory.getCurrentSession();
        if (s.isDirty()) {
            s.flush();
        }
        s.disconnect();
        s.close();
        sessionFactory.close();
    } catch (Exception e) {
        logger.error(e);
    }

    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        try {
            logger.info("Deregistering jdbc driver: " + driver);
            DriverManager.deregisterDriver(driver);
        } catch (SQLException e) {
            logger.warn("Error deregistering driver " + driver, e);
        }

    }
}

我注意到,在应用程序运行正常,但只要我停止Tomcat,我看到以下消息:

INFO: Stopping service Catalina
apr 17, 2013 4:34:36 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myWebApp-webapp] appears to have started a thread named [Timer-1] but has failed to stop it. This is very likely to create a memory leak.
apr 17, 2013 4:34:36 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myWebApp-webapp] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very likely to create a memory leak.
apr 17, 2013 4:34:36 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myWebApp-webapp] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] but has failed to stop it. This is very likely to create a memory leak.
apr 17, 2013 4:34:36 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/myWebApp-webapp] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] but has failed to stop it. This is very likely to create a memory leak.
apr 17, 2013 4:34:36 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/myWebApp-webapp] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@106f4e54]) and a value of type [org.apache.derby.iapi.services.context.ContextManager] (value [org.apache.derby.iapi.services.context.ContextManager@587cc9a3]) 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. 
apr 17, 2013 4:34:36 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/myWebApp-webapp] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@106f4e54]) and a value of type [org.apache.derby.iapi.services.context.ContextManager] (value [org.apache.derby.iapi.services.context.ContextManager@2e704a6]) 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. 
apr 17, 2013 4:34:36 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/myWebApp-webapp] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@106f4e54]) and a value of type [org.apache.derby.iapi.services.context.ContextManager] (value [org.apache.derby.iapi.services.context.ContextManager@36bc4735]) 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. 
apr 17, 2013 4:34:36 PM org.apache.coyote.AbstractProtocolHandler stop
INFO: Stopping ProtocolHandler ["http-bio-8080"]
apr 17, 2013 4:34:36 PM org.apache.coyote.AbstractProtocolHandler stop
INFO: Stopping ProtocolHandler ["ajp-bio-8009"]

我敢肯定,这个问题是在使用Hibernate不能够正确地关闭C3P0连接提供了连接池的水平。 我也试图与MySQL和问题仍然存在。

我也知道对内存泄漏tomcat的保护(即实际生产的消息),我运行Tomcat 7.0.39。

而且我不希望移动的连接池的应用程序(委托容器)外面。

我怎么能告诉休眠关机一切正常,并避免这些消息? 我在想什么?

先感谢您!

编辑:

在关机日志只显示:

2013-04-18 10:27:13,537 DEBUG [Thread-13] HibernateUtil  - Shutting down database: jdbc:derby:myDb
2013-04-18 10:27:13,555 DEBUG [Thread-13] HibernateUtil  - Database 'myDb' shutdown.
2013-04-18 10:27:13,656 DEBUG [Thread-13] SessionImpl  - Disconnecting session
2013-04-18 10:27:13,656 DEBUG [Thread-13] LogicalConnectionImpl  - Releasing JDBC connection
2013-04-18 10:27:13,658 DEBUG [Thread-13] SessionFactoryImpl  - HHH000031: Closing
2013-04-18 10:27:13,659 INFO  [Thread-13] HibernateUtil  - Deregistering jdbc driver: sun.jdbc.odbc.JdbcOdbcDriver@35849932
2013-04-18 10:27:13,659 INFO  [Thread-13] HibernateUtil  - Deregistering jdbc driver: org.apache.derby.jdbc.AutoloadedDriver40@7d7ff794

在启动时我只有该类AbstractPoolBackedDataSource下面一行:

2013-04-18 10:25:38,808 INFO  [Thread-2] AbstractPoolBackedDataSource  - Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@28fe617e [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@8c36e25a [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 2s05jr8u7u65au1gs0zth|25e62c36, idleConnectionTestPeriod -> 3000, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 300, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@423e8f48 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 2s05jr8u7u65au1gs0zth|363bec9c, jdbcUrl -> jdbc:derby:myDb, properties -> {user=******, password=******} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 2s05jr8u7u65au1gs0zth|43ba9cea, numHelperThreads -> 3 ]

Answer 1:

做一些调查研究后,我发现,明确地关闭所有连接解决了这个问题:

C3P0Registry.getNumPooledDataSources();

@SuppressWarnings({ "unchecked", "rawtypes" })
Iterator<Set> it = C3P0Registry.getPooledDataSources().iterator();
while (it.hasNext()) {
    try {
           dataSource = (PooledDataSource) it.next();
           dataSource.close();
    } catch (Exception e) {
           logger.error(e);
    }
}


Answer 2:

接受的答案是没有的伎俩,我的更简洁版本

for (Object o : C3P0Registry.getPooledDataSources()) {
  try {
    ((PooledDataSource) o).close();
  } catch (Exception e) {
    // oh well, let tomcat do the complaing for us.
  }
}


文章来源: Hibernate4 + c3p0 + Derby - Memory Leak on Tomcat stop or web application reload