在为hibernate 4.3实现org.hibernate.integrator.spi.Integrator时,可以获得一个Configuration、SessionFactoryImplementor和SessionFactoryServiceRegistry对象。
获取元数据的一种方法是获取连接提供程序:sessionFactory.getJdbcServices().getConnectionProvider()
但是getConnectionProvider()是不推荐的,并且不适用于多租户设置。
Javadocs说
只要有可能,通过
org.hibernate.engine.jdbc.spi.JdbcConnectionAccess访问连接应该优先于通过ConnectionProvider访问。
但我的问题是,我找不到获得JdbcConnectionAccess的方法。可以使用给定的SessionFactoryServiceRegistry并从SessionFactoryImpl#buildLocalConnectionAccess()复制代码,但这不是一个很好的解决方案。
在Integrator中获取连接的推荐方法是什么?
发布于 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文件中,如下所示:
<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>在我的天桥积分器中,我现在可以通过以下方法读取这些属性:
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;
}也许这有帮助..。
发布于 2017-06-15 12:35:37
在Hibernate 5中获取Integrator.integrate()方法中的连接可以通过以下方式完成:
final DataSource ds = (DataSource) sessionFactory.getProperties().get("hibernate.connection.datasource");
final Connection conn = ds.getConnection();发布于 2014-12-10 21:30:18
您可以从Integrator实现中执行以下操作:
SessionImplementor sessionImplementor = (SessionImplementor) sessionFactory.getCurrentSession();
Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection();这里的sessionFactory是从实现的方法中接收到的SessionFactoryImplementor。
https://stackoverflow.com/questions/27197161
复制相似问题