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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27996365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:41:49  来源:igfitidea点击:

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

javadatabaseoraclehibernate

提问by Shiva

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/488Is 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 中创建了一个测试应用程序,并使用 Oracle 12c 作为数据库。我能够连接到主数据库的“sys”用户。我能够创建、插入、更新这个数据库中的表。但我无法连接到可插拔数据库。有什么方法可以使用休眠连接到 PDB。测试是我的主数据库,pdborcl 是我的 PDB。我也为此尝试过谷歌,但没有关于 Hibernate 和 Oracle 12c 的帖子,但没有特定于 PDB 的帖子。有这篇文章说不支持12c。https://github.com/denimgroup/threadfix/issues/488这是真的吗?就我对 Oracle 12c 的理解而言,CDB 仅用于管理目的,而 PDB 将是用于应用程序目的的数据库。

hibernate.cfg.xml

休眠文件.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.

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

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

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

回答by Bjarte Brandt

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!

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

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.

sys 帐户是一个特殊帐户。Sys 对于数据库的含义与 root 用户对于 OS 的含义相同。您可能会对这些用户造成伤害!而是创建一个应用程序帐户/用户。

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

Mr. Hall explains all 12c connection scenarios here

霍尔先生在这里解释了所有 12c 连接场景

Best of luck,

祝你好运,

Bjarte

比亚特