Why do I get org.hibernate.HibernateException: No CurrentSessionContext configured

前端 未结 3 1707
误落风尘
误落风尘 2020-12-15 04:56

I\'m writing a simple project, a business app written in Swing, using Hibernate for back-end. I come from Spring, that gave me easy ways to use hibernate and transactions. A

相关标签:
3条回答
  • 2020-12-15 05:48

    No CurrentSessionContext configured

    Read the reference guide on Contextual Sessions. You're required to configure some provided or custom strategy for this. In a hibernate.cfg.xml, you'd configure it with

    <property name="hibernate.current_session_context_class">...</property>
    

    You'd probably want to use "thread" as the value to get per-thread sessions. When using Spring, it automatically sets this to a SpringSessionContext, allowing Spring to easily integrate Hibernate with its transaction management framework.

    I come from Spring, that gave me easy ways to use hibernate and transactions.

    If you're familiar with Spring, why aren't you using it to manage Hibernate here? You must already know how simple and foolproof it makes it.

    I never close a hibernate session in my code, just on application closing. Is this wrong?

    Yes, this is very wrong. Every session not closed is an open database connection, so your app is currently hemorrhaging connections.

    Illegal attempt to associate a collection with two open sessions

    That means exactly what it says. You tried to do some persistence operation (save(), update(), delete()) on something that was already associated to a different session. That's what will happen when you go randomly opening new sessions whenever, which is what's happening since SessionFactory.getCurrentSession() will always fail when no "current session context" is set. In general, never open a session just because one wasn't already there. You need to have well-defined strategies for opening and closing sessions and never let anything open a session outside of these "strategies". That's a sure path to resource leaks and errors like the one you've encountered.

    0 讨论(0)
  • 2020-12-15 05:52

    I wanted to ask you one thing, why are you trying to use "OpenSession" method?

    public static Session getSession() throws HibernateException {         
       Session sess = null;       
       try {         
           sess = sessionFactory.getCurrentSession();  
       } catch (org.hibernate.HibernateException he) {  
           sess = sessionFactory.openSession();     
       }             
       return sess;
    } 
    

    You don't have to call openSession(), because getCurrentSession() method is always returns current session (Thread in case if you have configured it to be).

    I got it!... You have to specify current context in your hibernate.cfg.xml file

    it should be:

    <property name="hibernate.current_session_context_class">thread</property>
    
    0 讨论(0)
  • 2020-12-15 05:52

    I faced the same problem when I am working on a portal where I am using spring remoting with hibernate. This kind of problem arise only if when the called service method contains multiple DAO calls that hit database with hibernate session.

    And the solution is set the @Transaction annotation for those methods with multiple DAO calls. (Implies all the DOA calls with in this method should be under one transaction.)

    0 讨论(0)
提交回复
热议问题