-->
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: yet another session and transaction problem
PostPosted: Thu Mar 27, 2008 1:56 pm 
Newbie

Joined: Thu Mar 27, 2008 1:30 pm
Posts: 6
I did read http://hibernate.org/42.html

But I still have questions...

I created the servlet filter to open a session when I receive a webservice request. This filter is opening and closing the session. I don't get Lazy initialization problem anymore.

To close the session I'm calling the disconnect method instead of close because if I call close method I get an exception that is kind of the same problem as the lazy initialization since the session is closed.

Code:
   
    public void commit(Object transaction) {
        Transaction t = (Transaction) transaction;
        commit(t);
    }
   
    public void commit(Transaction transaction) {
        try {
            if (transaction != null && !transaction.wasCommitted()
            && !transaction.wasRolledBack()) {
                transaction.commit();
                transaction = null;
            }
           
        } catch (Exception e) {
            if (transaction != null) {
                try {
                    rollback(transaction);
                } catch (HibernateException he) {
                   
                }
            }
            e.printStackTrace();
        }
    }

    public Object start(Object session) {
        Session s = (Session) session;
        return startTrans(s);
    }
   
    public Transaction startTrans(Session session) {
        return session.beginTransaction();
    }

    public Session openSession() {
        return sessionFactory.openSession();
        //return sessionFactory.getCurrentSession();
    }
   

    public void close(Session session) {
        if (session != null) {
            try {
                session.disconnect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public boolean find(EntityObject entityObj){
        boolean achou = false;
        Session session = openSession(entityObj);
        Transaction transaction = startTrans(session);
        try {
            EntityId id = new EntityId(entityObj);
            Serializable idObj=id.getKey();
            session.load(entityObj, idObj);
            commit(transaction);
            achou = true;
        } catch (Exception ex) {
            //System.out.println(ex.getMessage());
            ex.printStackTrace();
            rollback(transaction);
        } finally {
            close(session);
        }
        return achou;
    }


This code is not closing the connections to the database, the pool size is 5 and after few minutes of webservice calls I get about 80 connections. The DriverManagerConnectionProvider show the following information:

14:48:46,437 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 77
14:48:46,437 DEBUG DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
14:48:46,515 DEBUG DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1

Basically I want to know when I should close the session. Should I really close it all? And why the connections are not being closed?

EDIT: If i use getCurrentSession() from the SessionFactory will it be the same session I opened on my servletfilter?

EDIT2: I'm calling session.disconnect() (returning null) but session.isConnected() is still returning true


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.