如何从AEM as a Cloud Service连接Oracle数据库?
本文介绍了如何从Adobe Experience Manager (AEM) as a Cloud Service连接Oracle数据库。
描述 description
环境
Adobe Experience Manager
问题/症状
如何从AEM as a Cloud Service连接Oracle数据库?
解决方法 resolution
从AEM as a Cloud Service连接Oracle数据库涉及以下内容:
-
在pom中添加Maven依赖项
-
用于连接Oracle数据库的AEM服务
-
OSGi配置
在pom中添加Maven依赖项
第1步: 在项目的主文件中包含以下maven依赖项 pom.xml
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc-bom</artifactId>
<version>21.5.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
第2步: 在中包含以下maven依赖项 pom.xml
“核心”和“全部”的组合。
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ucp</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.xml</groupId>
<artifactId>xdb</artifactId>
</dependency>
用于连接Oracle数据库的AEM服务
用于从AEM as a Cloud Service连接到数据库的示例服务代码。 它可以包含在路径{0}中
路径{0} "项目文件夹"核心"服务
DatabaseService.java
package com.mysite.core.services;
public interface DatabaseService {
}
DatabaseServiceImpl.java
package com.mysite.core.services;
import com.day.commons.datasource.poolservice.DataSourcePool;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
@Component(
service = DatabaseService.class,
immediate = true
)
public class DatabaseServiceImpl implements DatabaseService {
private final Logger LOGGER = LoggerFactory.getLogger(DatabaseService.class);
@Reference
private DataSourcePool dataSourcePool;
@Activate
public void activate() {
try {
DataSource dataSource = (DataSource) dataSourcePool.getDataSource("oracle");
Connection connection = dataSource.getConnection();
if (connection != null) {
if (!connection.isClosed()) {
LOGGER.info("Connected with connection #4");
connection.close();
}
}
else {
LOGGER.info("Connection is null");
}
} catch (Exception ex) {
LOGGER.error("It was not possible to get the data source: " + ex.getMessage(), ex);
}
}
}
OSGi配置
第1步: 转到本地的AEM OSGi配置(http://localhost:4502/system/console/configMgr)。
第2步: 搜索“JDBC连接池”并配置与数据库相关的以下值(有关示例值,请参阅屏幕快照)
- JDBC驱动程序类
- JDBC连接URL
- 用户名和密码
- 数据源名称
步骤3: 按照以下链接中的步骤操作,并将OSGi配置转换为 .cfg.json
按照AEMaaCS标准将该文件文件添加到项目配置中。
recommendation-more-help
3d58f420-19b5-47a0-a122-5c9dab55ec7f