-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: hibernateTemplate correct way to rollback
PostPosted: Mon Jun 24, 2013 8:38 am 
Newbie

Joined: Thu May 30, 2013 9:55 am
Posts: 4
Hi

I have to save two beans in two tables , but in case of exception during saving second I need to rollback first .
I tried to use such approaches :
1. org.springframework.transaction.annotation.Transactional annotation :

Code:
    @Override
    @Transactional
    public void saveOrUpdate(ElementData elementData)
    {
   Element element = elementData.getElement();
   if (element != null)
   {
           hibernateTemplate.saveOrUpdate(element);
      hibernateTemplate.saveOrUpdate(elementData);
   } else
   {
       hibernateTemplate.saveOrUpdate(elementData);
   }
    }

.............
}


2. directly using SessionFactory :

Code:
@Override
    public void saveOrUpdate(ElementData elementData)
    {
   Element element = elementData.getElement();
   if (element != null)
   {
       Session currentSession = sessionFactory.openSession();
       currentSession.beginTransaction();
       try
       {
      currentSession.saveOrUpdate(element);
      currentSession.saveOrUpdate(elementData);
       } catch (Exception e)
       {
      currentSession.getTransaction().rollback();
      throw e;
       }
       currentSession.getTransaction().commit();
   } else
   {
       hibernateTemplate.saveOrUpdate(elementData);
   }
    }


3. cascade=CascadeType.ALL in ElementData bean which holds Element :

Code:
@Entity
public class ElementData implements Serializable
{

    public static final String DIRECT_JS_CALL = "DIRECT_JS_CALL";

    private static final long serialVersionUID = 6730581106117198964L;

    @Id
    @GeneratedValue
    private Integer id;

    @Column(name = "locatorType", unique = false, nullable = false)
    @Enumerated(EnumType.ORDINAL)
    private LocatorType locatorType;

    @Column(name = "locator", unique = false, nullable = false)
    private String locator;

    @Column(name = "frame", unique = false, nullable = true)
    private String frame;

    @Column(name = "language", unique = false, nullable = false)
    @Enumerated(EnumType.ORDINAL)
    private Language language;

    @ManyToOne(cascade =
    { CascadeType.ALL })
    @JoinColumn(name = "elementId", unique = false, nullable = false)
    private Element element;

    @ManyToOne
    @JoinColumn(name = "updatedBy", unique = false, nullable = false)
    private User updatedBy;

    @Column(name = "updatedDate", unique = false, nullable = true, columnDefinition = "timestamp")
    private String updatedDate;

    @ManyToOne
    @JoinColumn(name = "releaseId", unique = false, nullable = false)
    private ReleaseEntity releaseEntity;
......
}


In all cases Element table persist Element entity no matter if exception happens during saving ElementData .
Please help me to rollback Element in case of exception in saving ElementData


Top
 Profile  
 
 Post subject: Re: hibernateTemplate correct way to rollback
PostPosted: Mon Jun 24, 2013 2:27 pm 
Newbie

Joined: Sat Sep 10, 2011 7:43 pm
Posts: 3
@Transactional should be sufficient
then throw an exception and it should roll back
The reason it may not be working may be because you don't have a transaction manager configured
in the absence of a transaction manager it will do auto commit
no matter what you do


Top
 Profile  
 
 Post subject: Re: hibernateTemplate correct way to rollback
PostPosted: Mon Jun 24, 2013 3:04 pm 
Newbie

Joined: Thu May 30, 2013 9:55 am
Posts: 4
volatile32 wrote:
@Transactional should be sufficient
then throw an exception and it should roll back
The reason it may not be working may be because you don't have a transaction manager configured
in the absence of a transaction manager it will do auto commit
no matter what you do


Actually I have transactionManager configured :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   <context:annotation-config />
   <context:component-scan base-package="com.livenation.automation.dao" />
   <tx:annotation-driven />

   <bean
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location">
         <value>database.properties</value>
      </property>
   </bean>

   <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="url" value="${db.url}" />
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="username" value="${db.username}" />
      <property name="password" value="${db.password}" />
   </bean>

   <bean id="mySessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="myDataSource" />
      <property name="annotatedClasses">
         <list>
            <value>com.livenation.automation.bean.Domain</value>
            <value>com.livenation.automation.bean.Element</value>
            <value>com.livenation.automation.bean.ElementData</value>
            <value>com.livenation.automation.bean.Step</value>
            <value>com.livenation.automation.bean.TestCase</value>
            <value>com.livenation.automation.bean.TestSuite</value>
            <value>com.livenation.automation.bean.User</value>
            <value>com.livenation.automation.bean.ElementDataHistory</value>
            <value>com.livenation.automation.bean.DomainSpecificTestcase</value>
            <value>com.livenation.automation.bean.DomainLanguage</value>
            <value>com.livenation.automation.bean.ReleaseEntity</value>
            <value>com.livenation.automation.bean.RunEmail</value>
            <value>com.livenation.automation.bean.RunEntity</value>
            <value>com.livenation.automation.bean.StepHistory</value>
            <value>com.livenation.automation.bean.DataPool</value>
            <value>com.livenation.automation.bean.SchedulingSettings</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
         </props>
      </property>
   </bean>

   <bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="dataSource" ref="myDataSource" />
      <property name="sessionFactory" ref="mySessionFactory" />
   </bean>

</beans>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.