-->
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: transaction.commit() doesn't seem to work as intended
PostPosted: Sun Sep 03, 2006 1:28 pm 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
I have the following code for saving a new record (of users) to my MySQL 5.0.22 database:

Code:
User user = getuser();
Session session = null;
Transaction transaction = null;
String strUserId = null;

try {
   session = HibernateSessionFactory.getSessionFactory().openSession();
   transaction = session.beginTransaction();
   strUserId = session.save(user).toString();

   // logging ; if an exception happens here, a row in db is still created :(
   user.setId(Integer.parseInt(strUserId));
   handleLogs.setUsersLog(user, createdById);

   // Commit when logging is complete
   transaction.commit();
} catch (Exception e) {
   if (transaction != null) {
      try {
         transaction.rollback();
      } catch (HibernateException he) {
         // log
      }
   }
   throw new NeSystemException(e);
} finally {
   if (session != null) {
      try {
         session.close();
      } catch (HibernateException he) {
         // log
      }
   }
}


The problem is that if an exception occurs in the logging part, a new row is saved to the database even though transaction.commit() is never executed. I though the new row weren't supposed to be written to the database before after the commit? What's wrong with my code?


Top
 Profile  
 
 Post subject: Innodb or MyISAM?
PostPosted: Tue Sep 05, 2006 9:16 am 
Regular
Regular

Joined: Wed Aug 24, 2005 11:49 am
Posts: 63
Are you ising Innodb?
MyISAM doesn't support transactions.


Top
 Profile  
 
 Post subject: Re: Innodb or MyISAM?
PostPosted: Tue Sep 05, 2006 9:20 am 
Newbie

Joined: Mon Mar 27, 2006 7:16 am
Posts: 8
evdelst wrote:
Are you ising Innodb?
MyISAM doesn't support transactions.


ahhh... no I'm using MyISAM. Thanks for the tip, I didn't think of that :). I'll try it out and see if it works.


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.