Azure MSSQL连接间歇性丢失

时间:2018-04-07 08:07:45

标签: spring-boot azure-sql-database c3p0

我创建了一个spring boot微服务,并使用Azure MS-SQL作为数据库。 为了与数据库连接,我使用了spring-jpa以及以下配置

spring.datasource.url=jdbc:sqlserver://xxxxx:1433;databaseName=xxxx;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect

每当调用数据库时,我会间歇性地跟踪异常,如果我在某个时间再次重试,它会自动进行

com.microsoft.sqlserver.jdbc.SQLServerException:
   at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError (SQLServerException.java190)
   at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed (SQLServerConnection.java388)
   at com.microsoft.sqlserver.jdbc.SQLServerConnection.getWarnings (SQLServerConnection.java2115)
   at com.mchange.v2.c3p0.impl.NewProxyConnection.getWarnings (NewProxyConnection.java807)
   at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.handleAndClearWarnings (SqlExceptionHelper.java274)
   at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logAndClearWarnings (SqlExceptionHelper.java254)
   at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.releaseConnection (LogicalConnectionManagedImpl.java170)
   at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.close (LogicalConnectionManagedImpl.java215)
   at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close (JdbcCoordinatorImpl.java193)
   at org.hibernate.internal.SessionImpl.close (SessionImpl.java421)
   at org.hibernate.jpa.internal.EntityManagerImpl.close (EntityManagerImpl.java145)
   at sun.reflect.GeneratedMethodAccessor70.invoke
   at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java43)
   at java.lang.reflect.Method.invoke (Method.java498)
   at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke (ExtendedEntityManagerCreator.java347)
   at com.sun.proxy.$Proxy151.close
   at org.springframework.orm.jpa.EntityManagerFactoryUtils.closeEntityManager (EntityManagerFactoryUtils.java438)
   at org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor.afterCompletion (OpenEntityManagerInViewInterceptor.java111)
   at org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter.afterCompletion (WebRequestHandlerInterceptorAdapter.java72)
   at org.springframework.web.servlet.HandlerExecutionChain.triggerAfterCompletion (HandlerExecutionChain.java169)
   at org.springframework.web.servlet.DispatcherServlet.processDispatchResult (DispatcherServlet.java1059)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java984)
   at org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet.java901)
   at org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java970)
   at org.springframework.web.servlet.FrameworkServlet.doGet (FrameworkServlet.java861)
   at javax.servlet.http.HttpServlet.service (HttpServlet.java635)
   at org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java846)
   at javax.servlet.http.HttpServlet.service (HttpServlet.java742)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java231)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java52)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal (ApplicationContextHeaderFilter.java55)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal (ShallowEtagHeaderFilter.java110)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter.doFilter (WebRequestTrackingFilter.java86)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at com.siemens.isb.lib.security.oauth.filter.RequestBasedInitializerFilter.doFilter (RequestBasedInitializerFilter.java33)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at com.siemens.isb.lib.security.oauth.filter.ExceptionHandlerFilter.doFilter (ExceptionHandlerFilter.java28)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal (WebRequestTraceFilter.java110)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java317)
   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke (FilterSecurityInterceptor.java127)
   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter (FilterSecurityInterceptor.java91)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter (ExceptionTranslationFilter.java114)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.session.SessionManagementFilter.doFilter (SessionManagementFilter.java137)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter (AnonymousAuthenticationFilter.java111)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter (SecurityContextHolderAwareRequestFilter.java170)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter (RequestCacheAwareFilter.java63)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter (OAuth2AuthenticationProcessingFilter.java176)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter (LogoutFilter.java116)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal (HeaderWriterFilter.java64)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter (SecurityContextPersistenceFilter.java105)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal (WebAsyncManagerIntegrationFilter.java56)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter (FilterChainProxy.java331)
   at org.springframework.security.web.FilterChainProxy.doFilterInternal (FilterChainProxy.java214)
   at org.springframework.security.web.FilterChainProxy.doFilter (FilterChainProxy.java177)
   at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate (DelegatingFilterProxy.java347)
   at org.springframework.web.filter.DelegatingFilterProxy.doFilter (DelegatingFilterProxy.java263)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.web.filter.RequestContextFilter.doFilterInternal (RequestContextFilter.java99)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal (HttpPutFormContentFilter.java108)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal (HiddenHttpMethodFilter.java81)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.cloud.sleuth.instrument.web.TraceFilter.doFilter (TraceFilter.java164)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal (CharacterEncodingFilter.java197)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal (MetricsFilter.java106)
   at org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java107)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java166)
   at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java199)
   at org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java96)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java504)
   at org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java140)
   at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java81)
   at org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java87)
   at org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java342)
   at org.apache.coyote.http11.Http11Processor.service (Http11Processor.java803)
   at org.apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java66)
   at org.apache.coyote.AbstractProtocol$ConnectionHandler.process (AbstractProtocol.java790)
   at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun (NioEndpoint.java1459)
   at org.apache.tomcat.util.net.SocketProcessorBase.run (SocketProcessorBase.java49)
   at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java1149)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java624)
   at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run (TaskThread.java61)
   at java.lang.Thread.run (Thread.java748)

我使用c3p0数据源进行连接,上面的异常由c3p0记录如下

[c3p0] Another error has occurred [ com.microsoft.sqlserver.jdbc.SQLServerException: The connection is closed. ] which will not be reported to listeners!

请为此提供帮助,以确保顺畅连接。

1 个答案:

答案 0 :(得分:1)

当您设计真实世界的云应用时,您必须考虑的一件事是如何处理临时服务中断。此问题在云应用程序中非常重要,因为您非常依赖网络连接和外部服务。你可以经常得到一些通常会自我修复的小故障,如果你不准备聪明地处理它们,它们会给你的顾客带来糟糕的体验。

在云环境中,您会发现定期发生失败和丢弃的数据库连接。这部分是因为与Web服务器和数据库服务器具有直接物理连接的本地环境相比,您将经历更多负载平衡器。在其他情况下,您可能过于频繁地访问服务,并且服务故意限制您 - 拒绝连接 - 以防止您对服务的其他租户产生负面影响。

您可以识别通常是暂时性的错误,并自动重试导致错误的操作,而不是抛出异常并向客户显示不可用或错误页面,希望不久之后您将获得成功。大多数情况下,操作将在第二次尝试时成功,并且您将从错误中恢复,而客户不会意识到存在问题。

protected void handleTransientException(SQLException sqlE) 
        throws RetryPolicyException, SQLException { 

    // if no retries left throw exception 
    if (!isRetriesLeft()) { 
        throw new RetryPolicyException("Attempt to retry for " 
                + retryCount 
                + " times falied with " + waitingTime 
                + " millsecond interval"); 
    } 
    // Decrement the retries count 
    retriesLeft--; 
    if (isTransient(sqlE)) { 
        // Wait for specified time 
        waitForRetryInterval(); 
    } else { 
        // Its a Non transient exception Re throw the exception 
        retriesLeft = 0; 
        throw sqlE; 
    } 
} 

有关详细信息,请访问this页面。您还可以访问this文档以获取连接建议。