【问题标题】:How to do an automatic pooled reconnection on an Oracle and a grails project when a database connection is interrupted?当数据库连接中断时,如何在 Oracle 和 grails 项目上进行自动池重新连接?
【发布时间】:2022-01-24 20:30:16
【问题描述】:

我有一个项目使用了 grails 框架 3.X 和 Oracle DB,并且该项目部署在 tomcat 9.0.X 上。

项目存在DB连接问题,当项目与DB之间的连接、防火墙或任何网络配置发生变化时,DB连接中断,项目无法自动重新连接DB;或 DB 的设置已更改或 DB 已重新启动。正因为如此,现在,当这个问题发生时,我手动将项目重新部署在 tomcat 服务器上。

为了解决这个问题,我尝试了很多改变,但问题还没有消失。例如,我在 application.yml 上更改了 dataSource 配置的属性,并尝试了不同版本的 OJDBC 驱动程序。

当我在 application.yml 上将“pool”的值从 true 更改为 false 时,中断的连接可以自动重新连接。但是,我认为“pool: false”配置会产生负面影响,因为数据库连接操作很昂贵。例如,要达到最大连接限制,...

所以,我的问题是,当数据库连接中断时,如何在 Oracle 数据库和 grails 项目上进行自动池重新连接?

我目前在application.yml上的配置(可能因为测试有些属性发生了变化):

dataSources:
    dataSource:
        pooled: true
        jmxExport: true
        driverClassName: oracle.jdbc.OracleDriver

environments:
    development:
        dataSources:
            dataSource:
                logSql: false
                dbCreate: update
                url: jdbc:oracle:thin:@localdomain:1521/test
                username: test
                password: test
                properties:
                    maxActive: 50
                    maxIdle: 25
                    minIdle: 1
                    initialSize: 5
                    numTestsPerEvictionRun: 3
                    maxWait: 1000
                    testOnBorrow: true
                    testWhileIdle: true
                    testOnReturn: true
                    minEvictableIdleTimeMillis: 1000
                    timeBetweenEvictionRunsMillis: 1000
                    validationQuery: SELECT 1 FROM DUAL
                    validationQueryTimeout: 5
                    validationInterval: 5
                    removeAbandoned: true
                    removeAbandonedTimeout: 5
    production:
        dataSources:
            dataSource:
                dbCreate: none
                jndiName: "java:comp/env/testDs"
                properties:
                    maxActive: 50
                    maxIdle: 25
                    minIdle: 1
                    initialSize: 5
                    numTestsPerEvictionRun: 3
                    maxWait: 1000
                    testOnBorrow: true
                    testWhileIdle: true
                    testOnReturn: true
                    minEvictableIdleTimeMillis: 1000
                    timeBetweenEvictionRunsMillis: 1000
                    validationQuery: SELECT 1 FROM DUAL
                    validationQueryTimeout: 5
                    validationInterval: 5
                    removeAbandoned: true
                    removeAbandonedTimeout: 5

【问题讨论】:

  • 你能说一下你试过哪个版本,你的数据库是什么版本?
  • @Codebling 我已经尝试了几个版本的 OJDBC8。数据库版本是 12c (12.1)。
  • 您可以尝试将 oracle.net.DOWN_HOSTS_TIMEOUT 更改为 1(秒)。也可以将oracle.net.keepAlive 设置为trueOracleConnection settings。 Grails 使用什么连接池?
  • 我项目的连接池设置是默认的。据我了解,连接池是Tomcat JDBC Pool。
  • Grails默认的ORM是Hibernate ORM。

标签: java oracle tomcat grails ojdbc


【解决方案1】:

您可以尝试设置validationQuery属性为null,检查validationQuery属性是否与其他属性冲突。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 我尝试设置validationQuery属性为null,但问题依旧存在。
猜你喜欢
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 2015-08-09
相关资源
最近更新 更多