How to be specific about which CDB I want to connect to wrapping a PDB

multi-tenantoracleoracle-12csqlplus

I'm quite new to Oracle. I created a CDB like cdb1 and then a PDB within like pdb1. Now I can connect to my PDB using something like:

sqlplus user/password@server:port/pdb1

But can't I have two PDBs with the same name in different CDBs? If that happened, sqlplus wouldn't know which PDB I wanted to connect to. Does the listener have some robust way to map the PDB passed in the connection to an actual PDB within a CDB on the machine where the listener is running?

EDIT: I found out that when I write pdb1 in the sqlplus connection identifier, that is actually not the name of the PDB, but the name of the service, which just happens to be the same as the name of the PDB.

As far as I understand, I can map the service name to PDB name using select name, pdb from v$services;.

It seems to map services to CDBs I need to type lsnrctl service. And then I get output like:

Service "pdb1" has 1 instance(s).
  Instance "cdb1", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0 state:ready
         LOCAL SERVER

So I guess the instance cdb1 is referring to the CDB cdb1? Or does it also just happen to have the same name?

Then again, surely the service stating that it has 1 instance, means it could have 2 instances and these could point to separate CDBs, which brings me back to my original question of how to differentiate different PDBs with the same name.

Best Answer

Quite easily.

The full syntax for Easy Connect is:

Understanding the Easy Connect Naming Method

CONNECT username@[//]host[:port][/service_name][:server][/instance_name]

You just need to specify the service_name and the instance_name as well.

$ lsnrctl services
...
Service "pdb1" has 2 instance(s).
  Instance "RAIN", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:3 refused:0 state:ready
         LOCAL SERVER
  Instance "WIND", status READY, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:3 refused:0 state:ready
         LOCAL SERVER

You will have more than 1 instance serving the same service. The below may connect to any of the instances, but the listener decides which instance it forwards to your request, not you:

sqlplus user/password@localhost:1521/pdb1

The below commands always connect to just one of the instances. If that instance is down, the connection attempt fails, even though there is another instance providing the same service:

sqlplus user/password@localhost:1521/pdb1/rain
sqlplus user/password@localhost:1521/pdb1/wind