无法在tomcat上连接到MySQL,但它使用hibernate作为独立的Java类进行连接

时间:2018-12-12 11:05:23

标签: java mysql hibernate tomcat web-applications

我一直在尝试从Windows系统上基于Web的应用程序使用休眠模式连接到我的MySQL数据库。 我尝试禁用Windows防火墙,绕过端口3306上的防御程序,尝试将绑定地址127.0.0.1写入my.inf文件,尝试使用localhost和ip 127.0.0.1进行连接 MySQL服务器已启动并在系统上运行。 我能够通过SQL工作台和命令提示符(都使用MySQL -u root -p和MySQL -u root -p -h 127.0.0.1)连接到数据库

当我的程序作为独立应用程序运行时,它以相同的休眠配置和jdbc配置连接到数据库(只是我的Web应用程序中的测试类)。

在tomcat服务器上运行时,该程序将引发并异常

我将不胜感激。

例外情况如下

    Root Cause
com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    com.mysql.cj.NativeSession.connect(NativeSession.java:152)
    com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
    com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    java.sql.DriverManager.getConnection(Unknown Source)
    java.sql.DriverManager.getConnection(Unknown Source)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:130)
    org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
    org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:301)
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
    org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1387)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    com.sun.proxy.$Proxy19.beginTransaction(Unknown Source)
    com.octalian.oss.dao.impl.admin.user.UserDAOImpl.authorizeUser(UserDAOImpl.java:43)
    com.octalian.oss.service.impl.admin.user.UserServiceImpl.authorizeUser(UserServiceImpl.java:41)
    com.octalian.oss.controller.user.LoginController.authorizeUser(LoginController.java:85)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:892)
    ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1537)
    ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:96)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:88)
    ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1613)
    ognl.ASTMethod.getValueBody(ASTMethod.java:91)
    ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    ognl.SimpleNode.getValue(SimpleNode.java:258)
    ognl.Ognl.getValue(Ognl.java:467)
    ognl.Ognl.getValue(Ognl.java:431)
    com.opensymphony.xwork2.ognl.OgnlUtil$3.execute(OgnlUtil.java:351)
    com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecuteMethod(OgnlUtil.java:403)
    com.opensymphony.xwork2.ognl.OgnlUtil.callMethod(OgnlUtil.java:349)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:422)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:284)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:252)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:260)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:69)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:188)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:120)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:244)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:99)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:139)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:155)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:174)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:130)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:193)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.factory.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:556)
    org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:113)


Root Cause
java.net.ConnectException: Connection refused: connect
    java.net.DualStackPlainSocketImpl.connect0(Native Method)
    java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    java.net.PlainSocketImpl.connect(Unknown Source)
    java.net.SocksSocketImpl.connect(Unknown Source)
    java.net.Socket.connect(Unknown Source)
    com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    com.mysql.cj.NativeSession.connect(NativeSession.java:152)
    com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
    com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    java.sql.DriverManager.getConnection(Unknown Source)
    java.sql.DriverManager.getConnection(Unknown Source)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:130)
    org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
    org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:301)
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
    org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
    org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1387)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    com.sun.proxy.$Proxy19.beginTransaction(Unknown Source)
    com.octalian.oss.dao.impl.admin.user.UserDAOImpl.authorizeUser(UserDAOImpl.java:43)
    com.octalian.oss.service.impl.admin.user.UserServiceImpl.authorizeUser(UserServiceImpl.java:41)
    com.octalian.oss.controller.user.LoginController.authorizeUser(LoginController.java:85)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:892)
    ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1537)
    ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:96)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:88)
    ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1613)
    ognl.ASTMethod.getValueBody(ASTMethod.java:91)
    ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    ognl.SimpleNode.getValue(SimpleNode.java:258)
    ognl.Ognl.getValue(Ognl.java:467)
    ognl.Ognl.getValue(Ognl.java:431)
    com.opensymphony.xwork2.ognl.OgnlUtil$3.execute(OgnlUtil.java:351)
    com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecuteMethod(OgnlUtil.java:403)
    com.opensymphony.xwork2.ognl.OgnlUtil.callMethod(OgnlUtil.java:349)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:422)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:284)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:252)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:260)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:69)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:133)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:188)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:120)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:244)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:99)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:139)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:155)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:174)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:97)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:130)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:193)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:240)
    org.apache.struts2.factory.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:556)
    org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:113)

请同时注意图片

java main class

hibernate config

ping to 127.0.0.1

MySQL workbench

windows defender, I have tried 3306 as enabled and disabled

反病毒已关闭

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我很愚蠢地承认这一点,我的程序有一个静态块和一个试图连接到数据库的弹簧配置,并导致了问题。通过删除静态块并更新弹簧配置解决了该问题。

这是一个建筑错误。

很抱歉造成麻烦。

谢谢