Results 1 to 7 of 7

Thread: Roll back in hibernate

  1. #1

    Default 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. #2
    Join Date
    Jun 2005
    Posts
    4,241

    Default

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

  3. #3

    Default

    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?

  4. #4
    Join Date
    Jun 2005
    Posts
    4,241

    Default

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

  5. #5

    Default

    yes, i have only one data source

  6. #6
    Join Date
    Jun 2005
    Posts
    4,241

    Default

    Still not enough information. How is myWriter implemented?

  7. #7
    Join Date
    Oct 2008
    Posts
    7

    Default

    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.

Posting Permissions

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