0

首先,我使用 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

罐子:

- 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(); 
    
4

2 回答 2

0

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

因此,您似乎正在从受支持的数据库运行 Liferay,并尝试在“jdbc.ext”数据库中连接到 Oracle。我希望这不起作用,因为它需要服务构建器映射到 Oracle,尽管它们不包含在 CE 中。

您总是可以使用纯 JDBC(例如没有服务构建器),或者尝试 Antonio 的插件。目前我只知道它是 7.0 的,而不是 7.1 的(但我在这里可能错了)

于 2018-08-26T07:38:24.497 回答
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

于 2018-08-26T18:54:21.377 回答