0
votes

I have created a oracle datasource in weblogic with the name jdbc/myDS.

Weblogic created a xml file in mydomain/config/jdbc and the configuration works from the weblogic admin console. Test connection is working. My spring context file details are:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/myDS"/>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <util:map>
            <entry key="hibernate.hbm2ddl.auto" value="update" />
            <entry key="hibernate.show_sql" value="true" />
        </util:map>
    </property>
</bean>

<bean id="myDAO" class ="com.example.MyDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

My Java class is:

public class MyDAOImpl implements MyDAO{

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory){
    this.sessionFactory = sessionFactory;
}

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void persistPerson(Person  person) {
    Session session = getSessionFactory().openSession();

    try {

    session.beginTransaction();

    session.save(person);

    session.getTransaction().commit();

    }catch(HibernateException he) {

        he.printStackTrace();
    } finally {
         session.close();
    } 
}
}

An error occurred during activation of changes, please see the log for details. Message icon - Error weblogic.application.ModuleException: Message icon - Error While trying to lookup 'jdbc.myDS' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/myDS'

1
Please do help in resolving this error - Knight
This seems like a simple typo - why is it looking for jdbc.myDS instead of jdbc/myDS. - Display Name is missing
I have to give target to the jndi datasource "Admin Server". It started working. - Knight

1 Answers

-1
votes

The following provides a Spring XML configuration to obtain a datasource by a JNDI name and inject it into Springs AnnotationSessionFactoryBean.

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/YourJndi"/>
</bean> 


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.java.model.Employee</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
        </props>
    </property>
</bean>

This example has been sucessfully tested on Weblogic 10.3.4 with Oracle 11g.