Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Tomcat connection pool error in DXP
I have a JNDI connection pool defined at Tomcat level . When i start the server the connection pool is set up and no error is thrown but when i try to retrieve the same pool from my portlet using a simple java class i get the following error
Is there something in particular that i need to take care of when my portlet is deployed in OSGI framework to get this working. The same code was working in Liferay 6.2 and is working in vanilla Tomcat 8.
java.lang.ClassCastException: org.apache.naming.ResourceLinkRef cannot be cast to javax.sql.DataSource
at com.test.HbaseUtil.openConnection(HbaseUtil.java:55)
at com.test.DAOImpl.getDetails(DAOImpl.java:112)
at com.test.HomeMBean.getDetails(HomeMBean.java:341)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Is there something in particular that i need to take care of when my portlet is deployed in OSGI framework to get this working. The same code was working in Liferay 6.2 and is working in vanilla Tomcat 8.
Hi,
I have the same problem with liferay 7 ga3 and tomcat8 . Also for me the same configuration correctaly work in liferay 6.2.
I have the same problem both with hibernate configuration and with Spring. With Hibernate I tested the connection using jdbc and it works.
I have the same problem with liferay 7 ga3 and tomcat8 . Also for me the same configuration correctaly work in liferay 6.2.
I have the same problem both with hibernate configuration and with Spring. With Hibernate I tested the connection using jdbc and it works.
We are experiencing similar issue. Is it resolved for you?
Jatinderveer Singh:
java.lang.ClassCastException: org.apache.naming.ResourceLinkRef cannot be cast to javax.sql.DataSource at com.test.HbaseUtil.openConnection(HbaseUtil.java:55) at com.test.DAOImpl.getDetails(DAOImpl.java:112) at com.test.HomeMBean.getDetails(HomeMBean.java:341) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
While the original question is somewhat old, after two more having stated "the same" problem, I'd like to ask for more information - everybody who posted here:
- Are you sure you have the same problem? With code from the com.test package? At least this is not part of Liferay
- Your configuration might have been worked with Liferay 6.2, but that might also have been on a different tomcat version. You don't give any hint about the configuration you're using, no steps to reproduce
Yes Olaf, it works fine for me with Liferay 6.1, whereas the same issue I observed with Liferay 7.3 and tomcat 9. Please find below the configuration for server.xml and portlet-application-context.xml respectively.
server.xml
<Resource name="jdbc/LiferayPool"
global="jdbc/LiferayPool"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="root"
driverClassName="com.mysql.cj.jdbc.Driver"
description="Liferay database."
url="jdbc:mysql://localhost:3305/lportal731?autoReconnect=true"
testWhileIdle="true"
testOnBorrow="true"/>
context.xml
<ResourceLink name="jdbc/LiferayPool" global="jdbc/LiferayPool" type="javax.sql.DataSource"/>
portlet-application-context.xml
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/LiferayPool"/>
</bean>
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jndiDataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan" value="com.nividous.rpa.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
Thanks in advance.
Cheers,
Dipak
server.xml
<Resource name="jdbc/LiferayPool"
global="jdbc/LiferayPool"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="root"
driverClassName="com.mysql.cj.jdbc.Driver"
description="Liferay database."
url="jdbc:mysql://localhost:3305/lportal731?autoReconnect=true"
testWhileIdle="true"
testOnBorrow="true"/>
context.xml
<ResourceLink name="jdbc/LiferayPool" global="jdbc/LiferayPool" type="javax.sql.DataSource"/>
portlet-application-context.xml
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/LiferayPool"/>
</bean>
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jndiDataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="packagesToScan" value="com.nividous.rpa.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
Thanks in advance.
Cheers,
Dipak
The JNDI context for the app server is not exposed into the OSGi container. In order to get to it you have to use the Portal's class loader, something the JndiObjectFactoryBean has no idea how to do.
You have to use your own implementation of that class that knows how to temporarily switch to the portal class loader when doing the JNDI lookup and restoring on return.
You have to use your own implementation of that class that knows how to temporarily switch to the portal class loader when doing the JNDI lookup and restoring on return.