我正在使用hibernate.cfg运行Spring MVC应用程序,如下所示:
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="connection.url">jdbc:mysql://10.2.10.123:3306/xxx</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">xxx</property>
<property name="generate_statistics">false</property>
<property name="connection.password">xxx</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">50</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">7200</property>
<property name="current_session_context_class">thread</property>
<property name="c3p0.numHelperThreads">5</property>
<property name="show_sql">false</property>
当我在本地环境中运行应用程序时,它运行没有任何问题。但是在服务器上它给出了异常:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通讯链接失败
从服务器成功收到的最后一个数据包是206,756毫秒。成功发送到服务器的最后一个数据包是3毫秒之前。
我还得到以下日志:
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3573)
... 90 more
DEBUG: 11:42:41.858 com.mchange.v2.resourcepool.BasicResourcePool.trace(BasicResourcePool.java:1644) - trace com.mchange.v2.resourcepool.BasicResourcePool@2b5cccf1 [managed: 7, unused: 6, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@5700187d)
DEBUG: 11:42:41.861 com.mchange.v2.resourcepool.BasicResourcePool.decrementPendingAcquires(BasicResourcePool.java:422) - decremented pending_acquires: 0
DEBUG: 11:42:41.858 com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:967) - Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@61b03322
DEBUG: 11:42:41.861 com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.finerLoggingTestPooledConnection(C3P0PooledConnectionPool.java:323) - Test of PooledConnection [com.mchange.v2.c3p0.impl.NewPooledConnection@c2beb14] on CHECKOUT has FAILED.
java.sql.SQLException: Connection is invalid
有人可以帮忙吗?
答案 0 :(得分:0)
我通过将MySQL服务器上的wait_timeout和interactive_timeout的值增加到3600秒(1小时)来解决这个问题,之前两者的设置为60秒。
答案 1 :(得分:0)
就我而言,当我尝试从同一主机连接到在Ubuntu主机VM的容器内运行的mysql-server时遇到此错误。
示例:
如果我的虚拟机名称为abc.company.com
,则以下jdbc URL 不起作用:
jdbc:mysql://abc.company.com:3306/dbname
在类似xyz.company.com
的其他计算机上,jdbc url可以正常工作,但在abc.company.com
上则不能。
在abc.company.com
机器上,以下jdbc URL 可以正常工作:
jdbc:mysql://localhost:3306/dbname
这导致我检查了/etc/hosts
文件。
在/etc/hosts
中添加以下行可解决此问题:
127.0.1.1 abc.company.com abc
这似乎是一个操作系统错误,需要我们在某些Ubuntu版本中添加它们。 参考:https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution
在尝试此操作之前,我尝试了所有其他解决方案,例如GRANT ALL..
,更改了mysql.cnf中的bind-address
行。在这种情况下,它们都没有帮助我。