Monday, May 14, 2012

what is current_session_context_class in hibernate configuration xml

meaning and need for the current_session_context_class


When you create a hibernate session using any of the sessionFactory.openSession(...) methods the session factory will 'bind' the session to the current context. The default context is 'thread' which means the sesion factory will bind the session to the thread from which openSession(...) is called. 

This is useful because you can later call sessionFactory.getCurrentSession() which will return the session that is bound to the currently running thread. 

You can use other predefined current_session_context_class values such as 'jta' which will bind the session to the currently running JTA transaction (if you're running in an application server that supports JTA this is really useful). Or you can write your own implementation of org.hibernate.context.CurrentSessionContext and use that implementation to manage the current session context (not really advisable unless you absolutely need to). 

52 comments:

  1. Why
    Session session1 = sf.openSession();
    Session session2 = sf.getCurrentSession();
    System.out.println(session1 == session2);
    print false

    ReplyDelete
    Replies
    1. openSession() will return a new session object on every call, which is actually a instance of org.hibernate.impl.SessionImpl.We can use this method when we decided to manage the Session our self.
      It does not try to store or pull the session from the current context.
      If we use this method, we need to flush() and close() the session. It does not flush and close() automatically.

      getCurrentSession():


      Session getCurrentSession() throws HibernateException

      Obtains the current session. The "current session" refers to a Hibernate Session bound by Hibernate behind the scenes, to the transaction scope.

      A session is opened whenever getCurrentSession() is called for the first time and closed when the transaction ends. This creates a brand new session if one does not exist or uses an existing one if one already exists. It automatically configured with both auto-flush and auto-close attributes as true means Session will be automatically flushed and closed.

      Delete
  2. are you sure you haven't copied word to word description form
    https://forums.hibernate.org/viewtopic.php?p=2384980

    ReplyDelete
    Replies
    1. Seems like it ... unless its the same person.

      Delete
    2. this guy is Ramesh. I doubt this guy will go with the name Neil, who has posted it originally in jboss community

      Delete
    3. Yes its an exact copy of that, a kind of 100% plagiarism...

      Delete
    4. <script>alert("Is this site vulnerable to XSS attack ?");</script>

      Delete
    5. I'm not sure. %253Cscript%253Ealert('ifItWorks')%253C%252Fscript%253E

      Delete
  3. Could you please accept/deny the plagiarism with reason ? Or at least show some courtesy to the original owner ?
    Or these are too much to expect from a guy like you ?

    ReplyDelete
  4. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  5. We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete
  6. Thanks for sharing this article .
    Java Course in Solapur

    ReplyDelete
  7. infomative blog, keep posting . If you want to learn about java web developer then checkout java course in pune

    ReplyDelete
  8. 86F93
    ----
    ----
    matadorbet
    ----
    ----
    ----
    ----
    ----
    ----

    ReplyDelete