-->
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.  [ 4 posts ] 
Author Message
 Post subject: transaction commit and rollback
PostPosted: Tue Mar 01, 2005 5:15 pm 
Newbie

Joined: Thu Feb 24, 2005 5:21 pm
Posts: 5
Hibernate version:
2.1

Mapping documents:
<class name="ncr.dpc.domain.Promotion" table="PROMOTIONS">
<id name="id" column="promotionId" type="integer" unsaved-value="null">
<generator class="native"/>
</id>
<property name="code" type="integer"/>
<property name="customerEmployeeDiac" type="boolean"/>
<property name="customerNotEmployeeDiac" type="boolean"/>
<property name="customerRecordDiac" type="boolean"/>
<property name="description" type="string"/>
<property name="directed" type="boolean"/>
<property name="instantWin" type="boolean"/>
<property name="instantWinEvaluationMode" type="integer"/>
<property name="instantWinPrizeOddsOfWinning" type="integer"/>
<property name="instantWinPrizeQuantity" type="integer"/>
<property name="instantWinOddsCalculation" type="integer"/>
<property name="endDate" type="date"/>
<property name="endTime" type="string"/>
<property name="offlineDiac" type="integer"/>
<property name="priority" type="integer"/>
<property name="rewardLimitation" type="integer"/>
<property name="rewardLimitationTimes" type="integer"/>
<property name="rewardLimitationTransaction" type="integer"/>
<property name="startDate" type="date"/>
<property name="startTime" type="string"/>
<property name="state" type="integer"/>
<many-to-one name="promotionVariable" column="promotionVariableId" class="ncr.dpc.domain.PromotionVariable"/>
<many-to-one name="originalPromotion" class="ncr.dpc.domain.Promotion" column="originalPromotionId" unique="true"/>
<many-to-one name="category" class="ncr.dpc.domain.PromotionCategory" column="promotionCategoryId"/>
<list name="conditions" cascade="all-delete-orphan" lazy="true">
<key column="promotionId"/>
<index column="position"/>
<one-to-many class="ncr.dpc.domain.condition.Condition"/>
</list>

</class>



Code between sessionFactory.openSession() and session.close():
Session session = getSession();
session.setFlushMode(FlushMode.COMMIT);
Transaction transaction = null ;
boolean saved = (promotion.getId()==null)?(true):(false);
try {
transaction = session.beginTransaction();
// Save or update promotion
session.saveOrUpdate(promotion);
// If the promotion was saved
if (saved) {
// Update code
promotion.setCode(promotion.getId());
session.update(promotion);
}
// Commit
transaction.commit();
} catch (HibernateException e) {
rollbackTransaction(transaction);
// TODO Log
throw new NCRHomeException(e);
} finally {
closeSession(session);
}


Name and version of the database you are using:
MySql 4.1.8

The generated SQL (show_sql=true):
[3/1/05 17:31:20:120 ART] 450c450c SystemOut O Hibernate: insert into RECORDS (itemElegibility, promotionVariable) values (?, ?)
[3/1/05 17:31:20:126 ART] 450c450c SystemOut O Hibernate: insert into CONDITIONS (testValue, frecuentCustomerExclusion, rewardGrant, endTime, startTime, days, hostValue, bits, ConditionTypeId, condition_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


When I try to commit the transaction, an exception is raised. That's not the problem. The problem is that when the line session.saveOrUpdate() is executed, the new objects added to the conditions collection is inserted in the db. The point is that an insert is executed even when the transaction is rolled back and it was not commited.

The code calls 3 methods to get a session, rollback a transaction and to close a session. They call the respective methods in the session and transaction objects.

Thanks, martin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 8:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
MySQL has both Transaction and non-transactional tables. Use Innob (sp?) tables that have transactional support.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 10:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Also:
Quote:
when the line session.saveOrUpdate() is executed, the new objects added to the conditions collection is inserted in the db


Not true. When you call saveOrUpdate(), that instance and its associations are associated to that Hibernate session. No DML operations happen at this juncture. When you call commit(), an implicit "flush" occurs. Flush is the act during which Hibernate synchronizes in-memory state of entities associated to the session being flushed with the database.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 10:28 am 
Newbie

Joined: Thu Feb 24, 2005 5:21 pm
Posts: 5
david wrote:
MySQL has both Transaction and non-transactional tables. Use Innob (sp?) tables that have transactional support.


Thanks david. It solved my problem. Now the transactions are rollbacked.


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