Search code examples
javaspringweblogicjndi

Error on configuration of JNDI lookup for WebLogic + Spring


I'm migrating an application from OC4J to WebLogic 12c and the Spring beans are giving an error I can't figure out how to solve. My question is what can be the cause of this error. I have the following bean for the JNDI lookup:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="${datasource.jndiname}" />
    <property name="lookupOnStartup">
        <value>false</value>
    </property>
    <property name="proxyInterface">
        <value>javax.sql.DataSource</value>
    </property>
</bean>

The value ${datasource.jndiname} is expected to come from a config.properties file with the following line:

server.database.datasource=${datasource.jndiname}

And the value of server.database.datasource comes from a config.filter file with the line:

server.database.datasource=jdbc/DATASOURCE

This works fine with OC4J and it also works when I replace the ${datasource.jndiname} to its value jdbc/DATASOURCE in WebLogic, but it gives me the following error if I keep the reference (and I need to keep it):

JndiObjectTargetSource failed to obtain new target object; nested exception is javax.naming.NameNotFoundException: While trying to lookup '${datasource.jndiname}' didn't find subcontext '${datasource'. Resolved ''; remaining name '${datasource/jndiname}'

Solution

  • After some research, I found a property for the bean that fixed the error. Setting the property resourceRef to false makes the reference ${datasource.jndiname} work as expected. In the final code shown below I also added a JndiTemplate.

    <bean id="dsJndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.provider.url">t3://localhost:7001</prop>
                <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
            </props>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${datasource.jndiname}" />
        <property name="resourceRef" value="false"/>
        <property name="lookupOnStartup" value="false"/>
        <property name="proxyInterface" value="javax.sql.DataSource"/>
        <property name="jndiTemplate">
            <ref local="dsJndiTemplate" />
        </property>
    </bean>