无法连接到Azure MySql数据库(java.net.SocketException:连接重置)

时间:2018-08-09 06:16:29

标签: hibernate azure-sql-database

我正在尝试从Java(Maven)应用程序连接到Azure mysql云数据库。该连接最初可以正常工作,但是几分钟后我尝试重新连接时,它将失败。我正在使用spring5和hibernate5。

这是我的Hibernate属性方法:

private Properties getHibernateProperties() {
    Properties props = new Properties();
    try {
        // Setting JDBC properties
          String jdbcUrl = "jdbc:mysql://" + getDbServer() + ":3306/" + 
                    GeneratePlainPassword.decrypt(getDbNameKey(), getDbName()) + "?autoReconnect=true&useSSL=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&"
                            + "useLegacyDatetimeCode=false&serverTimezone=UTC&failOverReadOnly=false&maxReconnects=10&tcpKeepAlive=true";
          props.put(org.hibernate.cfg.Environment.DRIVER, "com.mysql.jdbc.Driver");
          props.put(org.hibernate.cfg.Environment.URL, jdbcUrl);
          props.put(org.hibernate.cfg.Environment.USER, GeneratePlainPassword.decrypt(getDbUserKey(), getDbUser()));
          props.put(org.hibernate.cfg.Environment.PASS, GeneratePlainPassword.decrypt(getDbPassKey(), getDbPass()));

          // Setting Hibernate properties
          props.put(org.hibernate.cfg.Environment.SHOW_SQL, getPrintHql());
          props.put(org.hibernate.cfg.Environment.DIALECT, "org.hibernate.dialect.MySQL57Dialect");
          props.put(org.hibernate.cfg.Environment.CONNECTION_PROVIDER, "org.hibernate.c3p0.internal.C3P0ConnectionProvider");
          //props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));

          // Setting C3P0 properties
          props.put(org.hibernate.cfg.Environment.C3P0_MIN_SIZE, "5");
          props.put(org.hibernate.cfg.Environment.C3P0_MAX_SIZE, "20");
          props.put(org.hibernate.cfg.Environment.C3P0_ACQUIRE_INCREMENT, "1");
          props.put(org.hibernate.cfg.Environment.C3P0_TIMEOUT, "1800000");  //30 minutes
          props.put(org.hibernate.cfg.Environment.C3P0_MAX_STATEMENTS, "150");

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return props;
}

这是我在闲置几分钟(4-5)之后尝试做某事时得到的错误日志:

11:28:37,184 ERROR [stderr] (default task-6) org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
11:28:37,185 ERROR [stderr] (default task-6)    at org.springframework.orm.hibernate5.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:564)
11:28:37,186 ERROR [stderr] (default task-6)    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374)
11:28:37,186 ERROR [stderr] (default task-6)    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:474)
11:28:37,186 ERROR [stderr] (default task-6)    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:289)
11:28:37,187 ERROR [stderr] (default task-6)    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
11:28:37,187 ERROR [stderr] (default task-6)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
11:28:37,187 ERROR [stderr] (default task-6)    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
11:28:37,187 ERROR [stderr] (default task-6)    at com.sun.proxy.$Proxy43.list(Unknown Source)
11:28:37,187 ERROR [stderr] (default task-6)    at com.example.csc.web.view.LoginViews.getUserListPageView(LoginViews.java:39)
11:28:37,188 ERROR [stderr] (default task-6)    at com.example.csc.web.controller.UserController.userListValidSession(UserController.java:67)
11:28:37,188 ERROR [stderr] (default task-6)    at com.example.csc.web.controller.UserController$$FastClassBySpringCGLIB$$d70ea989.invoke(<generated>)
11:28:37,188 ERROR [stderr] (default task-6)    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
11:28:37,188 ERROR [stderr] (default task-6)    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
11:28:37,188 ERROR [stderr] (default task-6)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
11:28:37,188 ERROR [stderr] (default task-6)    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
11:28:37,189 ERROR [stderr] (default task-6)    at com.example.csc.web.aspect.UserSessionTrackerAspect.validateUserSession(UserSessionTrackerAspect.java:41)
11:28:37,189 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
11:28:37,189 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
11:28:37,189 ERROR [stderr] (default task-6)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:28:37,190 ERROR [stderr] (default task-6)    at java.lang.reflect.Method.invoke(Method.java:498)
11:28:37,190 ERROR [stderr] (default task-6)    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:643)
11:28:37,190 ERROR [stderr] (default task-6)    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:632)
11:28:37,190 ERROR [stderr] (default task-6)    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
11:28:37,191 ERROR [stderr] (default task-6)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
11:28:37,191 ERROR [stderr] (default task-6)    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
11:28:37,191 ERROR [stderr] (default task-6)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
11:28:37,191 ERROR [stderr] (default task-6)    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
11:28:37,192 ERROR [stderr] (default task-6)    at com.example.csc.web.controller.UserController$$EnhancerBySpringCGLIB$$429233ff.userListValidSession(<generated>)
11:28:37,192 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
11:28:37,192 ERROR [stderr] (default task-6)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
11:28:37,192 ERROR [stderr] (default task-6)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:28:37,192 ERROR [stderr] (default task-6)    at java.lang.reflect.Method.invoke(Method.java:498)
11:28:37,193 ERROR [stderr] (default task-6)    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
11:28:37,193 ERROR [stderr] (default task-6)    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
11:28:37,193 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
11:28:37,194 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871)
11:28:37,194 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777)
11:28:37,194 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
11:28:37,195 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
11:28:37,195 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
11:28:37,195 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
11:28:37,196 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
11:28:37,196 ERROR [stderr] (default task-6)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
11:28:37,196 ERROR [stderr] (default task-6)    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
11:28:37,196 ERROR [stderr] (default task-6)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
11:28:37,197 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
11:28:37,197 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
11:28:37,197 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
11:28:37,197 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
11:28:37,197 ERROR [stderr] (default task-6)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
11:28:37,198 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
11:28:37,198 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
11:28:37,198 ERROR [stderr] (default task-6)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
11:28:37,198 ERROR [stderr] (default task-6)    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
11:28:37,199 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
11:28:37,199 ERROR [stderr] (default task-6)    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
11:28:37,199 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
11:28:37,199 ERROR [stderr] (default task-6)    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
11:28:37,200 ERROR [stderr] (default task-6)    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
11:28:37,200 ERROR [stderr] (default task-6)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
11:28:37,200 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
11:28:37,201 ERROR [stderr] (default task-6)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
11:28:37,201 ERROR [stderr] (default task-6)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
11:28:37,201 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
11:28:37,202 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
11:28:37,202 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
11:28:37,202 ERROR [stderr] (default task-6)    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
11:28:37,203 ERROR [stderr] (default task-6)    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
11:28:37,203 ERROR [stderr] (default task-6)    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
11:28:37,203 ERROR [stderr] (default task-6)    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
11:28:37,203 ERROR [stderr] (default task-6)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
11:28:37,203 ERROR [stderr] (default task-6)    at java.lang.Thread.run(Thread.java:748)
11:28:37,204 ERROR [stderr] (default task-6) Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
11:28:37,204 ERROR [stderr] (default task-6)    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
11:28:37,204 ERROR [stderr] (default task-6)    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
11:28:37,204 ERROR [stderr] (default task-6)    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
11:28:37,204 ERROR [stderr] (default task-6)    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
11:28:37,205 ERROR [stderr] (default task-6)    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:118)
11:28:37,205 ERROR [stderr] (default task-6)    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:145)
11:28:37,205 ERROR [stderr] (default task-6)    at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:521)
11:28:37,205 ERROR [stderr] (default task-6)    at org.springframework.orm.hibernate5.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:470)
11:28:37,205 ERROR [stderr] (default task-6)    ... 71 more
11:28:37,206 ERROR [stderr] (default task-6) Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 427,132 milliseconds ago.  The last packet sent successfully to the server was 427,423 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 problem.
11:28:37,206 ERROR [stderr] (default task-6)    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:590)
11:28:37,206 ERROR [stderr] (default task-6)    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:57)
11:28:37,206 ERROR [stderr] (default task-6)    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
11:28:37,207 ERROR [stderr] (default task-6)    at com.mysql.cj.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:3773)
11:28:37,207 ERROR [stderr] (default task-6)    at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:1059)
11:28:37,207 ERROR [stderr] (default task-6)    at org.hibernate.c3p0.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
11:28:37,208 ERROR [stderr] (default task-6)    at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
11:28:37,208 ERROR [stderr] (default task-6)    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:115)
11:28:37,208 ERROR [stderr] (default task-6)    ... 74 more
11:28:37,209 ERROR [stderr] (default task-6) Caused by: com.mysql.cj.core.exceptions.CJCommunicationsException: The last packet successfully received from the server was 427,132 milliseconds ago.  The last packet sent successfully to the server was 427,423 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 problem.
11:28:37,209 ERROR [stderr] (default task-6)    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
11:28:37,209 ERROR [stderr] (default task-6)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
11:28:37,210 ERROR [stderr] (default task-6)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
11:28:37,210 ERROR [stderr] (default task-6)    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11:28:37,210 ERROR [stderr] (default task-6)    at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
11:28:37,210 ERROR [stderr] (default task-6)    at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
11:28:37,211 ERROR [stderr] (default task-6)    at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
11:28:37,211 ERROR [stderr] (default task-6)    at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
11:28:37,211 ERROR [stderr] (default task-6)    at com.mysql.cj.mysqla.io.MysqlaProtocol.send(MysqlaProtocol.java:561)
11:28:37,211 ERROR [stderr] (default task-6)    at com.mysql.cj.mysqla.io.MysqlaProtocol.sendCommand(MysqlaProtocol.java:644)
11:28:37,212 ERROR [stderr] (default task-6)    at com.mysql.cj.mysqla.io.MysqlaProtocol.sqlQueryDirect(MysqlaProtocol.java:950)
11:28:37,212 ERROR [stderr] (default task-6)    at com.mysql.cj.mysqla.MysqlaSession.sqlQueryDirect(MysqlaSession.java:431)
11:28:37,212 ERROR [stderr] (default task-6)    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1974)
11:28:37,212 ERROR [stderr] (default task-6)    ... 79 more
11:28:37,213 ERROR [stderr] (default task-6) Caused by: java.net.SocketException: Connection reset
11:28:37,214 ERROR [stderr] (default task-6)    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:115)
11:28:37,214 ERROR [stderr] (default task-6)    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
11:28:37,214 ERROR [stderr] (default task-6)    at sun.security.ssl.OutputRecord.writeBuffer(OutputRecord.java:431)
11:28:37,215 ERROR [stderr] (default task-6)    at sun.security.ssl.OutputRecord.write(OutputRecord.java:417)
11:28:37,215 ERROR [stderr] (default task-6)    at sun.security.ssl.SSLSocketImpl.writeRecordInternal(SSLSocketImpl.java:886)
11:28:37,215 ERROR [stderr] (default task-6)    at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:857)
11:28:37,215 ERROR [stderr] (default task-6)    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
11:28:37,216 ERROR [stderr] (default task-6)    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
11:28:37,216 ERROR [stderr] (default task-6)    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
11:28:37,216 ERROR [stderr] (default task-6)    at com.mysql.cj.mysqla.io.SimplePacketSender.send(SimplePacketSender.java:50)
11:28:37,216 ERROR [stderr] (default task-6)    at com.mysql.cj.mysqla.io.TimeTrackingPacketSender.send(TimeTrackingPacketSender.java:43)
11:28:37,217 ERROR [stderr] (default task-6)    at com.mysql.cj.mysqla.io.MysqlaProtocol.send(MysqlaProtocol.java:552)
11:28:37,217 ERROR [stderr] (default task-6)    ... 83 more

感谢您的帮助!

0 个答案:

没有答案