When implementing an org.hibernate.integrator.spi.Integrator
for hibernate 4.3, one gets a Configuration
, SessionFactoryImplementor
and SessionFactoryServiceRegistry
object.
One way to fetch the metadata is to obtain a connection provider:
sessionFactory.getJdbcServices().getConnectionProvider()
But getConnectionProvider()
is deprecated and does not work for a multi tenant setup.
Javadocs say
Access to connections via
org.hibernate.engine.jdbc.spi.JdbcConnectionAccess
should be preferred over access viaConnectionProvider
, whenever possible.
But my problem is, that I don't find a way to obtain a JdbcConnectionAccess
. It would be possible to use the given SessionFactoryServiceRegistry
and replicate the code from SessionFactoryImpl#buildLocalConnectionAccess()
, but that is not a nice solution.
What is the recommended way to fetch a connection in an Integrator
?
I had a similar problem with deprecated method "sessionFactory.getJdbcServices().getConnectionProvider()". I used this function to get the dataSource for my FlywayIntegrator. I used them like in this example: http://blog.essential-bytes.de/flyway-hibernate-und-jpa-integrieren
Without the getConnectionProvider() i didn't found a solution to get the needed properties to connect with my database.
As a workaround i putted these properties in the standalone.xml file of my jboss configuration like this:
<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>
In my flyway integrator i can now read these properties by:
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;
}
Maybe this helps...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With