将Liferay 7.x与Oracle 11g2外部数据库连接-NullPointerException

时间:2018-08-26 00:13:13

标签: java tomcat liferay liferay-7 liferay-7.1

首先,我使用Liferay CE portal-7.1.0-GA1和Liferay portal 7.0 CE GA1进行了尝试。我将详细说明我的过程。我的portal-ext.properties(Liferay HOME文件夹)看起来如下:

jdbc.ext.driverClassName=oracle.jdbc.OracleDriver
jdbc.ext.url=jdbc:oracle:thin:@localhost:1521:db
jdbc.ext.username=xxx
jdbc.ext.password=xxx

JAR:

- ojdbc14.jar (\liferay-portal-7.0-ce-ga1\tomcat-8.0.32\lib)
- liferay-portal-oracledb-support-1.0 and liferay-portal-oracledb-support-1.0-SNAPSHOT (liferay-portal-7.0-ce-ga1\tomcat-8.0.32\webapps\ROOT\WEB-INF\lib)
- added ext-spring.xml (\modules\DemoService\DemoService-service\build\resources\main\META-INF\spring)

ext-spring.xml的代码

    <?xml version="1.0"?>
    <beans default-destroy-method="destroy" default-init-method="afterPropertiesSet" xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
       <!--bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean"
          id="liferayDataSourceFactory">
          <property name="propertyPrefix" value="jdbc.ext." />
          <property name="properties">
              <props>
                  <prop key="custom.jndi.name">extDataSource</prop>
              </props>
          </property>
          </bean-->
       <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean"
          id="liferayDataSourceFactory">
          <property name="propertyPrefix" value="jdbc.ext." />
       </bean>
       <bean
          class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
          id="liferayDataSource">
          <property name="targetDataSource" ref="liferayDataSourceFactory" />
       </bean>
       <alias alias="extDataSource" name="liferayDataSource" />
    </beans> 

收到此错误:

    00:10:31,259 ERROR [http-nio-8080-exec-5][render_portlet_jsp:131] null
    java.lang.NullPointerException
    at com.service.service.CountryLocalServiceUtil.getCountriesCount(CountryLocalServiceUtil.java:207)
    at com.demo.portlet.DemoPortlet.doView(DemoPortlet.java:39)
    at com.liferay.portal.kernel.portlet.LiferayPortlet.doDispatch(LiferayPortlet.java:302)
    at com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet.doDispatch(MVCPortlet.java:474)
    at javax.portlet.GenericPortlet.render(GenericPortlet.java:262)
    at com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet.render(MVCPortlet.java:294)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:103)
    at com.liferay.portlet.ScriptDataPortletFilter.doFilter(ScriptDataPortletFilter.java:57)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:100)
    at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:64)
    at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:105)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at com.liferay.portal.osgi.web.servlet.context.helper.internal.ServletContextHelperRegistrationImpl$PortletServletWrapper.service(ServletContextHelperRegistrationImpl.java:507)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:153)
    at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:50)
    at com.liferay.portal.osgi.web.servlet.context.helper.internal.ServletContextHelperRegistrationImpl$RestrictPortletServletRequestFilter.doFilter(ServletContextHelperRegistrationImpl.java:527)
    at org.eclipse.equinox.http.servlet.internal.registration.FilterRegistration.doFilter(FilterRegistration.java:121)
    at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:45)
    at org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:71) 

Country是service.xml中的实体,在构建Gradle之后,将生成CountryLocalServiceUtil。如果您使用的是Liferay 7,这是创建第一个应用程序的简单过程。

这是生成的类,我可以给出代码:

  • 在Portlet中,我刚刚调用了函数CountryLocalServiceUtil.getCountriesCount()

  • CountryLocalServiceUtil中的此函数是:

    public static int getCountriesCount() {
        return getService().getCountriesCount(); 
    }
    
  • 打开声明为:

    CountryLocalService.java @Transactional(propagation = Propagation.SUPPORTS, 
                                            readOnly = true)
    public int getCountriesCount(); 
    

2 个答案:

答案 0 :(得分:0)

您正在运行Liferay 7.x CE。请注意,Liferay社区版(CE)仅支持开源数据库,例如没有Oracle。有一个社区提供的加载项,该加载项增加了对Oracle和其他商业数据库的支持,但您未表示已安装它。

因此,似乎您正在从受支持的数据库中运行Liferay,并尝试在“ jdbc.ext”数据库中连接到Oracle。我希望这行不通,因为这将要求servicebuilder映射到Oracle,尽管CE中未包含它们。

您始终可以使用纯JDBC(例如,没有服务生成器)或try Antonio's plugin。目前,我只知道它适用于7.0,而不适用于7.1(但我在这里可能错了)

答案 1 :(得分:0)

我几乎可以确定您的问题不是数据库,而是服务对象不可用,因为您的问题不是getCountriesCount(),而是getService()

作为实验,请尝试对所需的服务不要使用* Util类,而是使用OSGi @Reference,这应排除数据库的问题(至少在此问题上如此)。如果您无法获得服务,则可能会发现比* Util类中抛出NPE更好的故障排除信息。

例如,在您的组件中:

@Reference
private volatile CountryLocalService countryLocalService;

Liferay的来源中有很多示例,例如: https://github.com/liferay/liferay-portal/blob/180d89ccaa80b86e68402c73a1483cd9e1817311/modules/apps/export-import/export-import-resources-importer/src/main/java/com/liferay/exportimport/resources/importer/internal/util/ImporterFactory.java