将Tomcat容器连接到mysql容器:JDBCConnectionException:无法获取JDBC连接

时间:2017-02-20 10:20:45

标签: mysql hibernate tomcat docker

我正在尝试使用Docker将使用Hibernate,XAMPP for Apache tomcat和mysql服务器在Windows上创建的web-app移植到Linux。我在tomcat容器上的webapps文件夹下部署了我的war文件,链接到mysql容器......事实是我的web应用程序非常慢:即,第一个功能是登录/注册表单:在Linux上,有延迟大约几分钟!

我使用以下命令创建了容器:

docker run -d --name mysql-phpmyadmin -p 127.0.0.1:8686:80 -p 127.0.0.1:3306:3306 grzesiekb/mysql-phpmyadmin

docker run -d -p 8484:8080 -p 8007:8009 --name tomcat8-linked --link mysql-phpmyadmin:mysqlphp tomcat:8.0-jre8

这是我的hibernate.cfg.xml文件:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
        <session-factory>

<!-- queste configurazioni sono per il testing in locale  -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://mysqlphp:3306/at_db</property>

<property name="hibernate.connection.username">root</property>
<property name="connection.password">mysql_pass</property>


<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<property name="show_sql">false</property>
<!--<property name="hibernate.hbm2ddl.auto">create-drop</property>  se al posto di update metto create, mi cancella il db e lo rifa -->
<property name="hbm2ddl.auto">update</property>
                <!-- Entity -->

<mapping class="com.ATBoscoCastellano.Entity.ClientUser" />
<mapping class="com.ATBoscoCastellano.Entity.GenericUser" />
<mapping class="com.ATBoscoCastellano.SessionManagement.SessionToken" />
<mapping class="com.ATBoscoCastellano.Entity.Post" />
<!-- <mapping class="it.boscus.StarFoods.entity.ExampleEmbeddableIDClass2" /> -->


        </session-factory>
</hibernate-configuration>

这就是我从tomcat日志中看到的:

20-Feb-2017 07:48:15.869 SEVERE [http-apr-8080-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.ATBoscoCastellano.servlets.UserSessionServlet] in context with path [/AtApplicazione1] threw exception
 org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)


....


Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 232,149,705 milliseconds ago.  The last packet sent successfully to the server was 232,149,705 milliseconds ago. is longer 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 timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this probl
em.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3644)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2452)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
    at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4873)
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.poll(PooledConnections.java:76)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:187)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:386)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:84)
    ... 34 more
Caused by: java.net.SocketException: Broken pipe (Write failed)
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)

我尝试在hibernate中添加autoReconnect=true在Url上,但仍然无法正常工作。在我的mysql my.cnf中没有wait_timeout行。

我必须告诉web-app有效,但是我的servlet(通过hibernate与mysql容器交互)之后我会参加很多时间(几十分钟)。这个管理登录/注册表单的servlet从db检查最终是否存在插入的凭证。

我有基于tomcat8.0-jre8官方泊坞窗图像的tomcat容器;

来自mysql -V

root@1c53f40b63d3:/var/log/mysql# mysql -V
mysql  Ver 14.14 Distrib 5.5.42, for debian-linux-gnu (x86_64) using readline 6.3

我真的不知道为什么在Windows上它运行得很好,但这里没有。我的servlet仍然处于“待处理”状态......我真的想知道为什么会花时间和如何解决它。

0 个答案:

没有答案