Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to connect to oracle 12c pluggable database using Hibernate?

I have created a test application in Hibernate and using Oracle 12c as database. I was able to connect to "sys" user of main database. I am able to create, insert, update the tables in this DB. But I am unable to connect to pluggable database. Is there any way to connect to PDB using hibernate. Test is my main DB and pdborcl is my PDB. I also tried google for this, but there is no post on Hibernate and Oracle 12c, but nothing specific to PDB. There is this article which says there is no support for 12c. https://github.com/denimgroup/threadfix/issues/488 Is this true? As far as my understanding of Oracle 12c goes, CDB is only for administration purposes and PDB will be the database used for application purposes.

hibernate.cfg.xml

    <?xml version="1.0" encoding="utf-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC  
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
<hibernate-configuration>  
    <session-factory>  

        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:test</property>  
        <property name="connection.username">sys as sysdba</property>  
        <property name="connection.password">sys</property>  
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>  
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>  

        <property name="show_sql">true</property>  

        <property name="format_sql">true</property>  
        <property name="hbm2ddl.auto">create</property>  

        <!-- JDBC connection pool (use the built-in) -->  
        <property name="connection.pool_size">1</property>  
        <property name="current_session_context_class">thread</property>  

        <mapping class="com.test.domain.Employee" />  
        <mapping class="com.test.domain.Department" />  

          </session-factory>  
</hibernate-configuration>  

When I try to connect to PDB by changing test to pdborcl it gives me below exception. I am able to ping both test and pdborcl from my system. I am also able to connect to both DBs using sqlplus from terminal.

I get below exception when I try to connect to PDB.

    Jan 15, 2015 10:30:22 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jan 15, 2015 10:30:22 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [oracle.jdbc.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:pdborcl]
Jan 15, 2015 10:30:22 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=sys as sysdba, password=****}
Jan 15, 2015 10:30:22 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jan 15, 2015 10:30:22 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Initial SessionFactory creation failed.org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.test.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at com.test.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.test.hibernate.HibernateTest.main(HibernateTest.java:15)
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at com.test.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more
Caused by: 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

    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
    ... 16 more
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:361)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
    ... 21 more
like image 357
Shiva Avatar asked Jan 17 '15 05:01

Shiva


People also ask

Does hibernate work with Oracle?

As you can see, code remains the same for different databases. So to use Hibernate with Oracle database, you need to use proper JDBC driver, connection properties and create sequence.

What is the difference between CDB and PDB in Oracle?

A CDB includes zero, one, or many customer-created pluggable databases (PDBs). A PDB is a portable collection of schemas, schema objects, and nonschema objects that appears to an Oracle Net client as a non-CDB.

How do I connect to a pluggable database in SQL Developer?

In SQL Developer, the DBA Navigator enables you to perform full administration of container databases and pluggable databases. Select View > DBA. On the DBA tab, right-click Connections and select Add Connection. In the Select Connection dialog box, select sys and click OK.


Video Answer


1 Answers

Your syntax is slightly wrong. Make sure you use the /SERVICE_NAME not the :SID. For jdbc to connect to a pluggable database, :SID can no longer be used. (well, it can but don't go there) Accessing an Oracle Database by service_name is the correct approach since Oracle 8i. And that is 100 it-years ago!

jdbc:oracle:thin:@localhost:1521/my_pdb_service_name

You will find available services by asking the listener

lsnrctl service

The sys account is a special account. Sys has the same meaning for the database as the root user has for the OS. You can do harm with these users! Instead create an application account/user.

create user mysuperapp identified by "Neverloginassys";
grant create session to mysuperapp;   

Mr. Hall explains all 12c connection scenarios here

Best of luck,

Bjarte

like image 59
Bjarte Brandt Avatar answered Oct 06 '22 00:10

Bjarte Brandt