Announcement Announcement Module
Collapse
No announcement yet.
Hibernate4 JPA and JDBC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate4 JPA and JDBC

    We want to migrate from hibernate 3 to hibernate 4. The main reason being the envers integration.

    Part of our application performs jdbc calls via iBatis (via an external bpm product: activiti),
    the other part access the database via jpa.

    Everything worked fine with hibernate 3.

    I guess we have to change something in the configuration in order to make it work.

    This the pertinent part of the config
    Code:
      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="database" value="db2"/>
                    <property name="databasePlatform" value="ch.foo.dialect.DB2zOSDialect"/>
                </bean>
            </property>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.dialect">ch.foo.dialect.DB2zOSDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">false</prop>
                </props>
            </property>
            <property name="persistenceXmlLocation" value="classpath:META-INF/foo-persistence.xml"/>
        </bean>
    
        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/foo"/>
            <property name="resourceRef" value="true"/>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
    and the foo-persistence.xml
    Code:
     <persistence-unit name="foo" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <mapping-file>META-INF/foo-orm.xml</mapping-file>
      </persistence-unit>
    What I could observe
    Activiti checks the database by calling these 2 methods one after the other:
    Code:
      In Class org.activiti.engine.impl.db.DbSqlSession
      DatabaseMetaData databaseMetaData = connection.getMetaData();  // line 689
      tables = databaseMetaData.getTables(..)   // line 708
    The getTables returns null, hiding a NullPointerException thrown in org.hibernate.engine.jdbc.internal.proxy.Connectio nProxyHandler#getResourceRegistry.

    The connection.getMetaData() goes through org.springframework.jdbc.datasource.TransactionAwa reDataSourceProxy.TransactionAwareInvocationHandle r#invoke
    where a connection is retrieved and released.

    The connection (logicalConnection) is hold in the org.hibernate.engine.jdbc.internal.proxy.Connectio nProxyHandler.

    The getTables will try to retrieve the connection from the ConnectionProxyHandler, which will fail with a NullPointerException.

    Workaround
    I could workaround the problem by patching org.hibernate.engine.jdbc.internal.proxy.Connectio nProxyHandler#continueInvocation.
    The result of method (getMetaData()) is wrapped in the ConnectionProxyHandler. By removing the wrapping I got back the expected behavior (Well I'm not sure of that as I don't know why it was wrapped in the first place)

    Code:
       Object result = method.invoke(extractPhysicalConnection(), args);
       // result = postProcess(result, proxy, method, args); <-- patch, commented : line 139
    I can't live for long with a patched Hibernate version.
    My guess is that I have to change something in the config.
    I tried a few things, but I don't have any more ideas

    The question
    The final question how can I configure hibernate 4 so that my use case works?

    Thanks

    Spring version: 3.1.1
    Hibernate version: 4.1.4
    Tomcat 7.0.23
    Last edited by fan; Jul 13th, 2012, 01:49 AM.

  • #2
    I found it out. The problem is how the
    Code:
    org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
    class was initialised.
    The
    Code:
    setReobtainTransactionalConnections();
    method was called with value 'true'
    I just let the default value and everything works fine

    Comment

    Working...
    X