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

标签 java hibernate gwt jdbc

我用 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/

相关文章:

java - 为什么此 GWT 对话框不将其两个按钮居中?

java - 在 GWT 中将 JavaScriptObject 转换为 Java 对象失败

java - 如何制作一个合并任何类型数组的通用方法?

mysql - hibernate、mysql、glassfish v3 和 JTA 数据源

Spring Hibernate - 无法为当前线程获取事务同步 session

java - 刷新时出现 NullPointerException [Java EE + Hibernate 4. 3.5]

gwt - 使用 RootLayoutPanel 和 DockPanel 时如何设置 5% 的边距

java - 如何使用java打印进程的Heap位置?

java - 在for循环之外打印数组java

java - 如何知道一个点是否在android中的多边形内部