Connect a web-app on tomcat container to mysql con

2019-03-06 11:24发布

i'm trying to port my web-app from windows (using xampp for apache+mysql) to ubuntu (using docker container, so i have two containers: one with tomcat and another one with mysql.. they're linked).

Related to this question, i can't reach my sql container. It's creted from this image.

Tomcat container is created from official tomcat:8.0 image.

The two container are linked via a bridge docker network created.

It's a web-app that provides login+register service with checking user's presence in db.

I have to tell that on windows all works well.

This is my hibernate.cfg file:

<!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://localhost:49162/AT_DB</property>

<property name="hibernate.connection.username">root</property>
<property name="connection.password">lxit</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>

After i insert my credential, i click on "login"; then there is a servlet named "UserSessionServlet" that checks if in the db is present an user with that credential; the error appairs when i click login, because UserSessionServlet return http 500 error.

[Edit] On tomcat container, i saw that logs tell me:

16-Feb-2017 09:43:41.498 SEVERE [http-nio-8080-exec-19] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.ATBoscoCastellano.servlets.UserSessionServlet] in context with path [/AtApplicazione1] threw exception [Servlet execution threw an 
exception] with root cause
 java.lang.NoClassDefFoundError: Could not initialize class com.ATBoscoCastellano.Util.HibernateUtil.HibernateUtil
    at com.ATBoscoCastellano.Entity.GenericUser.checkIfUserExistsByEmail(GenericUser.java:198)
    at com.ATBoscoCastellano.Entity.ClientUser.validateCredentials(ClientUser.java:259)
    at com.ATBoscoCastellano.servlets.UserSessionServlet.doPost(UserSessionServlet.java:78)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

But all these classes exist, under opt/tomcat/webapps/AtApplicazione1/WEB-INF/classes/com/AtBoscoCastellano/some/paths.

I have to tell that i've not modified my tomcat container from official image (tomcat:8.0), so i can't know if it's configured for hibernate or not.

My war is structured as follows: three folders (META-INF, WEB-INF, template) and a file (.DS_Store). Because of my exception is :java.lang.NoClassDefFoundError: Could not initialize class com.ATBoscoCastellano.Util.HibernateUtil.HibernateUtil , in the war, this class is under WEB-INF/classes/com/ATBoscoCastellano/Util/HibernateUtil/ ... in which there is HibernateUtil .class and .java.

When i cmd java/javac -version on my tomcat container, its output is:

root@a26ff8018f3f:/opt/tomcat# java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

root@a26ff8018f3f:/opt/tomcat# javac -version
javac "1.8.0_111"

[re-edit]: in my first requests, catalina logs says java.net.ConnectException: Connection refused; then, from the second request, it says to me always the same error.

[EDIT] now i'm using a new mysql container, named mysql-phpmyadmin from this image. I've linked tomcat container to this (new) container,; if i point, in the hibernate.cfg.xml, to localhost:PORT_MYSQL/AT_DB --> connection refused in tomcat log. If i point to mysql-phpmyadmin:PORT/AT_DB, tomcat logs says:

17-Feb-2017 08:31:25.649 SEVERE [http-apr-8080-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.ATBoscoCastellano.servlets.UserSessionServlet] in context with path [/AtApplicazione1] threw exception [Servlet execution threw an e
xception] with root cause
 java.sql.SQLException: null,  message from server: "Host '172.17.0.9' is not allowed to connect to this MySQL server"
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1038)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)

I have to tell that, in config file within mysql container, there is bind_address 0.0.0.0

1条回答
SAY GOODBYE
2楼-- · 2019-03-06 12:10

I think I have an idea why you are getting this exception. You are running 2 containers,

  1. mysql container for data base
  2. Tomcat container

Surely, your exception is due to static variable initialization exception,

public static final SessionFactory sessionFactory = buildSessionFactory();

Also, your property file has few problems,

  1. connection.url should be hibernate.connection.url
  2. You have mentioned your mysql hostname as localhost but it is running in another container. You should change it to "mysql" if you have named your mysql container as "mysql"

java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:214)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)

Clearly shows that your either

  1. Mysql is not runnning. Check by making a connection from sql client like squirrel.
  2. Tomcat container can't connect to mysql container. Check a quick ping by doing

    docker exec tomcat ping mysql  
    

Took me a while to deploy this application. I have solved the problem you have asked in this question. The primary problem was that mysql was not reachable from tomcat container. Also, the port specified was also not correct. Following is 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://mysql:3306/AT_DB</property>

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

 ....rest of configuration....      

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

There are few problems like following,

17-Feb-2017 16:07:53.156 INFO [http-nio-8080-exec-1] org.hibernate.dialect.Dialect.<init> HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
17-Feb-2017 16:07:53.776 INFO [http-nio-8080-exec-1] org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.processGetTableResults HHH000262: Table not found: ClientUser
17-Feb-2017 16:07:53.777 INFO [http-nio-8080-exec-1] org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.processGetTableResults HHH000262: Table not found: ClientUser
17-Feb-2017 16:07:53.818 INFO [http-nio-8080-exec-1] org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.processGetTableResults HHH000262: Table not found: Post
17-Feb-2017 16:07:53.819 INFO [http-nio-8080-exec-1] org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.processGetTableResults HHH000262: Table not found: Post
17-Feb-2017 16:07:53.828 INFO [http-nio-8080-exec-1] org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.processGetTableResults HHH000262: Table not found: SessionToken
17-Feb-2017 16:07:53.828 INFO [http-nio-8080-exec-1] org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.processGetTableResults HHH000262: Table not found: SessionToken
17-Feb-2017 16:07:53.830 WARN [http-nio-8080-exec-1] org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl.handleException GenerationTarget encountered exception accepting command : Unable to execut

But these are due to case sensitivity of unix which was probably not there in case of Windows. Rest of the exception are trivial to solve. I ran following docker commands,

 sudo docker run -ti --rm -p 8888:8080 -p 8009:8009 -v $(pwd):/usr/local/tomcat/webapps/ --name tomcat tomcat:9-jre8 bash

 sudo docker run -d --name mysql -p 49162:3306 -p 49161:80 -p 49160:22 lxitgto/mysql-phpmyadmin:v1

and rest of the network command.

查看更多
登录 后发表回答