Hibernate Integration

With Spring

The advantage is that you don't need any JNDI server. The following XML snippet illustrates how to configure Atomikos TransactionsEssentials 3.1 or higher with Spring and Hibernate.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <!-- configure an Atomikos JTA-aware datasource -->
   <bean id="datasource"
      class="com.atomikos.jdbc.SimpleDataSourceBean"
      init-method="init" destroy-method="close">
       <!-- set an arbitrary but unique name for the datasource -->
      <property name="uniqueResourceName"><value>XADBMS</value></property>
       <!-- set the underlying driver class to use, in this example case we use Oracle -->
      <property name="xaDataSourceClassName"><value>oracle.jdbc.xa.client.OracleXADataSource</value></property>
      <property name="xaDataSourceProperties">
          <!--
             set the driver-specific XADataSource properties (check your driver docs for more info)
          -->
         <value>user=scott;password=tiger;URL=...</value>
      </property>   
       <!-- exclusive mode determines when connections are reused; safest choice is true -->
      <property name="exclusiveConnectionMode"><value>true</value></property>
       <!-- how many connections in the pool? -->
      <property name="connectionPoolSize" value="3"/>
   </bean>
   

    <!-- configure Hibernate to use the Atomikos JTA and datasource for transaction control -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <!-- NOTE: for hibernate 2, use the following class: org.springframework.orm.hibernate.LocalSessionFactoryBean -->

       <!-- add the mapped resources (hbm files) of your application -->
      <property name="mappingResources">
         <list>
            <value>...</value>
         </list>
      </property>

       <!-- make sure that hibernate uses the Atomikos datasource (JTA enabled)! -->
      <property name="dataSource"><ref bean="datasource"/></property>
   
       <!-- configure hibernate to use Atomikos TransactionsEssentials -->
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
            <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
             <!-- for hibernate 2, use the following:
             <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate.TransactionManagerLookup</prop>
             -->
             <!-- add any other hibernate properties you need -->
         </props>
      </property>

   </bean>

    <!-- configure the Spring hibernate template with the session factory from above -->
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory"><ref bean="sessionFactory"/></property>
   </bean>
   
</beans> 

Without Spring

See HibernateThreeStandalone for an example application that integrates with Hibernate (without Spring). This approach requires a JNDI server.

Hibernate Connection Release Mode Settings

Hibernate 3 (or higher) has a configuration parameter called hibernate.connection.release_mode. The value of this parameter may affect the performance and the number of connections gotten from the pool, depending on the exclusiveConnectionMode parameter configured on the Atomikos DataSource implementation:

hibernate.connection.release_mode value Atomikos exclusiveConnectionMode value Atomikos pool behaviour
after_statement (default for JTA) true (the default value) uses a different connection for each statement within one transaction (no connection reuse)
after_statement (default for JTA) false (requires special DBMS support) connections are recycled per statement
on_close any Hibernate (re)uses the same connection throughout the Hibernate session
after_transaction any Hibernate (re)uses the same connection throughout the transaction NOTE: this setting seems to be ignored by Hibernate

Also see these related Hibernate Docsexternal

-- GuyPardon - 26 Jul 2007

Go
r8 - 29 Oct 2008 - 15:14:24 - GuyPardon
This site is powered by the TWiki collaboration platformCopyright © contributing authors and Atomikos. All material on this collaboration platform is the shared property of the contributing authors and Atomikos.
Syndicate this site RSSATOM