Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【已解决】sqlserver2008 连接中断后,可以自动重连问题解决 #1331

Closed
Hisen158 opened this issue Jul 15, 2016 · 9 comments
Closed
Milestone

Comments

@Hisen158
Copy link
Contributor

Hisen158 commented Jul 15, 2016

在实际项目(多数据源)中发现sqlserver连接中断后,无法支持自动重连,拿不到connect:

1.首先通过我的监控页面发现 sqlserver配置的连接池 连接有效性检查类名 为 null。

2.跟踪代码 com.alibaba.druid.pool.DruidDataSource.java initValidConnectionChecker() 方法中,
必须对应是“com.microsoft.jdbc.sqlserver.SQLServerDriver”才会实例化连接有效性检查类,那就是必须使用msbase.jar版本驱动,而我使用的是jtds-1.2.7.jar

3.解决方式A ,替换驱动为 (msbase.jar mssqlserver.jar msutil.jar)。ops!仍然无法解决,可能是我的数据库 是SQLSERVER2008的问题?

4.解决方式B,在2处initValidConnectionChecker()方法(889行)增加条件判断代码,使用jtds驱动也实例化MSSQLValidConnectionChecker.java()

            else if (realDriverClassName.equals(JdbcConstants.SQL_SERVER_DRIVER)
                ||realDriverClassName.equals("net.sourceforge.jtds.jdbc.Driver")
                ||realDriverClassName.equals("com.microsoft.sqlserver.jdbc.SQLServerDriver")) {
          this.validConnectionChecker = new MSSQLValidConnectionChecker();
}

5.使用jtds.jar测试业务通过,sqlserver断线后可自动重连。(模拟网络中断,又恢复网络的情况,每周有个业务系统凌晨要重启路由)

疑问:
1.三种 sqlserver 驱动 ,(msbase.jar mssqlserver.jar msutil.jar)、(sqljdbc4.jar)、(jtds-1.2.7.jar)
都调用 MSSQLValidConnectionChecker.java 作为校验类是否有其他问题?

2.另外必须配置validationQueryTimeout参数,默认为-1 ,不启动检查.
<property name="validationQueryTimeout" value="30000" />

3.请文少和其他大神评估是否有其他影响,希望采纳意见,我是在1.0.15版本下改的测试没问题,目前jtds-1.2.7.jar 驱动用的企业应该更多。

4.附spring-mybatis.xml 关于sqlserver的连接池配置

<!-- SQLSERVER2008数据源 -->
<bean name="dataSourceSeal" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="jdbc:jtds:sqlserver://localhost:1433/testdb" />
        <property name="username" value="${seal_username}" />
        <property name="password" value="${seal_password}" />

        <property name="dbType" value="sqlserver"></property>
        <!-- jtds-1.2.7.jar 驱动 -->
        <!-- url连接格式:jdbc:jtds:sqlserver://localhost:1433/testdb -->
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>

        <!-- msbase.jar mssqlserver.jar msutil.jar 驱动 -->
        <!-- url连接格式:jdbc:sqlserver://localhost:1433;database=testdb -->
        <!--<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"></property>
        --> 

        <!-- sqljdbc4.jar 驱动   -->
        <!-- url连接格式:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testdb -->
        <!-- <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> -->

        <!-- 初始化连接大小 -->
        <property name="initialSize" value="0" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="100" />
        <!-- 连接池最大空闲 -->
        <!-- <property name="maxIdle" value="20" /> -->
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="0" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000" />
        <property name="proxyFilters">
            <list>
                <ref bean="log-filter" />
            </list>
         </property>
        <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
        <!-- 默认为-1 ,不校验,必填 -->
        <property name="validationQueryTimeout" value="30000" />
        <property name="validationQuery" value="select 'x'" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="180000" />

        <!--
        <property name="connectionErrorRetryAttempts" value="3"></property>
        <property name="timeBetweenConnectErrorMillis" value="15000"></property>
        <property name="breakAfterAcquireFailure" value="true"></property> 
        -->
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
        <!-- 监控数据库 -->
        <property name="filters" value="mergeStat" />
</bean>
@wenshao
Copy link
Member

wenshao commented Jul 15, 2016

