-->
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.  [ 2 posts ] 
Author Message
 Post subject: Transaction doesn't commit to MySQL DB with Tomcat
PostPosted: Mon Aug 09, 2010 10:56 pm 
Newbie

Joined: Sun Jun 06, 2010 11:00 pm
Posts: 5
Hi, I've been having a really strange problem, for some reason when I run my web application
in Tomcat and this method is executed it claims to have worked says row = 1 and consequently
prints out the message "Updated Row: 1". However, when I look at my mySQL database the attribute
has not changed its value and it is still NULL. I've manually run the method (outside of Tomcat) with a main method and it DOES update the attribute in my MySQL database. Any ideas what's going wrong? The other things is, this is not my only interaction my web-app has with the database and the other interactions work fine.


Code:
   public void attributeUpdater(User user, Map<String,String> attribMap){
      Set<String> keySet = attribMap.keySet();
      Iterator<String> it = keySet.iterator();
      String colName = "";
      String colValue = "";
      String userName = user.getLocalName();
      
      DebugOut.write("The size of attribMap is\t"+attribMap.size());
      
      while(it.hasNext()){
         colName = it.next();
         colValue = attribMap.get(colName);
         
         
         Session session = factory.currentSession();
         
         try {
            session.beginTransaction();

            String hql = "update User set "+colName+" = '"+colValue+"' where userName = '"+userName+"'";
            DebugOut.write(hql);
            Query query = session.createQuery(hql);
            int row = query.executeUpdate();
            session.getTransaction().commit();
            if (row == 0){
               DebugOut.write("DIDN'T UPDATE");
               System.out.println("Hasn't updated any row!");
            }
            else{
               DebugOut.write("SHOULD HAVE UPDATED");
               DebugOut.write("Updated Row:" + row);
               System.out.println("Updated Row: " + row);
            }
         } catch (RuntimeException e) {
            DebugOut.write("RUNTIME EXCEPTION THROWN\t"+e.getMessage());
            session.getTransaction().rollback();
            throw new HibernateException("Error loading entity. " + e.getMessage(), e);
         } catch (Exception e) {
            DebugOut.write("EXCEPTION THROWN");
            session.getTransaction().rollback();
            throw new HibernateException("Error loading entity. " + e.getMessage(), e);
         } finally {
            factory.closeSession();
         }
      }
   }


Thanks


Top
 Profile  
 
 Post subject: Re: Transaction doesn't commit to MySQL DB with Tomcat
PostPosted: Wed Aug 11, 2010 9:56 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I guess you either forgot to flush or commit the hibernate transaction.

I you indeed intend to see on the database already modified data BEFORE the hibernate transaction is commited,
then you must be aware of the transaction isolation (which is one of the ACID aspects) effects.
This means you must have been flushed the hibernate context in tomcat and
the second application must access the database with READ_UNCOMMITED isolation,
only in this way you see the changed data before it is commited.


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