Fórum

Liferay DXP and War applications connection to JNDI

Rodrigo Hernández, modificado 6 Anos atrás.

Liferay DXP and War applications connection to JNDI

New Member Postagens: 9 Data de Entrada: 10/01/17 Postagens Recentes
Hi.

I had been trying to consume a JNDI JDBC resource from a .war application over a WebSphere server and I'm always getting a NoInitialContextError.

Is there any issue related to this behaviour? Or any documentation?

Thx for your time.
thumbnail
David H Nebinger, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

Liferay Legend Postagens: 14917 Data de Entrada: 02/09/06 Postagens Recentes
Why not just try:

Properties props = new Properties();
props.set("jndi.name", "jdbc/MyJndiDatasource");
DataSource ds = DataSourceFactoryUtil.getDataSourceFactory().initDataSource(props);


You must remember that your code is running within the OSGi container and therefore may not have access to a valid context.

By leveraging the Liferay facilities to access the portal's DS factory, you're gaining access to the portal's context and should get to the JNDI DS.











Come meet me at Devcon 2017 or 2017 LSNA!
Rodrigo Hernández, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

New Member Postagens: 9 Data de Entrada: 10/01/17 Postagens Recentes
I got an error:

ERROR [DataSourceFactoryImpl:130] Unable to lookup jdbc/ds
javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".
at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1970)
at com.ibm.ws.naming.ipbase.NameSpace.retrieveBinding(NameSpace.java:1377)
at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1220)
at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142)
at com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436)
at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477)
at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)
at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370)
at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
at javax.naming.InitialContext.lookup(InitialContext.java:428)
at com.liferay.portal.kernel.jndi.JNDIUtil._lookup(JNDIUtil.java:137)
at com.liferay.portal.kernel.jndi.JNDIUtil.lookup(JNDIUtil.java:34)
at com.liferay.portal.dao.jdbc.DataSourceFactoryImpl.initDataSource(DataSourceFactoryImpl.java:127)

Any idea about this?
thumbnail
David H Nebinger, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

Liferay Legend Postagens: 14917 Data de Entrada: 02/09/06 Postagens Recentes
What name is your ds registered under in WAS?









Come meet me at Devcon 2017 or 2017 LSNA!
Rodrigo Hernández, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

New Member Postagens: 9 Data de Entrada: 10/01/17 Postagens Recentes
The jndi name of my resource is named in the same way. jdbc/ds

I was looking in the code of the classes DataSourceFactoryUtil and JNDIUtil (for LR 6.1. I don't know if the classes between versions have changed) and, at the end, after validating the structure of the JNDI name, the line
(DataSource) context.lookup(jndiName) 
is called. Look the method _lookup of JNDIUtil

So, what's the difference of call the snippet that you suggested me of the following?

Context initContext = new InitialContext();
DataSource ds = (DataSource) context.lookup("jdbc/ds");
thumbnail
David H Nebinger, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

Liferay Legend Postagens: 14917 Data de Entrada: 02/09/06 Postagens Recentes
Assuming you have your scope and everything correct, that should be all that is necessary. Is your Liferay database connection also defined via jndi?










Come meet me at Devcon 2017 or 2017 LSNA!
Rodrigo Hernández, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

New Member Postagens: 9 Data de Entrada: 10/01/17 Postagens Recentes
In my portlet-ext.properties file the jdbc.default.jndi.name property is declared, but with a different datasource.

With your question, should I have to suppose that my jdbc/ds datasource must be the one to be declared? Or, does exist a way of handle multiple datasources that works over WAS?
thumbnail
David H Nebinger, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

Liferay Legend Postagens: 14917 Data de Entrada: 02/09/06 Postagens Recentes
I would say that whatever scope you defined the jdbc/LiferayPool datasource in WAS, the same scope should be used for jdbc/db.

Basically if one is working and the other is not, the reason it is not will likely be the difference in how the two are defined.








Come meet me at Devcon 2017 or 2017 LSNA!
Rodrigo Hernández, modificado 6 Anos atrás.

RE: Liferay DXP and War applications connection to JNDI

New Member Postagens: 9 Data de Entrada: 10/01/17 Postagens Recentes
I've checked and both have been configured in the same way. The only difference is the resource that are invoking.