你来提交一个Pull Request?最好今天,因为周末会发布新版本

@wenshao wenshao added this to the 1.0.23 milestone Jul 15, 2016
@Hisen158
Copy link
Contributor Author

行,今日提交。

@Hisen158
Copy link
Contributor Author

提交Pull Request 一定要安装github桌面程序吗? 我在eclipse中 Push to Upstream (master) 报权限错误,了。 https://github.com/alibaba/druid.git: git-receive-pack not permitted。忘指教。

@wenshao
Copy link
Member

wenshao commented Jul 15, 2016

fork,push到自己的repository,pull request

@wenshao
Copy link
Member

wenshao commented Jul 16, 2016

#1333

@wenshao wenshao closed this as completed Jul 16, 2016
@ZMRWEGO
Copy link

ZMRWEGO commented Jan 3, 2019

66666666666,新的版本都不存在这个问题了吗

@ZMRWEGO
Copy link

ZMRWEGO commented Jan 3, 2019

不过我现在使用的1.1.4版本,在多数据源下使用(本地sqlserver2008 远程两个mysql)还会出现sqlserver断线之后不能重连的情况,是配置的有问题吗

@ZMRWEGO
Copy link

ZMRWEGO commented Jan 3, 2019

spring:
  datasource:
    druid:
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      filters: "stat,wall,slf4j"
      initial-size: 1
      max-active: 30
      max-pool-prepared-statement-per-connection-size: 20
      max-wait: 60000
      min-evictable-idle-time-millis: 30000
      min-idle: 10
      pool-prepared-statements: true
      #检测池里连接的可用性 消耗性能 默认为false
      test-on-borrow: true
      test-on-return: false
      test-while-idle: true
      #心跳检测
      time-between-eviction-runs-millis: 5000
      useGlobalDataSourceStat: true
      validation-query: "select 'x'"
      #配置数据库自动重连:
      log-abandoned: true
      remove-abandoned: true
      remove-abandoned-timeout: 1800
      validation-query-timeout: 30000
    local:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      password: 
      url: "jdbc:sqlserver://192.168.5.20:1433;DatabaseName=Yktv56"
      username: 
    remote:
      driver-class-name: com.mysql.jdbc.Driver
      password: 
      url: "jdbc:mysql://58.49.35.14:3306/jmhx_scpm?useSSL=false&&autoReconnect=true"
      username: 
    remoteJob:
      driver-class-name: com.mysql.jdbc.Driver
      password: 
      url: "jdbc:mysql://47.96.95.76:3306/jmhx_scdc?useSSL=false&&autoReconnect=true"
      username: 
    type: com.alibaba.druid.pool.DruidDataSource

@urhosts
Copy link

urhosts commented Jan 21, 2021

spring:
  datasource:
    druid:
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      filters: "stat,wall,slf4j"
      initial-size: 1
      max-active: 30
      max-pool-prepared-statement-per-connection-size: 20
      max-wait: 60000
      min-evictable-idle-time-millis: 30000
      min-idle: 10
      pool-prepared-statements: true
      #检测池里连接的可用性 消耗性能 默认为false
      test-on-borrow: true
      test-on-return: false
      test-while-idle: true
      #心跳检测
      time-between-eviction-runs-millis: 5000
      useGlobalDataSourceStat: true
      validation-query: "select 'x'"
      #配置数据库自动重连:
      log-abandoned: true
      remove-abandoned: true
      remove-abandoned-timeout: 1800
      validation-query-timeout: 30000
    local:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      password: 
      url: "jdbc:sqlserver://192.168.5.20:1433;DatabaseName=Yktv56"
      username: 
    remote:
      driver-class-name: com.mysql.jdbc.Driver
      password: 
      url: "jdbc:mysql://58.49.35.14:3306/jmhx_scpm?useSSL=false&&autoReconnect=true"
      username: 
    remoteJob:
      driver-class-name: com.mysql.jdbc.Driver
      password: 
      url: "jdbc:mysql://47.96.95.76:3306/jmhx_scdc?useSSL=false&&autoReconnect=true"
      username: 
    type: com.alibaba.druid.pool.DruidDataSource

老哥,这个问题当年是最终怎么解决的哦,请教一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants