加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么我收到JDBC驱动程序警告和ThreadLocal错误?

发布时间:2020-12-15 04:42:25 所属栏目:Java 来源:网络整理
导读:我在GlassFish上运行我的应用程序,我使用 Spring Security和Hibernate. 当我运行应用程序时,GlassFish控制台上将显示以下警告和错误.我该如何避免它们? WARNING: The web application [] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to
我在GlassFish上运行我的应用程序,我使用 Spring Security和Hibernate.
当我运行应用程序时,GlassFish控制台上将显示以下警告和错误.我该如何避免它们?

WARNING:   The web application [] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak,the JDBC Driver has been forcibly unregistered.
SEVERE:   The web application [] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@1087985b]) and a value of type [org.hibernate.internal.SessionImpl] (value [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]) 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.
SEVERE:   The web application [] created a ThreadLocal with key of type [net.sf.json.AbstractJSON$1] (value [net.sf.json.AbstractJSON$1@362386d7]) and a value of type [java.util.HashSet] (value [[]]) 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.
SEVERE:   The web application [] created a ThreadLocal with key of type [net.sf.json.AbstractJSON$1] (value [net.sf.json.AbstractJSON$1@362386d7]) and a value of type [java.util.HashSet] (value [[]]) 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.

的hibernate.cfg.xml

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/myproject
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">12</property>

        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>



        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

<!--         Disable the second-level cache -->

<!-- <property name="cache.provider_class">
            org.hibernate.cache.EhCacheProvider
        </property>

        <property name="hibernate.cache.use_query_cache">true</property>-->


        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

HibernateUtil.java

public class HibernateUtil {

   private static ServiceRegistry serviceRegistry;
   private static final ThreadLocal<Session> threadLocal = new ThreadLocal();
   private static SessionFactory sessionFactory;

   private static SessionFactory configureSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

            return sessionFactory;
        } catch (HibernateException e) {
            System.out.append("** Exception in SessionFactory **");
            e.printStackTrace();
        }
       return sessionFactory;
  }     

  static {
    try {
      sessionFactory = configureSessionFactory();
    } catch (Exception e) {
      System.err.println("%%%% Error Creating SessionFactory %%%%");
      e.printStackTrace();
    }
  }

  private HibernateUtil() {
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }

  public static Session getSession() throws HibernateException {
    Session session = threadLocal.get();

    if (session == null || !session.isOpen()) {
      if (sessionFactory == null) {
        rebuildSessionFactory();
      }
      session = (sessionFactory != null) ? sessionFactory.openSession() : null;
      threadLocal.set(session);
    }

    return session;
  }

  public static void rebuildSessionFactory() {
    try {
      sessionFactory = configureSessionFactory();
    } catch (Exception e) {
      System.err.println("%%%% Error Creating SessionFactory %%%%");
      e.printStackTrace();
    }
  }

  public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);

    if (session != null) {
      session.close();
    }
  }
}

解决方法

这些是在服务器保持运行时应用程序重新部署时可能发生的错误消息.

如果是关闭方案或开发重新部署,则可以安全地忽略这些消息,只有在生产中需要重新部署时,这些消息才变得很重要,这种情况很少见.大多数时候即使在生产中我们也想停止服务器进程并完全重启它.这是每条消息的一些细节,意思是:

消息1 – 应用程序停止时未取消注册驱动程序:

WARNING: The web application [] registered the JDBC driver
[com.mysql.jdbc.Driver] but failed to unregister it when the web
application was stopped. To prevent a memory leak,the JDBC Driver has
been forcibly unregistered.

JDBC驱动程序在启动时在JVM级别的单例中注册,这意味着服务器通过在服务器级别的文件夹中发布驱动程序jar来完成.

在这种情况下,应用程序似乎携带驱动程序本身,这不是驱动程序的部署方式.

要解决此问题,请从应用程序中删除驱动程序,并将其注册到服务器级别.如果多个应用程序具有相同的驱动程序,这也会导致内存泄漏 – 请参阅此answer.

消息2 – 未清除ThreadLocal:

SEVERE: The web application [] created a ThreadLocal with key of
type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@1087985b])
and a value of type [org.hibernate.internal.SessionImpl] 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.

这意味着一个应用程序spring线程在线程中存储了一个Hibernate会话(每个线程作为数据存储,可以通过ThreadLocal附加事物).

但是当应用程序重新启动时,线程没有清理会话,因此当线程被重用时,在重新部署之后可以看到存储在线程中的这个变量.

这可能是令人惊讶的,但最糟糕的是会话指向其他对象,这些对象本身指向类,在重新部署之前指向旧的类加载器.

这意味着对象树的大部分将不会被垃圾收集,因为此泄漏的“链接”指向前一部署的对象.结果是ClassLoader Memory Leak.

消息说这个场景可能是由于未清理的ThreadLocals而发生的,并且将采取一些预防措施(开始杀死线程并创建新的线程而不是池化,以摆脱泄漏的线程本地).

总之,如果您不需要在生产中重新部署并始终重新启动服务器,则可以安全地忽略这些消息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读