title: Hibernate DataSource using JNDI + Weblogic
body: <p>I am trying to connect to my database using a Java <code>DataSource</code>. I am using Weblogic 12c and Hibernate to achieve this. My <code>web.xml</code> file contains the following resource definition:</p>
  
  <pre><code>    &lt;resource-ref&gt;
          &lt;res-ref-name&gt;jdbc/Microtek&lt;/res-ref-name&gt;
          &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
          &lt;res-auth&gt;Container&lt;/res-auth&gt;
      &lt;/resource-ref&gt;
  </code></pre>
  
  <p>In addition, my <code>weblogic.xml</code> contains the following:</p>
  
  <pre><code>    &lt;wls:resource-description&gt;
          &lt;wls:res-ref-name&gt;jdbc/Microtek&lt;/wls:res-ref-name&gt;
          &lt;wls:jndi-name&gt;microtek&lt;/wls:jndi-name&gt;
      &lt;/wls:resource-description&gt;
  </code></pre>
  
  <p>In my Hibernate config file (<code>hibernate.cfg.xml</code>), I have set the  following properties:</p>
  
  <pre><code>      &lt;property name="hibernate.connection.datasource"&gt;jdbc/Microtek&lt;/property&gt;
        &lt;property name="hibernate.connection.pool_size"&gt;10&lt;/property&gt;
        &lt;property name="show_sql"&gt;false&lt;/property&gt;
        &lt;property name="dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/property&gt;
        &lt;property name="hibernate.hbm2ddl.auto"&gt;create&lt;/property&gt;
        &lt;property name="javax.persistence.validation.mode"&gt;none&lt;/property&gt;
  </code></pre>
  
  <p>Also, inside the <code>META-INF</code> directory, I created an XML file called <code>context.xml</code> which contains the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Context&gt;
      &lt;ResourceLink global="jdbc/Microtek" name="jdbc/Microtek" type="javax.sql.DataSource"/&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>Finally, this is the exception I have been getting:</p>
  
  <pre><code>org.hibernate.HibernateException: Could not find datasource
      at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
      at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
      at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
      at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
      at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
      at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2831)
      at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2827)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1838)
      at com.microtekcomputers.models.test.main(test.java:15)
  Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
      at javax.naming.InitialContext.lookup(InitialContext.java:417)
      at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
      ... 8 more
  </code></pre>
  
  <p>It is my first time using JNDI to connect to a data source so I might be missing something. All ideas &amp; recommendations greatly appreciated.</p>
  
  <p><strong>UPDATE:</strong> After making the changes suggested by others, I am now getting the following exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/kernel/KernelStatus
      at weblogic.jndi.Environment.&lt;clinit&gt;(Environment.java:81)
      at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
      at javax.naming.InitialContext.init(InitialContext.java:244)
      at javax.naming.InitialContext.&lt;init&gt;(InitialContext.java:216)
      at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:51)
      at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
      at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
      at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
      at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
      at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
      at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2831)
      at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2827)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1838)
      at com.microtekcomputers.models.Test.main(Test.java:12)
  </code></pre>