How to get JDBC connection in Hibernate 4.3 Integrator?
When implemented org.hibernate.integrator.spi.Integrator
for hibernate 4.3, you get an object Configuration
, SessionFactoryImplementor
and SessionFactoryServiceRegistry
.
One way to get the metadata is to get the connection provider:
sessionFactory.getJdbcServices().getConnectionProvider()
But getConnectionProvider()
outdated and doesn't work for setting up multiple tenants.
Javadocs say
Accessing connections through
org.hibernate.engine.jdbc.spi.JdbcConnectionAccess
should be preferred when accessing throughConnectionProvider
whenever possible.
But my problem is that I haven't found a way to get JdbcConnectionAccess
. It would be possible to use this SessionFactoryServiceRegistry
and replicate the code from SessionFactoryImpl#buildLocalConnectionAccess()
, but this is not a good solution.
What is the recommended way to get a compound in Integrator
?
source to share
I had a similar problem with the deprecated method "sessionFactory.getJdbcServices (). GetConnectionProvider ()". I used this function to get the dataSource for my FlywayIntegrator. I used them as in this example: http://blog.essential-bytes.de/flyway-hibernate-und-jpa-integrieren
Without getConnectionProvider (), I haven't found a solution to get the required properties to connect to my database.
As a workaround, I put these properties in the standalone.xml file of my jboss config 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:
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 it helps ...
source to share
You can do the following from your Integrator implementation:
SessionImplementor sessionImplementor = (SessionImplementor) sessionFactory.getCurrentSession();
Connection conn = sessionImplementor.getJdbcConnectionAccess().obtainConnection();
SessionFactory is a SessionFactoryImplementor that you get from implemented methods.
source to share