-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate session management
PostPosted: Sat Dec 08, 2007 7:17 am 
Newbie

Joined: Sat Dec 08, 2007 6:49 am
Posts: 1
I'm using the opensession() method of session factory because i can't use getCurrentsession(i need to have few open sessions concurrently because I'm using hibernate pagination in my pages). for each transaction i'm opening and closing the session. The problem is that i have some missing data just the time i've inserted them. Actually there is inconsistency between DB and hibernate session. How can i fix this problem?
I have the regular hibernateUtil class. the getSession method is like this:

Code:
public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

      if (session == null || !session.isOpen()) {
         if (sessionFactory == null) {
            rebuildSessionFactory();
         }
         session = (sessionFactory != null) ? sessionFactory.openSession()
               : null;
         threadLocal.set(session);
      }
        return session;
    }


and here is a method for saving records:

Code:
public static boolean add(Object object, TransUser user) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

        Session session = null;
        Transaction tx = null;

        try {
            DateUtil dateUtil = DateUtil.getInstance();
            Persistent persistent = new Persistent(user.getUserId(), dateUtil.getCurrentDate("/"), dateUtil.getInstance().getCurrentTime(),
                    null, null, null, null, null, null, null, user.getWorkGroup());
            reflector.getSetterMethod(object, "persistent").invoke(object, persistent);
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            session.save(object);
            tx.commit();
            log.error("saved successfully");
            return true;
        }
        catch (HibernateException he) {
            tx.rollback();
            log.error("Could not save !" + he.getMessage());
            throw new IllegalAccessException(he.getMessage());
        }
        finally {
            HibernateUtil.closeSession();
        }
    }


i also use the following methods when i want to insert or update different entities with the same transaction:

Code:
/**
     * @return :Session
     */
    public static Session beginTransaction() {
        Session session = null;
        Transaction tx = null;
        session = HibernateUtil.getSession();
        tx = session.beginTransaction();
        return session;
    }

    /**
     * @param session :Session
     * @return :boolean
     */
    public static boolean endTransaction(Session session) throws IllegalAccessException {
        Transaction tx = session.getTransaction();
        try {
            tx.commit();
            log.error("commited successfully");
            return true;
        }
        catch (HibernateException he) {
            tx.rollback();
            log.error("Could not commit !" + he.getMessage());
            throw new IllegalAccessException(he.getMessage());
        }
        finally {
            HibernateUtil.closeSession();
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.