gpt4 book ai didi

java - 在 Web 应用程序中管理 EntityManagerFactory

转载 作者:行者123 更新时间:2023-11-30 09:45:40 24 4
gpt4 key购买 nike

我用 GWT 开发了一个网络应用程序。我只创建了一个 EntityManagerFactory(单例),但我不知道什么时候必须关闭它。我已按照此网站的说明进行操作:http://javanotepad.blogspot.com/2007/05/jpa-entitymanagerfactory-in-web.html , 但在 8 小时后没有进入我的应用程序我有错误:

    78616509 [http-9080-Processor4] ERROR org.hibernate.transaction.JDBCTransaction - JDBC begin failed
com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was 44,115,64
4 milliseconds ago. The last packet sent successfully to the server was 44,115,644 milliseconds ago. is lo
nger 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 tim
eouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1112)

尝试 2 或 3 次后一切正常。如果我在每次交易后关闭 EntityManagerFactory 我没有问题,但我不想那样做。我想知道如何管理 EntityManagerFactory 周期。

提前致谢。

最佳答案

错误信息自己说

The last packet successfully received from the server was 44,115,64 4 milliseconds ago. The last packet sent successfully to the server was 44,115,644 milliseconds ago. is lo nger 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 tim eouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

推荐的方法是使用c3p0 connection pool管理过期的连接。

Hibernate Documentation
Hibernate's own connection pooling algorithm is, however, quite rudimentary. It is intended to help you get started and is not intended for use in a production system, or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0.

在 MYSQL 引用文档中,不建议使用 autoReconnect 属性。

Connector/J autoReconnect
Should the driver try to re-establish stale and/or dead connections? If enabled the driver will throw an exception for a queries issued on a stale or dead connection, which belong to the current transaction, but will attempt reconnect before the next query issued on the connection in a new transaction. The use of this feature is not recommended, because it has side effects related to session state and data consistency when applications don't handle SQLExceptions properly, and is only designed to be used when you are unable to configure your application to handle SQLExceptions resulting from dead and stale connections properly. Alternatively, investigate setting the MySQL server variable "wait_timeout" to some high value rather than the default of 8 hours.

关于java - 在 Web 应用程序中管理 EntityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7486049/

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