Fórum

6.1 CE GA2 Tomcat bundle issue with JNDI resources

Bob Dietrich, modificado 11 Anos atrás.

6.1 CE GA2 Tomcat bundle issue with JNDI resources

Regular Member Postagens: 221 Data de Entrada: 16/05/05 Postagens Recentes
I believe there is an issue with Liferay 6.1 CE GA2 and the Tomcat bundle. Specifically, it seems to have broken the use of JNDI resources by servlets and portlets. I found this in a portlet that worked in GA1 but not in GA2.

Clarification: The example uses SQL embedded in a JSP to demonstrate the failure of GA2 in the simplest possible way. It's a horrible practice!

I created a simple war file that uses JNDI per the instructions in Tomcat 7 JNDI Datasource How-To, using MySQL. I did this to eliminate any issues with Liferay, Hibernate or other libraries. The resulting war file consists of three files (plus two jars). The war file is attached, but you'll have to set your own username and password to get it to work. The MySQL drivers have also been placed in both lib and lib/ext just to eliminate any classpath issues.

If you copy the war file to the webapp directory of Liferay 6.1 CE GA1 or vanilla copies of Tomcat 7.0.23 (same as GA1) or Tomcat 7.0.27 (same as GA2) or even Tomcat 7.0.32, you can open http://localhost:8080/test/test.jsp and get the results you would expect. However, if you do the same thing with Liferay 6.1 CE GA2, you get the following exception:
javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/MySqlDS"
	org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.getConnection(QueryTagSupport.java:318)
	org.apache.taglibs.standard.tag.common.sql.QueryTagSupport.doStartTag(QueryTagSupport.java:201)
	org.apache.jsp.test_jsp._jspx_meth_sql_005fquery_005f0(test_jsp.java:119)
	org.apache.jsp.test_jsp._jspService(test_jsp.java:72)

The three files are context.xml (you may need to copy this as test.xml to conf/Catalina/localhost), web.xml, and test.jsp.

context.xml:
<!--?xml version='1.0' encoding='utf-8'?-->
<context>
 <resource name="jdbc/MySqlDS" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" driverClassName="com.mysql.jdbc.Driver" username="user" password="password" url="jdbc:mysql://localhost:3306/lportal?useUnicode=true&amp;characterEncoding=UTF-8&amp;relaxAutoCommit=true" maxActive="200" maxIdle="30" maxWait="10000" minIdle="3" testWhileIdle="true" testOnBorrow="true" timeBetweenEvictionRunsMillis="120000" minEvictableIdleTimeMillis="600000" validationQuery="select 1" />
    <!-- Default set of monitored resources -->
    <watchedresource>WEB-INF/web.xml</watchedresource>
</context>

web.xml:
<!--?xml version="1.0" encoding="UTF-8"?-->
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/MySqlDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  <welcome-file-list>
    <welcome-file>test.jsp</welcome-file>
  </welcome-file-list>
</web-app>

test.jsp:
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;

<sql:query var="rs" datasource="jdbc/MySqlDS">
select foodItemId, name_, points from fooditem
</sql:query>


  
    <title>DB Test</title>
  
  

  <h2>Results</h2>
  
<c:foreach var="row" items="${rs.rows}">
    Id ${row.foodItemId}<br>
    Name ${row.name_}<br>
	Points ${row.points}<br>
</c:foreach>

  



I suppose I could be doing something wrong, but I certainly don't know what that is. Thanks.
Bob Dietrich, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Regular Member Postagens: 221 Data de Entrada: 16/05/05 Postagens Recentes
Forgot to mention I also tried it with the patched CE version (lcepatchers.org).
thumbnail
Juan Gonzalez P, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Liferay Legend Postagens: 3089 Data de Entrada: 28/10/08 Postagens Recentes
No, there isn't any issue in Liferay 6.1 and JNDI.

Why do you use that tags to access DB from jsp?

Your context.xml seems ok, but other changes aren't required.

Just add this property to your portal-ext.properties with your jndi:

jdbc.default.jndi.name=

and that's all.

BTW, is a very very very bad practice to execute SQL like that.
thumbnail
Juan Gonzalez P, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Liferay Legend Postagens: 3089 Data de Entrada: 28/10/08 Postagens Recentes
And be sure to put mysql driver jar into tomcat's classpath.
Bob Dietrich, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Regular Member Postagens: 221 Data de Entrada: 16/05/05 Postagens Recentes
Juan, thanks for replying. My apologies; apparently I didn't make it clear that the JNDI failure was for a portlet connecting to MySQL, not the portal itself.

