通过hibernate

时间:2017-02-16 10:05:07

标签: mysql hibernate tomcat jdbc docker

我试图将我的网络应用程序从windows(使用xampp for apache + mysql)移植到ubuntu(使用docker容器,所以我有两个容器:一个用tomcat,另一个用mysql ...他们&# 39;重新联系)。

与此question相关,我无法访问我的sql容器。它来自this image

Tomcat容器是从官方tomcat创建的:8.0图像。

这两个容器通过创建的桥接器网络链接。

它是一个提供登录+注册服务的网络应用程序,用于检查用户在数据库中的存在。

我必须告诉他们在Windows上一切正常。

这是我的hibernate.cfg文件:

<!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>

我插入凭证后,点击&#34;登录&#34 ;;然后有一个名为&#34; UserSessionServlet&#34;的servlet。检查数据库中是否存在具有该凭证的用户;单击登录时出现错误,因为UserSessionServlet返回http 500错误。

[编辑]在tomcat容器上,我看到日志告诉我:

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)

但所有这些类都存在于opt / tomcat / webapps / AtApplicazione1 / WEB-INF / classes / com / AtBoscoCastellano / some / paths下。

我必须告诉我,我没有从官方图像(tomcat:8.0)修改我的tomcat容器,因此我无法知道它是否配置为休眠状态。

我的战争结构如下:三个文件夹(META-INF,WEB-INF,模板)和一个文件(.DS_Store)。因为我的例外是:java.lang.NoClassDefFoundError: Could not initialize class com.ATBoscoCastellano.Util.HibernateUtil.HibernateUtil,在战争中,这个类在WEB-INF / classes / com / ATBoscoCastellano / Util / HibernateUtil / ...下,其中有HibernateUtil .class和.java。

当我在tomcat容器上使用java / javac -version时,其输出为:

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"

[重新编辑]:在我的第一次请求中,catalina日志说java.net.ConnectException: Connection refused;然后,从第二个请求开始,它总是向我说同样的错误。

[编辑]现在我正在使用一个名为mysql-phpmyadmin来自this图片的新mysql容器。我已将tomcat容器链接到此(新)容器;如果我在hibernate.cfg.xml中指向localhost:PORT_MYSQL/AT_DB - &gt;连接在tomcat日志中被拒绝。如果我指向mysql-phpmyadmin:PORT/AT_DB,tomcat日志会说:

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)

我必须告诉你,在mysql容器中的配置文件中,有bind_address 0.0.0.0

1 个答案:

答案 0 :(得分:0)

我想我知道你为什么会遇到这个例外。你正在运行2个容器,

  1. 数据库的mysql容器
  2. Tomcat容器
  3. 当然,您的异常是由于静态变量初始化异常,

    public static final SessionFactory sessionFactory = buildSessionFactory();
    

    此外,您的属性文件几乎没有问题,

    1. connection.url应为hibernate.connection.url
    2. 您已将mysql主机名称为localhost,但它在另一个容器中运行。你应该把它改成&#34; mysql&#34;如果你已经将你的mysql容器命名为&#34; mysql&#34;
    3. 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)
      

      清楚地显示你的任何一个

      1. Mysql没有运行。通过从sql客户端如squirrel建立连接来检查。
      2. Tomcat容器无法连接到mysql容器。执行

        检查快速ping
        docker exec tomcat ping mysql  
        
      3. 我花了一些时间来部署这个应用程序。我已经解决了你在这个问题中提出的问题。主要问题是无法从tomcat容器访问mysql。此外,指定的端口也不正确。以下是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>
        

        几乎没有问题如下,

        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
        

        但这些是由于unix的区分大小写,在Windows的情况下可能不存在。其余的例外是微不足道的。我运行了以下docker命令,

         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
        

        和其他网络命令。