gpt4 book ai didi

hibernate - 如何配置 Hibernate 以使用 DBCP2 连接池?

转载 作者:行者123 更新时间:2023-11-28 23:23:14 24 4
gpt4 key购买 nike

我正在使用 Tomcat 和 Hibernate 开发 Web 应用程序。上传并启动应用程序后,我的服务器日志返回此消息:

ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper- The last packet successfully received from the server was 193,600 milliseconds ago. The last packet sent successfully to the server was 193,601 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

关于这个问题的许多类似问题都说我需要使用像 c3p0 或 DBCP 这样的连接池来解决它。我决定使用 DBCP2 并根据 Internet 示例中的许多建议在 Maven 中添加 commons-dbcp2 和 commons-pool2 作为依赖项。不知为何,官方DBCP documentation目前没有提供示例。

还在我的 hibernate.cfg.xml 中添加了以下几行:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://server.org:3306/database?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.current_session_context_class">thread</property>

<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<property name="hibernate.dbcp.initialSize">5</property>
<property name="hibernate.dbcp.maxTotal">20</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dbcp.minIdle">5</property>
<property name="hibernate.dbcp.maxWaitMillis">-1</property>

<!-- Mapping -->

</session-factory>
</hibernate-configuration>

此外,重新启动后,我的服务器日志中仍会收到此消息:

WARN org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl- HHH000402: Using Hibernate built-in connection pool [not for production use!]

肯定是我做错了什么。但是因为 DBCP 文档没有提供示例。我找不到必须做些什么来纠正它。

如果有人能帮助我解决这个问题,我将不胜感激。

最佳答案

您使用的是什么版本的 commons-dbcp2commons-pool2?您不必在 pom 中指定 commons-pool2,因为它将作为 commons-dbcp2 的一部分包含在内。

使用 commons-dbcp2 中包含的不同版本的 commons-pool2 声明 commons-pool2 可能会遇到依赖性问题。

关于hibernate - 如何配置 Hibernate 以使用 DBCP2 连接池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40349651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com