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?

jconlin
  • 3,806
  • 6
  • 31
  • 32

5 Answers5

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

harriyott
  • 10,505
  • 10
  • 64
  • 103
Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
  • `org.hibernate.context.ThreadLocalSessionContext` – bora.oren Mar 28 '11 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 Jul 01 '11 at 10:02
  • 1
    Hibernate 4+ moved this class to: org.hibernate.context.internal.ThreadLocalSessionContext as mentioned by Jacob below – bigleftie Oct 10 '13 at 23:58
  • In @atkeE2036's answer, "hibernate" is misspelled. Here's the correct spelling: org.hibernate.context.ThreadLocalSessionContext – Yuriy Nemtsov Apr 27 '10 at 21:47
  • I was trying to add this line to persistence.xml, but it was actually to hibernate.cfg.xml. This solved my issue. – George Sep 11 '15 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).

Jacob Mouka
  • 2,005
  • 18
  • 26
3

In Hibernate 4.1.5 it is:

<property> name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
Jatin
  • 31,116
  • 15
  • 98
  • 163
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>
Rejeev Divakaran
  • 4,384
  • 7
  • 36
  • 36
0
var config = new Configuration().Configure(); // Read config from hibernate.cfg.xml
config.CurrentSessionContext<WebSessionContext>(); // <-- No CurrentSessionContext fixing
return config.BuildSessionFactory();
ADM-IT
  • 3,719
  • 1
  • 25
  • 26