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

  • Transactions with Hibernate and JDBC

    Hi everyone,

    I have a Spring-based app which combines JDBC (via iBATIS) and Hibernate data access.
    Naturally, I need the Hibernate transactions to commit before a DAO method, which uses plain JDBC, starts. How can I do this?

    My current configuration is listed bellow.

    Thanks for tips,
    Ondra


    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
      <!-- JDBC Datasource -->
      <bean id="simpleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
      </bean>
    
      <bean id="dataSourcePool" class="org.apache.commons.pool.impl.GenericObjectPool">
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minIdle" value="5" />
        <property name="maxIdle" value="10" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
        <property name="maxActive" value="50" />
        <property name="maxWait" value="-1" />
      </bean>
    
      <bean id="connectionFactory" class="org.apache.commons.dbcp.DataSourceConnectionFactory">
        <constructor-arg><ref bean="simpleDataSource"/></constructor-arg>
      </bean>
    
      <bean id="poolableConnectionFactory" class="org.apache.commons.dbcp.PoolableConnectionFactory">
        <constructor-arg index="0"><ref bean="connectionFactory"/></constructor-arg>
        <constructor-arg index="1"><ref bean="dataSourcePool"/></constructor-arg>
        <constructor-arg index="2"><null/></constructor-arg>
        <constructor-arg index="3"><null/></constructor-arg>
        <constructor-arg index="4"><value>false</value></constructor-arg>
        <constructor-arg index="5"><value>true</value></constructor-arg>
      </bean>
    
      <bean id="pooledDataSource" class="org.apache.commons.dbcp.PoolingDataSource" depends-on="poolableConnectionFactory">
        <constructor-arg><ref bean="dataSourcePool"/></constructor-arg>
      </bean>
    
    
    
      <!-- Transaction manager for iBATIS. Not needed. iBatis has <transactionManager type="EXTERNAL"/> -->
      <!--
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="pooledDataSource"/>
      </bean>
      -->
    
      <!-- iBatis -->
      <!-- http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-ibatis  -->
      <bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="dataSource" ref="pooledDataSource"/>
        <property name="configLocation" value="ibatis-maps/SqlMapConfig2.xml"/>
      </bean>
      <alias alias="iBatis" name="sqlMap"/>
    
    
    
      <!-- Hibernate -->
    
    
      <!-- JPA - Hibernate -->
      <!-- See: http://blog.springsource.com/2006/05/30/getting-started-with-jpa-in-spring-20/ -->
      <!-- Docs: http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.html -->
      <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="ISIR_PU"/>
        <property name="dataSource" ref="pooledDataSource"/>
        <property name="jpaVendorAdapter">
          <!-- Docs: http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/orm/jpa/vendor/HibernateJpaVendorAdapter.html -->
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
             <property name="showSql" value="${hibernate.showSql}" />
             <property name="generateDdl" value="false" />
             <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
          </bean>
        </property>
      </bean>
    
      <!-- Transaction Manager for JPA. -->
      <bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"/>
        <property name="jpaDialect">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>
      </bean>
    
    
    
      <!-- Transaction by AOP -->
      <aop:config>
        <!-- DAO -->
        <aop:pointcut id="daoServiceMethods" expression="execution(* isir.dao.*.*(..))"/>
        <!-- Controllers -->
        <aop:pointcut id="controllerMethods" expression="execution(* isir.UdalostiQueueController*.*(..))"/>
        <!-- Actions -->
        <aop:pointcut id="actionMethods" expression="execution(* isir.actions.*.*(..))"/>
    
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoServiceMethods"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="controllerMethods"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="actionMethods"/>
    
      </aop:config>
    
    
      <tx:advice id="txAdvice" transaction-manager="myTxManager">
        <tx:attributes>
          <!-- all methods starting with 'get' are read-only -->
          <tx:method name="get*" read-only="true"/>
          <!-- other methods use the default transaction settings (see below) -->
          <tx:method name="*"/> <!--  propagation="REQUIRED" - default -->
        </tx:attributes>
      </tx:advice>
    
       
    </beans>

  • #2
    Well you don't need to commit you need to flush, at least if you want everything treated as a single transaction.

    Alef wrote a blog about this which you probably find interesting.

    Comment

    Working...
    X