Liferay DB configuration using JNDI

In this  post I want to share liferay DB configuration using JNDI instead of portal-ext.properties.

For JDBC connection we use portal-ext.properties.

If your custom service builder portlet is pointing to another database then we can use JNDI to configure that database.

So now here I am doing this using JNDI configuration.
Follow below steps:
1) We have to add DB properties in server.xml instead of portal-ext.properties
    Here is an example:
  <GlobalNamingResources>
<!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="jdbc/CustomDBPoolShared" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@YOUR_SERVER:1521:YOUR_SERVICE" username="root" password="root" maxActive="20" maxIdle="5" maxWait="10000" /> ...... </GlobalNamingResources>
2) Create ResourceLink in context.xml.
 
<Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --> <ResourceLink name="jdbc/CustomDBPoolShared" global="jdbc/CustomDBPoolShared" type="javax.sql.DataSource"/> .... </Context> 
3) Now change ext-spring.xml of your custom portlet.
<bean id="customDBDataSourceTarget" class="com.liferay.portal.spring.jndi.JndiObjectFactoryBean" lazy-init="true"> <property name="jndiName"> <value>java:/comp/env/jdbc/CustomDBPoolShared</value> </property> </bean> <bean id="customDBDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" lazy-init="true"> <property name="targetDataSource"> <ref bean="customDBDataSourceTarget" /> </property> </bean>

Thats IT!

So this way we can configure liferay database configuration using JNDI.

Blogs
Thanks for this post it is very clear how you achieve it. I know we have discussed this type of approach before but right now we are currently:

1. point to ROOT.xml file via this add to our portal-ext.properties
jdbc.default.jndi.name=jdbc/LiferayPool

2. in the ROOT.xml file we define the jndi like this:
<!--<Manager className="com.liferay.support.tomcat.session.SessionLessManagerBase" />-->

<Resource name="jdbc/LiferayPool"
auth="Container"
type="javax.sql.DataSource"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://cont90:1433/LR_CONT165_PROD_GA2_A"
username="liferaysqladmin"
password="L!fer@y5ql"
maxActive="500"
maxIdle="120" maxWait="20000"
removeAbandoned="true"
removeAbandonedTimeout="20"
logAbandoned="true"
maxPoolSize="100"
minPoolSize="10"
acquireIncrement="10"
validationQuery="SELECT 1"
minEvictableIdleTimeMillis="3600000"
timeBetweenEvictionRunsMillis="1800000"
numTestsPerEvictionRun="10"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
/>

Could you comment on the advantage with your approach?

Thanks.
JNDI is fatser than JDBC configuration. And main advantage while moving or deploying code to other developement.
JNDI really useful when you have to move an application between environments: development to integration to test to production. If you configure each app server to use the same JNDI name, you can have different databases in each environment and not have to change your code. You just pick up the WAR file and drop it in the new environment.
Hi Devang,

I am using a standard OSGi module, without Spring. How do I do that last step without spring?

Thank you,
Clint
As I know if you are using service builder than you have to do with spring for LR 6.2 for external database configuration.
but no one can stop you to do with JDBC if you are not using service builder.
Thank you for providing this. Where should i do the third step if all i have is a liferay MVC portlet?

Using Liferay 7.0 GA2
Hi Kanwar,

If you are connecting to an external database, your portlet should have an ext-spring.xml file in the META_INF folder. You should perform step 3 on this file.
How can such a JNDI datasource be used with Liferay 7 in a Portlet that does NOT use Spring? The standard JavaEE lookup through the IntialContext does not work any longer with Liferay 7