2011-09-20 102 views
2

我开发了一个带GWT的Web应用程序。我只创建一个EntityManagerFactory(单例),但我不知道什么时候必须关闭它。我按照这个网页的指示:http://javanotepad.blogspot.com/2007/05/jpa-entitymanagerfactory-in-web.html,且8个小时没有进入我的应用程序后,我有错误:在Web应用程序中管理EntityManagerFactory

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循环。

在此先感谢。

回答

2

错误消息由它的自我

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.

的建议的方法是说使用​​用于管理过期的连接。

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.

相关问题