1、本机tnsnames.ora 配置如下
test4=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.4)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
2、代码配置如下:
spring.datasource.url = jdbc:oracle:thin:@192.168.1.4:1521:test4
spring.datasource.username = username
spring.datasource.password = password
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
3、看似好像配置没有问题,但是运行起来一直报ORA-12505错误。
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
4、反复测试Oracle服务端监听正常。
5、最后发现配置问题,改正为如下配置:
①spring.datasource.url = jdbc:oracle:thin:@//192.168.1.4:1521/orcl
②spring.datasource.url = jdbc:oracle:thin:@192.168.1.4:1521:orcl
以上两种方法均可以。
6、总结:Oracle有两种配置方式,SID指的是全局数据库名,而并非你本地的tnsnames.ora中配置的别名。
所以,在配置的时候,应该首先知道数据库配置的全局数据库名,再去代码中配置。
格式一:jdbc:oracle:thin:@//<host>:<port>/<service_name>
格式二:jdbc:oracle:thin:@<host>:<port>:<SID>
格式三:jdbc:oracle:thin:@<TNSName>