Announcement Announcement Module
Collapse
No announcement yet.
Defining a Transaction for spring with hibernate and Oracle as DB Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Defining a Transaction for spring with hibernate and Oracle as DB

    Hi All
    I am using the spring hibernate template to save into DB..I have not yet used transaction...How can i define the transaction manager for hibernate in spring config and also how to use the transaction manager in the code?

  • #2
    Check the samples shipped with Spring and check the reference guide.

    General info and Hibernate info

    how to use the transaction manager in the code
    Thats the beauty of it you don't use it in the code, it is al declarative transaction management.

    Comment


    • #3
      Transaction not getting rolled back

      Hi
      my spring config file is configured as follows:

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
      <beans>
      
        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory" 
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="dataSource">
            <ref local="dataSourceDB"/>
           </property>
      		
            <!-- Hibernate mapping files -->
            <property name="mappingResources">
                <list>
      	        <value>Application.hbm.xml</value>
      	  </list>
            </property>
      		
            <property name="hibernateProperties">
              <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.connection.pool_size">10</prop>
      		 </props>
            </property>
            
         </bean>
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory">
          	<ref bean="sessionFactory"/>
          </property>
        </bean>
        
        <!-- Pass the session factory to our ApplicationDAO -->
        <bean id="applnDAO" class="com.test.HibernateImplApplicationDAO">
          <property name="sessionFactory">
          	<ref local="sessionFactory"/>
          </property>
        </bean>
        
        <!-- Pass the session factory to our ApplicationDAO -->
        <bean id="applicantDAO" class="com.test.HibernateImplApplicantDAO">
          <property name="sessionFactory">
          	<ref local="sessionFactory"/>
          </property>
        </bean>
        
        <bean name="applicationService" class="com.test.service.ApplicationService"/>
        
        <bean id="proxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      		<property name="transactionManager" ref="transactionManager"/>
      		<property name="target" ref="applicationService"/>
      		<property name="transactionAttributes">
      			<props>
      				<prop key="create*">PROPAGATION_REQUIRED</prop>
      			</props>
      		</property>
      	</bean>
      	
      </beans>
      There is one service class which has a create() method internally calling different methods of 2 different DAO classes....I want to apply the transaction manager to this service class...Create method of service class is something like this:

      Code:
      public void create()
      {
        1. //call applnDAO.method1
        2. // call applicantDAO.method2
      ]
      However i can see that if method1 succeeds and method 2 fails, still method1 data is there in the DB...Is the transaction manager not starting a transaction on applicationService.create() ?

      Comment


      • #4
        When you test your service you should use proxyBean and not applicationService, since the first one is the one which is proxied.
        A transaction will rollback only on RuntimeExceptions and Errors; checked exceptions have to be specified in transaction configuration.

        Comment


        • #5
          Or else instead of having a common data access class...I can use the individual DAOs as reqd

          DAO1.method1()
          DAO2.method2()

          Can i define a proxy to handle these type of calls?...I want something in order as follows:

          1. //Start transaction
          2. DAO1.method1()
          3. DAO2.method2()
          4. //Transaction commit if success or rollback

          I am wondering how i can define a proxy to handle the situation like this...Any idea?...If not possible via proxy, is there some other way?

          Comment


          • #6
            Andrei gave you the solution. Make sure you use the correct bean. I tend to work with anonymous inner classes to handle those situations.

            Code:
            <bean id="applicationService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            	<property name="transactionManager" ref="transactionManager"/>
            	<property name="target">
            	  <bean class="com.test.service.ApplicationService"/>
            	</property>
            	<property name="transactionAttributes">
            		<props>
            			<prop key="create*">PROPAGATION_REQUIRED</prop>
            		</props>
            	</property>
            </bean>
            Also something I notice is that your service doesn't seem to have any dependency to your configured dao's?

            Next to that it might be that the pointcut you specified isn't matching, I'm not certain if create() matches create*. Try * instead of create*.

            Comment

            Working...
            X