Liferay portlet non-liferay JNDI data source null

For a Liferay 6.2 user portlet that accesses an Oracle database without liferay, we encounter a problem when the returned data source is null.

We configured the tomcat / conf / context.xml file

<!-- Adding custom New non liferay datasource --> <Resource name="jdbc/NewPool" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = dbservernameorip)(PORT = 9999)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = dbSIDorservicename)))" username="user" password="pwd" maxActive="35" maxIdle="10" maxWait="20000" removeAbandoned="true" logAbandoned="true" minEvictableIdleTimeMillis="3600000" timeBetweenEvictionRunsMillis="1800000" testOnBorrow="true" testOnReturn="false" /> 

The web.xml portlet contains:

 <resource-ref> <description>Oracle Datasource example</description> <res-ref-name>jdbc/NewPool</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 

Search Code:

 String JNDI = "jdbc/NewPool" _log.debug("JNDI Name is: " + JNDI); _log.debug("dataSource in dbConnect is :" + dataSource); Context context = new InitialContext(); Context envContext = (Context)context.lookup("java:/comp/env"); _log.debug("envContext in dbConnect is :" + envContext); try { DataSource ds = (DataSource)envContext.lookup(JNDI); 

Liferay can successfully use the context.xml resource with a similar data source for the Oracle Liferay database.

Is any other wiring required for the Liferay portlet to establish a connection to another database?

The same portlet code works in weblogic without changing web.xml. A similar search and configuration code for the JNDI data source runs on vanilla tomcat (without liferay) and a simple war file (non liferay portlet).

Update:

I checked db connections on server with netstat -an | grep dbport. it does not show the established connection.

I also tried setting portal.security.manager.strategy = none in portal -ext.properties. That didn't work either.

Any insight is greatly appreciated as we are stuck here.

Thanks!

+5
source share
3 answers

I just stumbled upon this topic in the Liferay forum, which basically says. oput this in your tomcat / conf / server.xml

 <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/> <Resource name="jdbc/XXX" auth="Container" type="javax.sql.DataSource" maxActive="20" maxIdle="10" maxWait="5000" removeAbandoned="true" removeAbandonedTimeout="250" validationQuery="SELECT 1" username="user2" password="pwd2" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/myOtherDb"/> 

and this is in your context. xml:

 <ResourceLink name="jdbc/XXX" global="jdbc/XXX" type="javax.sql.DataSource"> 

He has to do the trick. If you really ask why Liferay can find the jndi resource but not your portlet: I have no hint ...

+1
source

We also had problems using resources and pools. Since we had very few requests for processing and performance that didn’t cause concern in our scenario, we used the JDBC connection directly without a pool and worked fine (we connect to the MS Sql server).

 Connection conn = null; Statement stmt = null; ResultSet rs = null; String url = "jdbc:jtds:sqlserver://host/dbname"; String driver = "net.sourceforge.jtds.jdbc.Driver"; String db_userName = PropsUtil.get("jdbc.default.username"); String db_password = PropsUtil.get("jdbc.default.password"); try { Class.forName(driver); conn = DriverManager.getConnection(url, db_userName, db_password); String sql = "SELECT * FROM Users"; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()){ // DO WHAT YOU WANT return true; } rs.close(); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) conn.close(); }catch(SQLException se){ }// do nothing try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try 

And it works great. The username and password are configured in the portal -ext.properties file (we use the same account as for our own db liferay)

Hope this helps

+1
source

I think that is our question. It seems to have been a typo.

All references to dataSource must be changed to ds. The code has been changed. It turned out that the variable name was not changed to ds in the code after the declaration of the ds variable during troubleshooting.

 String JNDI = "jdbc/NewPool" _log.debug("JNDI Name is: " + JNDI); _log.debug("dataSource in dbConnect is :" + ds); Context context = new InitialContext(); Context envContext = (Context)context.lookup("java:/comp/env"); _log.debug("envContext in dbConnect is :" + envContext); try { DataSource ds = (DataSource)envContext.lookup(JNDI); _log.debug("dataSource in dbConnect is :" + ds) 

We need to check this out. I will post the results after the final test.

+1
source

All Articles