legacy-knowledge-base
公開されました Sep. 10, 2025

JNDIを使用した外部データソースの確立

written-by

Kanchan Bisht

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

legacy-article

learn-legacy-article-disclaimer-text

問題

  • Liferay DXP以外のデータソースを使用することが要求されることもあります。 これを行うには、JNDIデータソースまたはJDBCプロパティによってデータソースを設定することができます。
  • Liferay DXPのデータベース以外のものを外部データベースと呼びます。 本書で説明する手順は、JNDIを使用して外部データベースを設定するためのものです。

Environment

  • Liferay DXP 7.3
  • MySQLデータベース

解決策

  • 次のような手順で行います:

    (1) MySQLコマンドでデータベースを作成します:

    create database external character set utf8;
    use external;
    create table external_Sample (
    name VARCHAR(75) null,
    id_ bigint not null primary key
    );

    (2) tomcat/conf の server.xml に以下のように JNDI Resource エントリを設定する。

    <GlobalNamingResources>
    <Resource
    name="jdbc/externalDataSource"
    auth="Container"
    type="javax.sql.DataSource"
    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
    driverClassName="com.mysql.cj.jdbc.Driver"
    url="jdbc:mysql://localhost/external?characterEncoding=UTF-8"
    username="root"
    password="root"
    maxActive="20"
    maxIdle="5"
    maxWait="10000"
    />
    </GlobalNamingResources>

    (3) tomcat/confのcontext.xmlファイルにCreate Resourceのリンクがあります。

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

    (4) Liferay DXP インスタンスの [LIFERAY_HOME]/osgi/log4 フォルダに com.liferay.blade.samples.jndiservicebuilder.service-log4j-ext.xml を作成します。 このフォルダーがまだ存在しない場合は、作成します。 この内容を、作成したXMLファイルに追加します。

    <?xml version="1.0"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <category name="com.liferay.blade.samples.jndiservicebuilder.service.impl">
    <priority value="INFO" />
    </category>
    </log4j:configuration>

    (5) サーバを再起動します。
    (6) 添付のjarを一つずつデプロイする(API、Service、Webポートレットの3つのjarが添付されているサンプルプラグイン)
    (7) サンプルオプションから 'Hello from NewExternalDbSbWeb' ポートレットをページ上にドラッグjndi-web-module-image.png
    (8) 名前を記入
    (9) 外部データベースにexternal_sampleテーブルエントリーがないか確認します。
    期待される動作 対象の外部データベースのテーブルに名前が保存されます。

追加情報

  • LDS version:3.9.5.202112170330-ga6
  • サービスビルダーモジュールプロジェクトを作成し、サービスビルダーモジュールの service.xml ファイルで、エンティティのデータソースを liferayDataSourceに設定します。 上記のサンプルプラグインをテストに使用しない場合は、サービスモジュールの src/main/resources/META-INF/spring フォルダ または従来のポートレットの WEB-INF/src/META-INF フォルダに親コンテキスト拡張(ext-spring.xmlなど)を作成する必要があります。 このファイルは、すでに存在するliferayDataSourceを上書きするために使用されます。

    <?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="custom." />
    <property name="properties">
    <props>
    <prop key="custom.jndi.name">jdbc/externalDataSource</prop>
    </props>
    </property>
    </bean>
    <!-- The data source bean refers to the factory to access the data source. -->
    <bean
    class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
    id="liferayDataSource">
    <property name="targetDataSource"
    ref="liferayDataSourceFactory" />
    </bean>

    <!-- In service.xml, we associated our entity with the extDataSource. To
    associate the extDataSource with our overridden liferayDataSource, we define
    this alias. -->
    <alias alias="extDataSource" name="liferayDataSource" />
    </beans>
  • サーバーを起動する前に、 {Liferay-Home}/tomcat/lib/ext に mysql.jar を配置してください。
  • LiferayのService Builderが外部データソースのテーブルを生成できない。
    外部データベースのテーブルを手動で作成する必要があります。 ドキュメントにあるように、サービスビルダーはLiferayの内部テーブルのみを管理します。 一方、ServiceBuilderは、外部データベースのテーブルやインデックスなどを管理することはない。 しかし、ここで提供するPOCは7.3で動作し、ユーザーインターフェースから外部のMySQLデータベースに値を挿入することができます。
  • JDBCを使用した外部データソースの確立
did-this-article-resolve-your-issue

legacy-knowledge-base