Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Set JtaTransactionManager in LocalSessionFactoryBean

  1. #1
    Join Date
    Jul 2005
    Posts
    26

    Default Set JtaTransactionManager in LocalSessionFactoryBean

    The api of LocalSessionFactoryBean says the following -

    Set the JTA TransactionManager to be used for Hibernate's TransactionManagerLookup. If set, this will override corresponding settings in Hibernate properties. Allows to use a Spring-managed JTA TransactionManager for Hibernate's cache synchronization.

    Pls tell me how do i specify a spring-managed JTA TransactionManager in LocalSessionFactoryBean?

  2. #2
    Join Date
    Apr 2005
    Location
    Philippines
    Posts
    35

    Default

    maybe this example will help:

    Code:
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="mappingResources">
        <list>
          <value>inventory.hbm.xml</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
        </props>
      </property>
      <property name="dataSource">
        <ref bean="myDataSource2"/>
      </property>
    </bean>
    
    <bean id="myTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
    
    <bean id="myProductDao" class="product.ProductDaoImpl">
      <property name="sessionFactory">
        <ref bean="mySessionFactory1"/>
       </property>
    </bean>
    
    <bean id="myInventoryDao" class="product.InventoryDaoImpl">
      <property name="sessionFactory">
        <ref bean="mySessionFactory2"/>
      </property>
    </bean>
    
    <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
      <property name="productDao">
        <ref bean="myProductDao"/>
      </property>
      <property name="inventoryDao">
        <ref bean="myInventoryDao"/>
       </property>
    </bean>
    
    <bean id="myProductService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
        <ref bean="myTransactionManager"/>
      </property>
      <property name="target">
        <ref bean="myProductServiceTarget"/>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="increasePrice*">PROPAGATION_REQUIRED</prop>
          <prop key="someOtherBusinessMethod">PROPAGATION_MANDATORY</prop>
        </props>
      </property>
    </bean>

  3. #3
    Join Date
    Jul 2005
    Posts
    26

    Default

    I am asking about how to set the jtaTransactionManager property in LocalSessionFactoryBean. This property is used for setting TRANSACTION_MANAGER_STRATEGY in hibernate configuration.

  4. #4
    Join Date
    May 2005
    Location
    France: Toulouse
    Posts
    7

    Default

    when using jta, transactionManager and LocalSessionFactoryBean are disconnected.

    Why ? Cause JTA don't only manage DB Transaction.

    You must use declarative transaction with aop like this (for sample) :

    Code:
    	<bean id="myServiceTarget"
    		class="my.package.MyServiceImplementation">
    	</bean>
    	<bean id="myService"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="myJtaTransactionManager" />
    		<property name="target" ref="myServiceTarget" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="myMethodFilter*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    i hope this help

  5. #5
    Join Date
    Jul 2005
    Posts
    26

    Default

    Please go thru the api of LocalSessionFactoryBean. There is a method named setJtaTransactionManager(). This property is used by hibernate to pass transaction manager to its plugged in cache. So if i am using a combination of spring, hibernate and treecache, its important for me to set this property.

    In this property i have to pass javax.transaction.TransactionManager. I am unable to understand what to pass here.

  6. #6
    Join Date
    Apr 2005
    Location
    Philippines
    Posts
    35

    Default

    In my example above,

    I tried to illustrate how the org.springframework.transaction.jta.JtaTransaction Manager, has been used with the Hibernate.

    myProductService is using the JtaTransactionManager, and its target is myProductServiceTarget which actually used the DAO's, which also used the Hibernate Session factory(org.springframework.orm.hibernate.LocalSes sionFactoryBean)

  7. #7
    Join Date
    Apr 2005
    Location
    Philippines
    Posts
    35

    Default

    I have not tested this fully, but may this would help:

    <bean id="myTransactionManager" class="org.springframework.transaction.jta.JtaTran sactionManager"/>


    <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    ....
    ....

    <property name="jtaTransactionManager">
    <ref bean="myTransactionManager"/>
    </property>


    </bean>

  8. #8
    Join Date
    Jul 2005
    Posts
    26

    Default

    I have already tried this, it doesnt works. I get the following exception -

    org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.springframework.transaction.jta.JtaTransaction Manager] to required type [javax.transaction.TransactionManager] for property 'jtaTransactionManager'

  9. #9
    Join Date
    Apr 2005
    Location
    Philippines
    Posts
    35

    Default

    org.springframework.transaction.jta.JtaTransaction Manager is a subclass of AbstractPlatformTransactionManager which can only be used in TransactionInterceptor. I believe my first reply is the best waay to use JtaTransactionManager.

  10. #10
    Join Date
    Jul 2005
    Posts
    26

    Default

    Yes i understand that. I have configured my applicationContext in the same way you have done it. Its just that for caching, i need to set a additional property.

    When spring api provides support for the property 'jtaTransactionManager' in LocalSessionFactoryBean, then there must be a way to set it. I just want to know what to pass in there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •