-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem in Hibernate Transaction Management.
PostPosted: Fri Feb 26, 2010 2:13 am 
Newbie

Joined: Tue Oct 25, 2005 1:40 am
Posts: 7
I am facing some problem in transaction commit/rollback.

The situation is I want to update the record before deleting it. So if either of update or delete fails the current transaction should be rolled back, else it should be commited.
In my DAO Impl class delete method I have written following code.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().begin();
// update Record
getHibernateTemplate().update(DTOObject);
// delete Record
getHibernateTemplate().delete(DTOObject);
getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().commit();
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

But after I call commit on transaction in only calls delete query on database. Not the update one.

I tried with declarative transaction management also, by configuring Transaction attributes in config files as follows:-

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocations">
<list>
<value>classpath*:hibernate.cfg.xml</value>
</list>
</property>
</bean>

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

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" rollback-for="org.hibernate.exception.ConstraintViolationException" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="fooServiceOperation"
expression="execution(* x.y.classname.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
</aop:config>

And for declarative transaction management, in my DAO Impl class I am writing update & delete in single method as :-
+++++++++++++++++++++++++++++++++++++++++++
getHibernateTemplate().update(DTOObject);
getHibernateTemplate().delete(DTOObject);
++++++++++++++++++++++++++++++++++++++++++++

But Still its only calling delete query on database, not the update one.

Could you please let me know how can I achieve this - it should call both queries on DB or it should call neither.

Many thanks in advance.

-Pras


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate Transaction Management.
PostPosted: Fri Feb 26, 2010 3:09 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
With a explicite flush() between update and delete you force hibernate to do the update.
(I assume that without flush hibernate swallows the update action as the entity will be deleted anyway)

Code:
getHibernateTemplate().getSessionFactory().getCurrentSession().getTransaction().begin();
// update Record
getHibernateTemplate().update(DTOObject);
getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
// delete Record
getHibernateTemplate().delete(DTOObject);


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate Transaction Management.
PostPosted: Fri Feb 26, 2010 3:24 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Hi,

If you are using spring to manage the transaction, don't call begin or commit transaction manually.
What's your purpose of calling update before deleting the entity?
Try to call
Code:
getHibernateTemplate().flush();

before calling delete


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate Transaction Management.
PostPosted: Fri Feb 26, 2010 4:06 am 
Newbie

Joined: Tue Oct 25, 2005 1:40 am
Posts: 7
So if I call session.flush() it will force hibernate to call update query before delete.

But what about rollback? If delete fails then will it rollback update also?

Also my purpose for calling update before delete is, it is required for audit purposes so I am updating field needed in audit for last update/delete operation.


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate Transaction Management.
PostPosted: Fri Feb 26, 2010 4:36 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
If delete fails then will it rollback update also?


YES, the rollback rollbacks the whole actions you made since beginning the transaction (beginTransaction)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.