Message Boards

Connecting to JNDI Data Sources

Pankaj Kumar, modified 4 Years ago.

Connecting to JNDI Data Sources

Regular Member Posts: 101 Join Date: 7/27/14 Recent Posts
Hi All,
I am using Liferay dxp 7.2.Can any body tell me how we can connect to external database using JNDI Data Sources.
Thanks,Pankaj Semwal
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Connecting to JNDI Data Sources

Liferay Legend Posts: 14914 Join Date: 9/2/06 Recent Posts
The only caveat is that you need to set the class loader to the portal's class loader before you invoke the JNDI lookup.

Otherwise the JNDI lookup is exactly the same as you would do it in any other application.
Pankaj Kumar, modified 4 Years ago.

RE: Connecting to JNDI Data Sources

Regular Member Posts: 101 Join Date: 7/27/14 Recent Posts
Thanks David  for response.
In ext.spring file i have added below property  <bean id="bean1" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/db1" />
         
    </bean>
    
     <bean id="customDBDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true">
    <property name="targetDataSource">
        <ref bean="bean1" />
    </property>and while starting the module i am getting below error
 Invocation of init method failed; nested except
ion is javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.naming.java.javaURLContextFactory [Root exception is java.lan
g.ClassNotFoundException: Unable to load class org.apache.naming.java.javaURLContextFactory]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Connecting to JNDI Data Sources

Liferay Legend Posts: 14914 Join Date: 9/2/06 Recent Posts
You can't use the spring example because it will not properly set the class loader.
Pankaj Kumar, modified 4 Years ago.

RE: Connecting to JNDI Data Sources

Regular Member Posts: 101 Join Date: 7/27/14 Recent Posts
Ok..Thanks...I was able to get the Datasource connection through the way we get in J2EE application.
InitialContext ctx = new InitialContext();