如何在hibernate 4.3 Integrator中获取JDBC连接?

时间:2014-11-28 23:29:40

标签: java hibernate jdbc orm multi-tenant

为hibernate 4.3实现org.hibernate.integrator.spi.Integrator时,会得到一个ConfigurationSessionFactoryImplementorSessionFactoryServiceRegistry对象。

获取元数据的一种方法是获取连接提供程序: sessionFactory.getJdbcServices().getConnectionProvider()

getConnectionProvider()已被弃用,不适用于多租户设置。

Javadocs说

  

应尽可能通过org.hibernate.engine.jdbc.spi.JdbcConnectionAccess访问通过ConnectionProvider进行连接。

但我的问题是,我找不到获得JdbcConnectionAccess的方法。可以使用给定的SessionFactoryServiceRegistry并复制SessionFactoryImpl#buildLocalConnectionAccess()中的代码,但这不是一个好的解决方案。

Integrator中获取连接的推荐方法是什么?

3 个答案:

答案 0 :(得分:1)

我对弃用的方法“sessionFactory.getJdbcServices()。getConnectionProvider()”也有类似的问题。我使用此函数来获取FlywayIntegrator的dataSource。我在这个示例中使用了它们:http://blog.essential-bytes.de/flyway-hibernate-und-jpa-integrieren

没有getConnectionProvider(),我没有找到解决方案来获取连接数据库所需的属性。

作为一种解决方法,我将这些属性放在我的jboss配置的standalone.xml文件中,如下所示:

<system-properties>
    <property name="com.mycomp.myapp.servername" value="localhost"/>
    <property name="com.mycomp.myapp.databasename" value="xxx"/>
    <property name="com.mycomp.myapp.databaseuser" value="yyy"/>
    <property name="com.mycomp.myapp.databasepassword" value="zzz"/>
</system-properties>

在我的flyway集成商中,我现在可以通过以下方式阅读这些属性:

private DataSource getDataSourceFromSystemProperty() {
    String servername = System.getProperty("com.mycomp.myapp.servername");
    if (servername == null || servername.isEmpty()) {
        logger.warning("No system property found in standalone.xml file for com.mycomp.myapp.servername");
        servername = "localhost";
    }
    String databaseName = System.getProperty("com.mycomp.myapp.databasename");
    if (databaseName == null || databaseName.isEmpty()) {
        logger.warning("No system property found in standalone.xml file for com.mycomp.myapp.databasename");
        databaseName = "xxx";
    }
    String databaseUser = System.getProperty("com.mycomp.myapp.databaseuser");
    if (databaseUser == null || databaseUser.isEmpty()) {
        logger.warning("No system property found in standalone.xml file for com.mycomp.myapp.databaseuser");
        databaseUser = "yyy";
    }
    String databasePassword = System.getProperty("com.mycomp.myapp.databasepassword");
    if (databasePassword == null || databasePassword.isEmpty()) {
        logger.warning("No system property found in standalone.xml file for com.mycomp.myapp.databasepassword");
        databasePassword = "zzz";
    }

    final PGSimpleDataSource dataSource = new PGSimpleDataSource();
    dataSource.setServerName(servername);
    dataSource.setPortNumber(5432);
    dataSource.setDatabaseName(databaseName);
    dataSource.setUser(databaseUser);
    dataSource.setPassword(databasePassword);
    return dataSource;
}

也许这有帮助...

答案 1 :(得分:0)

您可以从Integrator实施中执行以下操作:

SessionImplementor sessionImplementor = (SessionImplementor) sessionFactory.getCurrentSession();
Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection();

这里的sessionFactory是您从实现的方法中获得的SessionFactoryImplementor。

答案 2 :(得分:0)

在Hibernate 5中的Integrator.integrate()方法中获取连接可以通过以下方式完成:

final DataSource ds = (DataSource) sessionFactory.getProperties().get("hibernate.connection.datasource");
final Connection conn = ds.getConnection();