首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在hibernate 4.3 Integrator中获取JDBC连接?

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

Stack Overflow用户
提问于 2014-11-28 23:29:39
回答 3查看 3.2K关注 0票数 5

在为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中获取连接的推荐方法是什么?

EN

回答 3

Stack Overflow用户

发布于 2015-01-27 11:50:39

对于不推荐的方法"sessionFactory.getJdbcServices().getConnectionProvider()".,我也有类似的问题。我使用这个函数为我的dataSource获取FlywayIntegrator。我在本例中使用了它们:http://blog.essential-bytes.de/flyway-hibernate-und-jpa-integrieren

没有getConnectionProvider(),我就找不到一个解决方案来获得与数据库连接所需的属性。

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

代码语言:javascript
复制
<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>

在我的天桥积分器中,我现在可以通过以下方法读取这些属性:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-06-15 12:35:37

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

代码语言:javascript
复制
final DataSource ds = (DataSource) sessionFactory.getProperties().get("hibernate.connection.datasource");
final Connection conn = ds.getConnection();
票数 1
EN

Stack Overflow用户

发布于 2014-12-10 21:30:18

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

代码语言:javascript
复制
SessionImplementor sessionImplementor = (SessionImplementor) sessionFactory.getCurrentSession();
Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection();

这里的sessionFactory是从实现的方法中接收到的SessionFactoryImplementor。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27197161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档