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

Chr*_*lph 5 java orm hibernate jdbc multi-tenant

在实现org.hibernate.integrator.spi.Integratorfor hibernate 4.3 时,会得到一个Configuration,SessionFactoryImplementorSessionFactoryServiceRegistry对象。

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

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

Javadocs 说

只要有可能,访问连接 viaorg.hibernate.engine.jdbc.spi.JdbcConnectionAccess应该优先于访问 via ConnectionProvider

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

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

小智 0

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

SessionImplementor sessionImplementor = (SessionImplementor) sessionFactory.getCurrentSession();
Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection();
Run Code Online (Sandbox Code Playgroud)

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

  • 在集成器中,没有当前会话,因此会抛出“HibernateException”,并显示消息“未配置 CurrentSessionContext!”。 (2认同)