2011-09-20 93 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.

相關問題