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: Using hibernate Session from a java Thread
PostPosted: Wed Jul 18, 2007 10:17 am 
Newbie

Joined: Wed Jul 18, 2007 9:39 am
Posts: 1
Location: Hyderabad, India
Hibernate version: 3.2.4

Mapping documents:
Code:
<class name="Documents" table="DOCUMENTS_TABLE" >
     <id name="docId" column="DOC_ID"/>
     <property name="refNo" column="REF_NO"/>
     <property name="document" column="DOCUMENT_BLOB" type="blob"/>
  </class>

Code between sessionFactory.openSession() and session.close():

Code:
Query query = session.createQuery("from Documents where refNo = '222'");
List appRefNoList = query.list();


Full stack trace of any exception that occurs:none

Name and version of the database you are using: Oracle 9i

The generated SQL (show_sql=true): none

Debug level Hibernate log excerpt: none


Can anyone help me in using a hibernate session from a class that extends java.lang.Thread. My class is a daemon thread that runs continuously. It makes use of hibernate session at regular intervals.
I had used the following code to get Hibernate Session,
but it didn't work in the thread. It works fine when I run it from a
non-thread java class.

Code:
public class HibernateSession {

   public static final ThreadLocal session = new ThreadLocal();
   
    public static Session currentSession() throws HibernateException {

      Session s = (Session) session.get();
      if (s == null) {
         SessionFactory sf = HibernateUtil.getSessionFactory();
         s = sf.openSession();
         session.set(s);
      }
      return s;
    }

    public static void closeSession() throws HibernateException {
       Session s = (Session) session.get();
       session.set(null);
       if (s != null) s.close();
    }
}



Thanks in advance.

Regards,
Krishna Mohan.


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.