Announcement Announcement Module
Collapse
No announcement yet.
unclosed connection with hibernate criteria Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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;

    Comment


    • #3
      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;

      Comment


      • #4
        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.

        Comment

        Working...
        X