Skip to Main Content
  • Questions
  • Set Default Connection to PDB in Oracle 12c

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, TOM.

Asked: August 11, 2016 - 6:28 am UTC

Last updated: August 14, 2016 - 5:44 am UTC

Version: 12c

Viewed 10K+ times! This question is

You Asked

Hi Tom and Team,

I am using a Oracle 12c and i have created a PDB,( say PDB_1.), a USER (say USER_1) and a table (say TABLE_1)
When i query to USER_1.TABLE_1 from a java application it is not running due to the default session is CDB.
If i run "ALTER SESSION SET CONTAINER = PDB_1" and then i query it is working fine.This is happening in SQL Developer and in Java code

My question is is there any way i can specify my default Connection/ session is PDB_1 so i dont have to alter the session every time in Java code.

Can we do it in the Java code, if yes, how?
Can we do it in the Server Side, if Yes, how?

Thanks,
Tom

and Connor said...

Each PDB will register itself as a service. So in your tnsnames.ora file, you might have:

CDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = CDB)
    )
  )

PDB1 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = PDB1)
    )
  )



and so forth.

So any connection that does user/pass@db can easily pick a pdb.

If you are *on* the database server, and have connections that do just: user/pass, then simply set the environment variable TWO_TASK to the name of the PDB service.

Hope this helps.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Oracle connection to Pluggable Database

TOM JAIN, August 13, 2016 - 5:50 pm UTC

Yes. It worked and my connection string looks like

Connection conn = DriverManager.getConnection(dbc:oracle:thin:@<host>:<port>/<PDB1>, username, password);

Thanks
Connor McDonald
August 14, 2016 - 5:44 am UTC

glad we could help

More to Explore

Multitenant

Need more information on Multitenant? Check out the Multitenant docs for the Oracle Database