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

时间:2015-01-17 05:09:22

标签: java database oracle hibernate

我在Hibernate中创建了一个测试应用程序,并使用Oracle 12c作为数据库。我能够连接到主数据库的“sys”用户。我能够在此DB中创建,插入,更新表。但我无法连接到可插拔数据库。有没有办法使用hibernate连接到PDB。测试是我的主要数据库,pdborcl是我的PDB。我也尝试过google,但Hibernate和Oracle 12c上没有帖子,但没有特别针对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连接到两个DB。

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

1 个答案:

答案 0 :(得分:8)

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

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

您可以通过询问听众

找到可用的服务
lsnrctl service

sys帐户是一个特殊帐户。 Sys与root用户对OS的含义相同。你可以伤害这些用户!而是创建一个应用程序帐户/用户。

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

先生。 Hall解释了所有12c连接方案here

祝你好运,

比阿特