0

如果我有Liferay 7.2 社区版并且想使用Oracle 12c作为我的业务数据的外部数据库,那么使用JNDI连接的最简单和最好的方法是什么?我可以在 Tomcat 中创建DataSource并使用 JNDI 查找来连接 Liferay Service Builder吗?

4

1 回答 1

1

这是我识别和使用的一种简单方法:

步骤(1):根据现有的外部数据库将实体定义/映射到 service.xml 中。如果表不存在,则手动创建所有表和字段,因为 Liferay 服务生成器不会生成 SQL 代码来自动在外部数据库中创建表。如果要使用命名空间,请在实体下方手动映射并在数据库中定义相同的内容。

服务.xml

<entity local-service="true" name="Employee" table="employee" data-source="extDataSource" remote-service="false" uuid="false">
        <column name="employeeId" db-name="employeeid" primary="true" type="long" />
        <column name="groupId" db-name="groupid" type="long" />
        <column name="userName" db-name="username" type="String" />
    </entity>
</service-builder>

Step (2) : 在LIFERAY-HOME\tomcat-9.0.17\conf\server.xml 下放入以下内容

<Resource
    name="jdbc/myDataSource"
    auth="Container"
    type="javax.sql.DataSource"
    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    url="jdbc:oracle:thin:@localhost:1521:orcl"
    username="USERNAME"
    password="PASSWORD"
    maxActive="20"
    maxIdle="5"
    maxWait="10000"
/>

Step (3) : 在Context下的 LIFERAY-HOME\tomcat-9.0.17\conf\context.xml 中加入以下内容

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

第 (4) 步:要连接 Oracle 或任何专有数据库,需要以下 2 个 JAR:

  • Oracle 驱动:获取驱动并放在 liferay-ce-portal-7.2.1-ga2\tomcat-9.0.17\lib\ext 下
  • 外部数据库支持库:下载最新的

Liferay 门户数据库多合一支持 JAR

来自 Maven 存储库

https://mvnrepository.com/artifact/it.dontesta.labs.liferay.portal.db/liferay-portal-database-all-in-one-support/1.2.1

下载 JAR liferay-portal-database-all-in-one-support-1.2.1.jar并放在 LIFERAY-HOME\tomcat-9.0.17\webapps\ROOT\WEB-INF\lib 下

步骤(5) :在portal-ext.properties中定义以下内容

jdbc.mydb.jndi.name=jdbc/myDataSource

步骤(6):在 * -service下创建一个 DataSourceProviderImpl 类:

package com.demo4.external.data.source.spi;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.liferay.portal.kernel.dao.jdbc.DataSourceFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.DataSourceProvider;
import com.liferay.portal.kernel.util.PropsUtil;

public class DataSourceProviderImpl implements DataSourceProvider {

@Override
public DataSource getDataSource() {

DataSource dataSource = null;

try {
            
dataSource = DataSourceFactoryUtil.initDataSource(PropsUtil.getProperties("jdbc.mydb.", true));

            
//  **Note:** Sometimes above line dosn't work in some environments, then follow below approach. In this case above Step(5) is not required because it's directly making lookup into server's context.


//  InitialContext initialContext = new InitialContext();
//  dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/myDataSource");


        } catch (Exception e) {
            e.printStackTrace();
        }

        return dataSource;
    }
}

步骤(7) :向JDK SPI(服务提供者接口)注册上述实现。为此,请在 * -service下创建以下文件夹/文件

META-INF/services/com.liferay.portal.kernel.dao.jdbc.DataSourceProvider

在此文件下放入以下行:

com.demo4.external.data.source.spi.DataSourceProviderImpl

全部做完。只需构建服务,进行 Gradle 刷新并启动服务器。这将完美地工作。

快乐学习!

于 2020-12-13T07:49:17.703 回答