26

I keep getting a :

HibernateException: No CurrentSessionContext configured!

in my code. The only information other searches that returned is that the culprit is:

<property name="current_session_context_class">thread</property>

in my hibernate.cfg.xml. I do have that in my hibernate.cfg.xml file, and I am running this in Tomcat 6. Does anyone know other possible causes?

5 Answers 5

26

I believe you need to set your current_session_context_class to an actual class...I have my hibernate.cfg.xml configured for that as follows:

<property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>

The above code is used for a mySql database but i don't think it matters what type the database is for the above property

Hope this helps

5
  • In @atkeE2036's answer, "hibernate" is misspelled. Here's the correct spelling: <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property> Commented Apr 27, 2010 at 21:47
  • <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
    – bora.oren
    Commented Mar 28, 2011 at 14:25
  • 3
    @javaloper, it would be nice if you just said that there was a typo: "hibnerate" instead of "hibernate". I was wondering why you posted the same thing in your comment.
    – Jamol
    Commented Jul 1, 2011 at 10:02
  • 1
    Hibernate 4+ moved this class to: org.hibernate.context.internal.ThreadLocalSessionContext as mentioned by Jacob below
    – bigleftie
    Commented Oct 10, 2013 at 23:58
  • I was trying to add this line to persistence.xml, but it was actually to hibernate.cfg.xml. This solved my issue. Commented Sep 11, 2015 at 13:34
11

Both of these work in Hibernate 4+:

<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>

In earlier version of Hibernate it used org.hibernate.context.ThreadLocalSessionContext (as shown in the other answers).

3

In Hibernate 4.1.5 it is:

<property> name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
2

I am using Hibernate 4 on tomcat 7 and the below property (the one mentioned in the question) worked for me.

<property name="current_session_context_class">thread</property>
0
var config = new Configuration().Configure(); // Read config from hibernate.cfg.xml
config.CurrentSessionContext<WebSessionContext>(); // <-- No CurrentSessionContext fixing
return config.BuildSessionFactory();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.