title: Hibernate : No CurrentSessionContext configured
body: <p>I'm stuck due to some silly mistake somewhere but not able to figure out !</p>
  
  <p><strong>Hibernate 4.2.6</strong></p>
  
  <p>I have referred to this question, asked several times previously, for example <a href="https://stackoverflow.com/questions/761480/hibernate-no-currentsessioncontext-configured">here</a></p>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration SYSTEM
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="hibernate.bytecode.use_reflection_optimizer"&gt;false&lt;/property&gt;
  
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;root&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/test&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
  
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
  
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;property name="hibernate.current_session_context_class"&gt;org.hibernate.context.ThreadLocalSessionContext&lt;/property&gt;
  
          &lt;mapping resource="Event.hbm.xml"&gt;&lt;/mapping&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>Event.hbm.xml</strong></p>
  
  <pre><code>&lt;!DOCTYPE hibernate-mapping PUBLIC 
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
  
      &lt;class name="com.db.pojo.Event" table="Events"&gt;
  
          &lt;id name="id" column="Id"&gt;
              &lt;generator class="native"&gt;&lt;/generator&gt;
          &lt;/id&gt;
          &lt;property name="title" column="Title"&gt;&lt;/property&gt;
  
          &lt;property name="date" column="Date" type="timestamp"&gt;&lt;/property&gt;
  
      &lt;/class&gt;
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>HibernateUtil</strong></p>
  
  <pre><code>package com.db.util;
  
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistryBuilder;
  
  public class HibernateUtil {
  
      private static final SessionFactory sessionFactory = buildSessionFactory();
  
      private static SessionFactory buildSessionFactory() {
          // TODO Auto-generated method stub
  
          Configuration configuration = new Configuration();
          configuration.configure("hibernate.cfg.xml");
  
          ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
                  .applySettings(configuration.getProperties());
          return configuration.buildSessionFactory(serviceRegistryBuilder
                  .buildServiceRegistry());
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
  }
  </code></pre>
  
  <p>Whenever I try this :</p>
  
  <pre><code>Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  </code></pre>
  
  <p>I get :</p>
  
  <pre><code>Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
      at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
      at test.EventManager.createAndStoreEvent(EventManager.java:27)
      at test.EventManager.main(EventManager.java:17)
  </code></pre>
  
  <p><strong>Edited after executing on my personal laptop</strong></p>
  
  <ol>
  <li>The code runs fine if I use 'thread' in the cfg file</li>
  <li>If I use 'ThreadLocalSessionContext', I get the below exception(<strong>Please note that even the output on the console is now enormous than the previous run</strong>) - is it that the class 'ThreadLocalSessionContext' was not present in the 3.0 version(<em>I tried to find in the API but the site doesn't have the older API</em> )</li>
  </ol>
  
  <p>Now I'm suspicious about the dtd - are Hibernate 3.0 jars being referred somewhere while the local ones are ignored, even though SYSTEM is specified? I got the previous exception(no current context) while executing the code on a machine with restricted Internet access. But when I executed the same on my personal laptop :</p>
  
  <pre><code>eventDesciption : Team eventDate : 2013-12-12
  Nov 28, 2013 8:55:59 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager &lt;clinit&gt;
  INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
  Nov 28, 2013 8:55:59 PM org.hibernate.Version logVersion
  INFO: HHH000412: Hibernate Core {4.2.6.Final}
  Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: HHH000206: hibernate.properties not found
  Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Environment buildBytecodeProvider
  INFO: HHH000021: Bytecode provider name : javassist
  Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration configure
  INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
  Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration getConfigurationInputStream
  INFO: HHH000040: Configuration resource: hibernate.cfg.xml
  Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration addResource
  INFO: HHH000221: Reading mappings from resource: resources/Event.hbm.xml
  Nov 28, 2013 8:55:59 PM org.hibernate.cfg.Configuration doConfigure
  INFO: HHH000041: Configured SessionFactory: null
  Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
  Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000115: Hibernate connection pool size: 20
  Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000006: Autocommit mode: false
  Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/test]
  Nov 28, 2013 8:55:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000046: Connection properties: {user=root, password=****}
  Nov 28, 2013 8:56:00 PM org.hibernate.dialect.Dialect &lt;init&gt;
  INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
  Nov 28, 2013 8:56:00 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
  INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
  Nov 28, 2013 8:56:00 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
  INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
  Nov 28, 2013 8:56:00 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory &lt;init&gt;
  INFO: HHH000397: Using ASTQueryTranslatorFactory
  Nov 28, 2013 8:56:00 PM org.hibernate.internal.SessionFactoryImpl buildCurrentSessionContext
  ERROR: HHH000302: Unable to construct current session context [org.hibernate.context.ThreadLocalSessionContext]
  org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.context.ThreadLocalSessionContext]
      at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:152)
      at org.hibernate.internal.SessionFactoryImpl.buildCurrentSessionContext(SessionFactoryImpl.java:1544)
      at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:516)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790)
      at com.db.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
      at com.db.util.HibernateUtil.&lt;clinit&gt;(HibernateUtil.java:9)
      at test.EventManager.createAndStoreEvent(EventManager.java:27)
      at test.EventManager.main(EventManager.java:17)
  Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.context.ThreadLocalSessionContext
      at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:319)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149)
      ... 7 more
  
  Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
      at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
      at test.EventManager.createAndStoreEvent(EventManager.java:27)
      at test.EventManager.main(EventManager.java:17)
  </code></pre>