Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fetch a JDBC connection in a hibernate 4.3 Integrator?

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 via ConnectionProvider, 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?

like image 736
Christian Rudolph Avatar asked Nov 01 '22 13:11

Christian Rudolph


1 Answers

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...

like image 165
Gatschet Avatar answered Nov 08 '22 05:11

Gatschet