Spring Boot,SQL Server,AKS,连接已关闭

时间:2018-12-03 15:15:03

标签: java sql-server hibernate

我们有一个运行在AKS(Azure Kubernetes服务)上的spring boot应用程序。 在该服务中,我们注入并调用过程调用程序类:

package com.thing;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ProcedureInvoker {

@PersistenceContext
private final EntityManager entityManager;

@Autowired
public ProcedureInvoker(final EntityManager entityManager) {
    this.entityManager = entityManager;
}


public int getValueCall(String user, String password) {


    StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("dbo.CHECK_USER");


    // In out parameters
    storedProcedureQuery.registerStoredProcedureParameter(1, String.class, ParameterMode.IN);
    storedProcedureQuery.registerStoredProcedureParameter(2, String.class, ParameterMode.IN);
    storedProcedureQuery.registerStoredProcedureParameter(3, Integer.class, ParameterMode.INOUT);


    //
    storedProcedureQuery.setParameter(1, user);
    storedProcedureQuery.setParameter(2, password);
    storedProcedureQuery.setParameter(3, new Integer(-3));

    Object result = storedProcedureQuery.getOutputParameterValue(3);

    storedProcedureQuery.execute();

    int resu = ((Integer)result).intValue();

    return resu; 
}

}

有时候(经过1天或几个小时的正常工作;大约有3000个电话),我们开始收到此错误:

com.microsoft.sqlserver.jdbc.SQLServerException:
   at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError 
(SQLServerException.java227)
   at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed 
(SQLServerConnection.java796)
   at com.microsoft.sqlserver.jdbc.SQLServerConnection.setNetworkTimeout 
(SQLServerConnection.java4768)
   at com.zaxxer.hikari.pool.PoolBase.setNetworkTimeout (PoolBase.java541)
   at com.zaxxer.hikari.pool.PoolBase.quietlyCloseConnection 
(PoolBase.java129)
   at com.zaxxer.hikari.pool.HikariPool.lambda$closeConnection$1 
(HikariPool.java434)
   at java.util.concurrent.ThreadPoolExecutor.runWorker 
(ThreadPoolExecutor.java1149)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run 
(ThreadPoolExecutor.java624)
   at java.lang.Thread.run (Thread.java748)

为解决此问题,我们删除了Pod以重新启动它们。但这不是解决方案。

Pom有:

<!--  DDBB -->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- End DDBB -->

来自Yaml的属性

spring.datasource.driverClassName: 
com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql: false
spring.jpa.hibernate.dialect: org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.properties.hibernate.format_sql: true
logging.level.org.hibernate.type: debug

已解决! 最后,我们添加了一个解决方案:

entityManager.close()

我们认为句子对于程序或功能不是必需的(它已在春季关闭)。

该问题可能来自SQL Server,因为该过程具有未声明的return语句(我们在Spring调用它时遇到了一些问题)。也许SQL Server将连接的关闭委托给spring,而spring则相反(委托给SQL Server)

0 个答案:

没有答案