Announcement Announcement Module
Collapse
No announcement yet.
Roll back in hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Roll back in hibernate

    My reader class pass a list of objects into my writer class. My writer class insert/update many records in db. Suppose if i am throwing a business exception thrown for third record , i want to rollback prevoius two records inserted. This is not happening for me and the job.xml is given below:

    Code:
    <bean id="test" parent="skipLimitStep">
        <property name="streams">
             <list>
                     <ref bean="headerReader" />
              </list>
        </property>
        <property name="commitInterval" value="5" />
        <property name="skipLimit" value="20"/>                                     
        <property name="itemReader">
                       <bean class="com.MyReader">
                             <property name="itemReader" ref="headerReader" />
                             <property name="manager" ref="manager" />
                        </bean>
        </property>
        <property name="itemWriter">
                         <bean class="org.springframework.batch.item.database.HibernateAwareItemWriter">
    		  <property name="sessionFactory" ref="sessionFactory" />
    		   <property name="delegate" ref="myWriter" />
    	         </bean>
         </property>
    </bean>
    
    
    <bean id="headerReader"        class="org.springframework.batch.item.database.HibernateCursorItemReader">
            <property name="queryString">
                <value>${SOME_QUERY}</value>
            </property>
            <property name="sessionFactory" ref="sessionFactory" />
            <property name="saveState" value="false" />
    </bean>
    
    
    <bean id="myWriter"
            class="com.MyWriter">
            <property name="manager" ref="manager" />
    </bean>
    Here com.MyReader extends DelegatingItemReader and com.MyWriter implements ItemWriter, ItemStream.

    I tried by adding transaction attribute also.
    Should i define any transaction boundary using AOP ?
    I am using the same simple-job-launcher-context.xml with oracle as db.

    Kindly please tell me how can i achieve rollback here?

  • #2
    Is your SessionFactory connected to a the transaction manager for the step, and is that a HibernateTransactionManager?

    Comment


    • #3
      I'm using both JDBC(for reading reference data) and Hibernate(for reading input and writing output) in my Job. I'm using simpleStep and its configured like this:

      Code:
      <bean id="simpleStep"
      		class="org.springframework.batch.core.step.item.SimpleStepFactoryBean"
      		abstract="true">
      		<property name="transactionManager" ref="transactionManager" />
      		<property name="jobRepository" ref="jobRepository" />
      		<property name="startLimit" value="100" />
      		<property name="commitInterval" value="1" />
      	</bean>
      
      <bean id="transactionManager"
      		class="org.springframework.orm.hibernate3.HibernateTransactionManager"
      		lazy-init="true">
      		<property name="sessionFactory" ref="sessionFactory" />
      	</bean>
      Please let me know if this configuration is correct?

      Comment


      • #4
        So far so good. And there is only one DataSource?

        Comment


        • #5
          yes, i have only one data source

          Comment


          • #6
            Still not enough information. How is myWriter implemented?

            Comment


            • #7
              Maybe the problem is the commitInterval
              if you specify:
              <property name="commitInterval" value="1" />
              then Spring Batch will do a commit for each line you read.
              Try changing this value to a value greatter than 3 (the line you do the rollback) and see what happens.

              Comment

              Working...
              X