是否可以使用 Hibernate 连接到 oracle 12c 可插拔数据库?

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

我在 Hibernate 中创建了一个测试应用程序并使用 Oracle 12c 作为数据库。我能够连接到主数据库的 "sys" 用户。我能够在这个数据库中创建、插入、更新表。但是我无法连接到可插拔数据库。有什么方法可以使用休眠连接到 PDB。 Test 是我的主数据库,pdborcl 是我的 PDB。我也为此尝试了 google,但是 Hibernate 和 Oracle 12c 上没有 post,但没有特定于 PDB 的内容。有这篇文章说不支持 12c。 https://github.com/denimgroup/threadfix/issues/488这是真的吗? 就我对 Oracle 12c 的理解而言,CDB 仅用于管理目的,PDB 将是用于应用程序目的的数据库。

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>  

当我尝试通过将测试更改为 pdborcl 来连接到 PDB 时,出现以下异常。我能够从我的系统 ping 测试和 pdborcl。我还可以从终端使用 sqlplus 连接到两个数据库。

当我尝试连接到 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.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

你的语法有点错误。确保使用 /SERVICE_NAME 而不是 :SID。 对于 jdbc 连接到可插入数据库,不能再使用 :SID。 (好吧,它可以但不要去那里) 通过 service_name 访问 Oracle 数据库是自 Oracle 8i 以来的正确方法。那是 100 年前!

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

您将通过询问听众找到可用的服务

lsnrctl service

sys账户是一个特殊账户。 Sys 对数据库的意义与 root 用户对 OS 的意义相同。您可以对这些用户造成伤害!而是创建一个应用程序 account/user。

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

先生Hall 解释所有 12c 连接场景 here

祝你好运,

比亚特