To clarify the other points:
  • There is definitely an issue. This very simple servlet does *not* fail in vanilla Tomcat or GA1, but does fail in GA2. The behavior of the bundled Tomcat has changed in GA2.
  • As I pointed out, this is solely an example to show the failure, eliminating libraries that might cloud the issue. Agreed, putting DB code in a JSP is horrible and I've never done it.
  • The jdbc.default.jndi.name= only affects the Liferay portal connection to the database through JNDI. Just to make sure, however, I tested with it and the code still failed.
  • The MySQL driver is in Tomcat's class path (overkill: in both lib and lib/ext)
I'll add a clarification to the original posting.

I should also point out that if you look at Tomcat using PSI-Probe, under GA2 PSI-Probe indicates an error for the resource in test: "This resource is misconfigured and runtime information is not available". This does not happen in GA1 or vanilla Tomcat.
thumbnail
Juan Gonzalez P, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Liferay Legend Postagens: 3089 Data de Entrada: 28/10/08 Postagens Recentes
Oh I understand.

To access from external Liferay database just follow this:

http://www.liferay.com/community/wiki/-/wiki/Main/Connect+to+a+Database+with+Plugins+SDK
Bob Dietrich, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Regular Member Postagens: 221 Data de Entrada: 16/05/05 Postagens Recentes
Thanks, but the wiki instructions involve Spring and ServiceBuilder, neither of which were used by the portlet where I discovered this problem.
thumbnail
Hitoshi Ozawa, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Liferay Legend Postagens: 7942 Data de Entrada: 24/03/10 Postagens Recentes
I think liferay.com just plugged a security bug to make it unable to access database directly from jsp.
Bob Dietrich, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Regular Member Postagens: 221 Data de Entrada: 16/05/05 Postagens Recentes
Hitoshi Ozawa:
I think liferay.com just plugged a security bug to make it unable to access database directly from jsp.
Thanks, but the JSP example, horrible as it is, is dying when trying to make the JNDI connection. The same thing happened in my portlet that accesses the database through Java, not the JSP.
thumbnail
Hitoshi Ozawa, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Liferay Legend Postagens: 7942 Data de Entrada: 24/03/10 Postagens Recentes
Sorry, but you'll have to use Spring + Hibernate now. I think the main problem is Liferay.com does not have a Developer Specification Design Manual. I brought this up once and they just showed me a coding guideline. There's different ways to do things but it's depressing when they suddenly drop support on one of them.
Bob Dietrich, modificado 11 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

Regular Member Postagens: 221 Data de Entrada: 16/05/05 Postagens Recentes
Interesting conjecture.

Filed as LPS-32640
Jose F. Fernandez, modificado 10 Anos atrás.

RE: 6.1 CE GA2 Tomcat bundle issue with JNDI resources

New Member Postagens: 3 Data de Entrada: 31/07/13 Postagens Recentes
I'm facing to issues with JNDI when Liferay is deployed in a Tomcat installation. If I undeploy Liferay I can see the JNDI datasources with Psi-probe, when Liferay is running, no JNDI datasources are shown, nor can be accesed. The root cause of the exception is:

Caused by: javax.naming.NameNotFoundException: Name java:comp/env/jdbc/aperte-reports is not bound in this Context

In my case, the problem is caused by the Liferay security manager, when overrides the context, all JNDI entries are lost.
If you have a plugin that enables the security manager, you will get that problem (mine was a carousel portlet), if you don't have such a portlet, with the default configuration, security manager will not be activated, so no problem!

Look for that text in your log:
14:24:29,225 INFO [localhost-startStop-1][PACLPolicyManager:90] Activating PACL policy manager
14:24:29,226 INFO [localhost-startStop-1][PACLPolicyManager:146] Overriding the current security manager to enable plugin security management
14:24:29,232 INFO [localhost-startStop-1][PortalSecurityManager:179] Overriding the initial context factory builder

If you have this, you will have problems with JNDI (no datasources will be shown by Psy-Probe, and other plugins that use JNDI such as Aperte Reports will stop to work)

If you have experienced these problems, you can disable Liferay security manager in portal-ext.properties, adding:
portal.security.manager.strategy=none

Hope will be helpfull for someone, I could not find a solution for this and have to debug code by hours after lot of time lost with configuration settings, trying different versions of Tomcat, etc. etc.

All is working now!!!