glassfish(3.1.2) で、リモート MySql データベースへの JDBC 接続プール (および JDBC リソース) を作成しました。GlassFish で ping テストを実行すると、成功します。
私の Java EE 6 アプリは、その永続化ユニット (persistence.xml) を使用しています。
<persistence-unit name="remotePU" transaction-type="JTA">
<jta-data-source>jdbc/remotePU</jta-data-source>
<properties>
</properties>
</persistence-unit>
dao では、永続化コンテキストを注入します。
@PersistenceContext(unitName = "remotePU")
private EntityManager em;
次に、このエンティティ マネージャーを使用してクエリを実行します。
この同じ設定をローカル データベースでテストしたところ、問題なく動作したので、エラーがコードに含まれていないことがわかりました。
クエリ時に取得する例外スタックは次のとおりです。
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
....
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
....
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
....
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
リモート接続が遅いだけですか? これは、リモート データベースのサーバーへの ping です。
Pinging xxxxx.net [xx.xx.xx.xx] with 32 bytes of data:
Reply from xx.xx.xx.xx: bytes=32 time=29ms TTL=54
Reply from xx.xx.xx.xx: bytes=32 time=28ms TTL=54
Reply from xx.xx.xx.xx: bytes=32 time=28ms TTL=54
Reply from xx.xx.xx.xx: bytes=32 time=28ms TTL=54
Ping statistics for xx.xx.xx.xx:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 28ms, Maximum = 29ms, Average = 28ms
接続を改善するために、Glassfish の JDBC 接続プールの設定を調整できますか?