Search code examples
javatomcatjdbcliferayjndi

Setting up JNDI with a Tomcat 7 environment using Liferay 6.1


I'm very confused why this isn't working. The environment is a Liferay 6.1 instance with Tomcat 7 and the database is NOT the default database from Liferay. It's a secondary server that is used for data. So I'm not sure if that matters with Liferay or not.

web.xml (located in webapps/conf in Tomcat)

<web-app>...
<resource-ref>
    <description>My database</description>
    <res-ref-name>jdbc/xxx</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
</web-app>

server.xml (located in webapps/conf in Tomcat)

<GlobalNamingResources>
<Resource name="jdbc/xxx" auth="Container" type="javax.sql.DataSource" username="a" password="y" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/xxx" 
    maxActive="200" maxIdle="25" />
</GlobalNamingResources>

context.xml (located in webapps/conf in Tomcat)

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

Code:

        Context ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/xxx");
        Connection conn = ds.getConnection();

Error:

2016-12-21 19:13:04 FATAL asdasdsd:128 - Exception thrown in (removed): 
javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/xxx] is not bound in this Context. Unable to find [java:comp].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)

What am I missing?

Edit:

Also confirmed the following:

  1. mysql connector is in the path
  2. same jar is in the classpath of the portlet

Second Edit:

I created a brand new, fresh dynamic web application project with the same configuration and DAO layer and it worked 100%. I have a feeling it's related to Liferay now. Unfortunately..

Third Edit:

Tried everything, including this article: http://www.journaldev.com/2513/tomcat-datasource-jndi-example-java

This didn't work either. Same exception. The lack of information about this issue on the Liferay website is amazing to me. Documentation appears to be very much lacking.


Solution

  • For anyone that this helps in the future, because boy was this a pain and not well documented.

    I had to add this to the portlet-ext.properties of the Liferay application:

    portal.security.manager.strategy=none
    

    Once I found this link: Liferay/Tomcat "hot-deploy" closes JNDI connection, how can I keep it open?

    It ended up solving my problem.