Results 1 to 4 of 4

Thread: unclosed connection with hibernate criteria

  1. #1
    Join Date
    Aug 2004
    Posts
    19

    Default unclosed connection with hibernate criteria

    Hello, i am trying to make a simple Criteria load operation using Hibernate and Spring. But method gives a "unclosed connection" warning after the operation. Here is the code. class is extended from the HibernateDaoSupport. i use simple dbcp configuration for connections.


    Code:
       public List getMessages(int max)
        {
            try
            {
                Criteria crit = this.getSession().createCriteria(EMessage.class);
                crit.setMaxResults(max);
                List messages = crit.list();
                return messages;
            } catch (HibernateException e)
            {
                e.printStackTrace();
            }
            return null;
        }
    system actually works, and retrieves a certain amount of messages. i am actually not sure how to use the session here, or if i need to close. but no matter what i did system gave me "unclosed connection " warning.

    any help?

  2. #2
    Join Date
    Aug 2004
    Posts
    19

    Default

    i tried doing it with a Query, and same thing happened. Probably i am confusing things . by the way i use MSSQL with jtds 0.9 driver.

    Code:
            String qStr = " FROM o IN " + EncounterMessage.class;
            try
            {
                Query q = getHibernateTemplate().createQuery(this.getSession(), qStr);
                q.setMaxResults(max);
                List list = q.list();
                return list;
            } catch (HibernateException e)
            {
                e.printStackTrace();
            }
            return null;

  3. #3
    Join Date
    Aug 2004
    Posts
    19

    Default

    ok, this is strange, when i chaged the code like this, warning disappeared. i am guesing that for the operations Hibernate template does not support you have to open a new session? i think i have to check the internals of HibernateSupport class and HibernteTemplate objects to understan what is going on..

    Code:
            try
            {
                Session sess =  getHibernateTemplate().getSessionFactory().openSession();
                Criteria crit = sess.createCriteria(EncounterMessage.class);
                crit.setMaxResults(max);
                List messages = crit.list();
                sess.close();
                return messages;
            } catch (HibernateException e)
            {
                e.printStackTrace();
            }
            return null;

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    You've provided almost no details. Please provide more details. What app server are you running in? Where is the warning coming from? What is your transactional setup? (i.e. is this all in one wrapping transaction, etc.) Is your DAO inheriting from the standard HibernateDaoSupport? etc.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  2. Replies: 0
    Last Post: Apr 6th, 2005, 08:24 AM
  3. Unclosed Hibernate Connection
    By jvargas in forum Data
    Replies: 4
    Last Post: Mar 28th, 2005, 01:24 PM
  4. Replies: 1
    Last Post: Feb 10th, 2005, 09:57 AM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •