【问题标题】:Connect to a database dynamically in hibernate在休眠中动态连接到数据库
【发布时间】:2023-03-29 13:09:01
【问题描述】:

所以我有多个可插拔数据库 (PDB),我想使用 Hibernate 动态连接到其中的任何一个。如何实现这样的功能?

要连接到 PDB1(以及其他 PDB),我有:

protected void setupPdb1() {
    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure("hibernate-1.cfg.xml") // configures settings from hibernate.cfg.xml
            .build();
    try {
        sessionFactory1 = new MetadataSources(registry).buildMetadata().buildSessionFactory();
    } catch (Exception ex) {
        StandardServiceRegistryBuilder.destroy(registry);
        throw new RuntimeException(ex);
    }
}

我的休眠配置文件对应PDB1如下:

<?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/pdb1.oradev.oraclecorp.com</property>
    <property name="connection.username">test</property>
    <property name="connection.password">password12</property>
    <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="dialect">org.hibernate.dialect.Oracle12cDialect</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="net.codejava.hibernate.Book" />

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

这种方法的问题是每个 PDB 都有一个配置文件。如何使用 Hibernate 动态选择要连接的 PDB?

【问题讨论】:

    标签: java xml database hibernate oracle12c


    【解决方案1】:

    这取决于你的策略,切换数据库的触发器是什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多