oracle数据库的连接重试/超时问题

时间:2020-05-12 17:31:31

标签: java spring hibernate spring-boot oracle12c

在一个multi-modules/components项目中,我尝试使用oracle db image,其中服务名称为test的组件(spingboot应用程序)依赖于服务名称为oracle-database的组件test-database。当我使用docker-compose up来启动应用程序时,数据库服务需要花费很长时间才能启动并运行。但是,由于oracle DB服务当时还没有启动,因此测试服务立即抛出异常:

Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection

test_1           | Caused by: java.net.ConnectException: Connection refused (Connection refused)

我正在使用以下数据源/ JPA配置(application.properties文件)连接到数据库:

spring.datasource.url=jdbc:oracle:thin:@(DESCRIPTION_LIST=(FAILOVER=OFF)(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=test-database)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=testdb1.localdomain))))
spring.datasource.username=******
spring.datasource.password=******
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.default_schema=test
spring.jpa.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect

所以我尝试在TNS值中添加超时时间,重试次数和重试延迟,如下所示:

spring.datasource.url=jdbc:oracle:thin:@(DESCRIPTION_LIST=(FAILOVER=OFF)(DESCRIPTION=(CONNECT_TIMEOUT=600)(TRANSPORT_CONNECT_TIMEOUT=150)(RETRY_COUNT=10000)(RETRY_DELAY=3)(ADDRESS=(PROTOCOL=TCP)(HOST=test-database)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=testdb1.localdomain))))

使用这种配置,当我尝试启动应用程序时,test应用程序正在等待数据库服务启动,一旦启动,test服务就会抛出异常:

Caused by: java.sql.SQLRecoverableException: Listener refused the connection with the following error: test_1 | ORA-12514, TNS:listener does not currently know of service requested in connect descriptor

我认为数据库服务尚未准备就绪,这就是test应用程序无法连接到数据库服务且test应用程序失败的原因。

有趣的是,它甚至没有尝试重新连接。我已经尝试通过调整超时和重试参数值来实现,但是没有运气。

是否有任何配置可以强制test应用程序一次又一次地重新连接,直到建立连接?

请注意:应用程序正在tomcat中运行。

0 个答案:

没有答案