Search code examples
oracletomcat7jndiliferay-6portlet

Liferay portlet non-liferay JNDI data source null


For a Liferay 6.2 custom portlet accessing a non liferay Oracle database we are running into an issue where the data source returned is null.

We have configured the tomcat/conf/context.xml

<!-- 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 portlet web.xml 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>

The code for lookup is:

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 use the context.xml resource with a similar data source for the Liferay Oracle database successfully.

Is some other wiring needed for Liferay portlet to establish a connection to another database?

The same portlet code works on weblogic without the web.xml change. Similar JNDI data source lookup code and configuration works on vanilla tomcat (without liferay) and a plain war (non liferay portlet) file.

Update:

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

I have also tried setting the portal.security.manager.strategy=none in portal-ext.properties. This did not work either.

Any insight is much appreciated as we are kind of stuck here.

Thanks!


Solution

  • I just stumbled over this thread 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 in your context.xml:

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

    It should do the trick. If you really asking WHY Liferay can find the jndi resource, but not your portlet: I have no clue...