Oracle 18c XE:使用 SID 连接到 PDB(用于遗留应用程序)

Oracle 18c XE: Connecting to a PDB using SID (for legacy application)

我刚刚安装了 Oracle 18c XE Dadtabase 并在 CDB 中创建了一个 PDB (XEPDB1)。然后我在 PDF 中创建了一个新模式,我可以使用服务名称(SQL 开发人员)成功连接到该模式。

但我的遗留应用程序需要 SID 连接,这似乎允许使用 listener.ora 中的 USE_SID_AS_SERVICE_XE = on 行。我还将 SID_DESC 添加到 SID_LIST,但我仍然收到错误:

TNS:listener does not currently know of SID given in connect descriptor

这是我的 listener.ora:

DEFAULT_SERVICE_LISTENER = XE
USE_SID_AS_SERVICE_XE = on

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\app\michael\product.0.0\dbhomeXE)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:C:\app\michael\product.0.0\dbhomeXE\bin\oraclr18.dll")
    )
    (SID_DESC =
      (GLOBAL_DBNAME = XE
      (ORACLE_HOME = C:\app\michael\product.0.0\dbhomeXE)
      (SID_NAME = XEPDB1
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = persik.ddns.vmware.com)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

这是 tnsnames.ora:

XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )

XEPDB1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XEPDB1)
    )
  )

LISTENER_XE =
  (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))


ORACLR_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (CONNECT_DATA =
      (SID = CLRExtProc)
      (PRESENTATION = RO)
    )
  )

它应该是 USE_SID_AS_SERVICE_LISTENER_NAME refer 第 7.4.19 节在你的情况下监听器名称是 LISTENERUSE_SID_AS_SERVICE_LISTENER

带有 Java 代码和 18xe docker 容器的测试用例。

注释掉 listener.ora 和 运行 java 代码中的行

[oracle@240946cde855 admin]$ cat listener.ora  | grep on
# listener.ora Network Configuration File:
#USE_SID_AS_SERVICE_LISTENER=on

 $grep "1521" Conn.java
  Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.99.128:1521:xepdb1", "hr", "hr");

$ java -cp "ojdbc8.jar" Conn
Exception in thread "main" 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

取消注释并弹跳侦听器

[oracle@240946cde855 admin]$ sed -i 's|#USE|USE|g' listener.ora
[oracle@240946cde855 admin]$ cat listener.ora  | grep on
# listener.ora Network Configuration File:
USE_SID_AS_SERVICE_LISTENER=on
[oracle@240946cde855 admin]$ lsnrctl reload

LSNRCTL for Linux: Version 18.0.0.0.0 - Production on 22-JUL-2020 10:39:52

Copyright (c) 1991, 2018, Oracle.  All rights reserved.

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
The command completed successfully

$ java -cp "ojdbc8.jar" Conn
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production