使用Oracle DB的Spring启动项目有时会获得IO连接超时

时间:2018-04-17 07:59:34

标签: oracle spring-boot jpa

我使用Spring Boot + JPA部署应用程序,并使用Oracle DB存储数据。 有时我在使用应用程序一段时间后在日志中出现此错误。

SQL Error: 17002, SQLState: 08006 - IO Error: Connection timed out

配置属性

server.port = 8095
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring.datasource.url=jdbc:oracle:thin:@//localhost:1521/xe
spring.datasource.username=root
spring.datasource.password=root
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

如果我将autoReconnect=true添加到数据源网址,我在启动应用时遇到错误。

SQL Error: 12514, SQLState: 08006
oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor

at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:272)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
... 46 common frames omitted

有谁知道问题的根本原因以及如何解决它?感谢。

1 个答案:

答案 0 :(得分:0)

从SQLPlus测试Oracle DB连接。 另外,检查您的Oracle service_name在tnsnames.ora文件中是否有效。

例如,如果您在tnsnames.ora中有用户条目,则如下所示:

SAMPLEUSER =

  (DESCRIPTION =

    (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = <fqdnHostNameOrAddress>)(PORT = <validPORT>))
)
(CONNECT_DATA =
  (SERVER = DEDICATED)
  (SERVICE_NAME = <OracleDBServiceName>)
)

然后使用SQLPlus进行测试,如下所示:

sqlplus user @ SAMPLEUSER