2

I am trying to connect to my database using a Java DataSource. I am using Weblogic 12c and Hibernate to achieve this. My web.xml file contains the following resource definition:

    <resource-ref>
        <res-ref-name>jdbc/Microtek</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

In addition, my weblogic.xml contains the following:

    <wls:resource-description>
        <wls:res-ref-name>jdbc/Microtek</wls:res-ref-name>
        <wls:jndi-name>microtek</wls:jndi-name>
    </wls:resource-description>

In my Hibernate config file (hibernate.cfg.xml), I have set the following properties:

      <property name="hibernate.connection.datasource">jdbc/Microtek</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">false</property>
      <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
      <property name="hibernate.hbm2ddl.auto">create</property>
      <property name="javax.persistence.validation.mode">none</property>

Also, inside the META-INF directory, I created an XML file called context.xml which contains the following:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <ResourceLink global="jdbc/Microtek" name="jdbc/Microtek" type="javax.sql.DataSource"/>
</Context>

Finally, this is the exception I have been getting:

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

It is my first time using JNDI to connect to a data source so I might be missing something. All ideas & recommendations greatly appreciated.

UPDATE: After making the changes suggested by others, I am now getting the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/kernel/KernelStatus
    at weblogic.jndi.Environment.<clinit>(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.<init>(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)

1 Answer 1

1

You do not have a connection factory defined for your data source, something like this in the code:

environment.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory"); 

Or this in the hibernate.cfg.xml:

<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>

In weblogic you can configure connection factories to your need, WLInitialContextFactory is the default.

Also see the Oracle docs and client examples

Sign up to request clarification or add additional context in comments.

3 Comments

I added the property to the hibernate file and I am getting the error above
Did you get past this? Try removing WebLogic System Libraries from the Bootstrap Entries section. stackoverflow.com/questions/23639876/…
I actually decided not to do it using JNDI because of time constraints but I will later on try it again and let you know if it works. Thanks for the help

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.