Announcement Announcement Module
No announcement yet.
Using both Hibernate and iBatis in the same application Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using both Hibernate and iBatis in the same application

    I wish to use both Hibernate and iBatis in the same application. I want to be able to have transactions which may use Hibernate and iBatis (and straight JBDC) in a single transaction. How do I configure this?

    Right now I have a configuration that looks like this:

    	<!-- Local DataSource that works in any environment -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName"><value>$&#123;jdbc.driverClassName&#125;</value></property>
    		<property name="url"><value>$&#123;jdbc.url&#125;</value></property>
    		<property name="username"><value>$&#123;jdbc.username&#125;</value></property>
    		<property name="password"><value>$&#123;jdbc.password&#125;</value></property>
    	<!-- SqlMap setup for iBATIS Database Layer -->
    	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    		<property name="configLocation"><value>classpath&#58;/sqlMapConfig.xml</value></property>
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource"/></property>
    		<property name="mappingResources">
    		<property name="hibernateProperties">
    				<prop key="hibernate.dialect">$&#123;hibernate.dialect&#125;</prop>
    	<!-- Transaction manager for a single JDBC DataSource -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource"><ref local="dataSource"/></property>
    	<!-- Transaction manager for a single Hibernate SessionFactory -->
    	<bean id="transactionManagerHibernate" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    But this means I have two seperate transaction managers and thus can't have share a transaction for both Hibernate and iBatis/JBDC access.

  • #2
    As you see from HibernateTransactionManager you can use plain JDBC in combination with HTM in the same transaction.

    This implementation is appropriate for applications that solely use Hibernate for transactional data access, but it also supports direct data source access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services that access Hibernate (including transactional caching) and services that use plain JDBC (without being aware of Hibernate)! Application code needs to stick to the same simple Connection lookup pattern as with DataSourceTransactionManager (i.e. <code>DataSourceUtils.getConnection</code>).
    Ofcourse you have to have 2 TM, since they don't know about eachother.