-->
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: How do I restrict Hibernate from issuing auto commit?
PostPosted: Mon Feb 13, 2006 3:34 pm 
Beginner
Beginner

Joined: Tue Jun 21, 2005 1:45 pm
Posts: 38
Below are code snippets for HibernateUtil and HiberateProcessor that I'm using.

My problem is hibernate issues update statements even though I dont do a transaction commit. I saw this happening when

- A session is first opened and retrieves records list from table A
- Loops thro the list and calls getters on value objects in that list.
- Then without closing the session, a query is made to table B

At this point of time, hibernate issues update statement on all the rows retrieved from table A.

I'm using <sql-query/> to execute a SQL stmt. Plz let me know is there a property in hibernate config which can be used to restrict Hibernate from doing issuing update statements?


Code:
HibernateUtil -

    public static Session currentSession(SessionFactory sessionFactory)
    {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null)
        {
            s = sessionFactory.openSession();
            session.set(s);
        }
        else
        {
           if (!s.isConnected())
           {
              closeSession();
              s = sessionFactory.openSession();
               session.set(s);
           }
        }
        if(!s.isConnected())
           s.reconnect();
       
        return s;
    }



HibernateProcessor -

    public List select(String id, Map data)
    {
        Session session = HibernateUtil.currentSession(sessionFactory);
        Transaction tx = session.beginTransaction();
       
        List results = null;
       
        try
        {
           Query qry = session.getNamedQuery(id);
          
           String params[] = qry.getNamedParameters();
           if(params!=null)
           {
               for(int i=0; i<params.length; i++)
               {
                   Object paramVal = data.get(params[i]);
                   qry.setParameter(params[i], paramVal);
               }
           }
           results = qry.list();
        }catch(Exception e)
        {
           if(e instanceof JDBCConnectionException)
         {
            boolean flag = handleJDBCConnectionException((JDBCConnectionException) e, session, data);
            if(flag)
               return null;
            select(id, data);
         }
           else
              logger.error(ExceptionUtils.getFullStackTrace(e));
        }finally
        {
            try
            {
               if(tx!=null)
                  if(tx.isActive())
                     tx.rollback();
            }catch(Exception e)
            {}
        }
       
        return results;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 3:58 pm 
Newbie

Joined: Wed Oct 12, 2005 7:06 pm
Posts: 10
It could be bacause of datatype mismatch. I had this problem before. I had an attribute in Class A defined as 'short' but was reading it as Integer.

So hibernate was doing a dirty check and thought that the values have been changed after it was loaded and hence flushing it back to the database.

Hope this helps

Lachu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 4:38 pm 
Beginner
Beginner

Joined: Tue Jun 21, 2005 1:45 pm
Posts: 38
r_lachu wrote:
It could be bacause of datatype mismatch. I had this problem before. I had an attribute in Class A defined as 'short' but was reading it as Integer.

So hibernate was doing a dirty check and thought that the values have been changed after it was loaded and hence flushing it back to the database.

Hope this helps

Lachu


Lachu, thanks for the response.

You could be certainly right that Hibernate might be issuing update stmts due to datatype mismatch between value object and hibernate config/database table. I keep Integer in the value object even for the columns which are of type 'short' in the database column. However is there a way to restrict this auto flush back to the database?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 4:48 pm 
Newbie

Joined: Wed Oct 12, 2005 7:06 pm
Posts: 10
you can call

session.setFlushMode(FlushMode.NEVER)

Lachu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 5:09 pm 
Beginner
Beginner

Joined: Tue Jun 21, 2005 1:45 pm
Posts: 38
r_lachu wrote:
you can call

session.setFlushMode(FlushMode.NEVER)

Lachu


Thanks Lachu !!!!

You just got a credit for yourself!!


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.