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

我在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-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>

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();

}

}

}

回答:

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

如果是关闭场景或开发重新部署,则可以安全地忽略这些消息,仅当您需要在生产中重新部署时,这些消息才变得很重要,这种情况很少见。即使在生产中,大多数时候我们也想停止服务器进程并完全重启它。这是每个消息的一些详细信息,其含义是:

警告:Web应用程序[]注册了JDBC驱动程序[com.mysql.jdbc.Driver],但在Web应用程序停止时未能注销它。为了防止内存泄漏,已强制注销JDBC驱动程序。

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

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

要解决此问题,请从应用程序中删除驱动程序,然后在服务器级别上注册它。如果多个应用程序具有相同的驱动程序,这也会导致内存泄漏-

严重:Web应用程序[]创建了一个ThreadLocal,其键类型为[java.lang.ThreadLocal](值[java.lang.ThreadLocal@1087985b]),并且值类型为[org.hibernate.internal.SessionImpl],但未能成功在Web应用程序停止时将其删除。线程将随着时间的流逝而更新,以尝试避免可能的内存泄漏。

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

但是,在重新启动应用程序时,线程没有清理会话,因此在重新使用线程后,重新部署后,该线程中存储的该变量可以看到。

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

这意味着由于泄漏到先前部署的对象的“链接”,将不会大量收集对象树。结果是ClassLoader内存泄漏。

该消息指出,这种情况可能是由于未清理的ThreadLocals而发生的,并且将采取一些预防措施(开始杀死线程并创建新线程,而不是创建池,以消除泄漏的线程局部变量)。

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

以上是 为什么会收到JDBC驱动程序警告和ThreadLocal错误? 的全部内容, 来源链接: utcz.com/qa/407764.html

回到顶部