How to connect the Oracle database from AEM as a cloud service?

This article provides an understanding on How to connect the Oracle database from Adobe Experience Manager (AEM) as a cloud service.

Description description

Environment

Adobe Experience Manager

Issue/Symptoms

How to connect the Oracle database from AEM as a cloud service?

Resolution resolution

Connecting the Oracle database from AEM as a cloud service involves the following steps:

  • Adding Maven dependency in pom

  • AEM Service to connect with Oracle database

  • OSGi Configuration

Adding Maven dependency in pom

Step 1: Include this maven dependency in your project’s main 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>

Step 2:  Include the below maven dependency in pom.xml of both “core” and “all”.

<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>

AEM Service to connect with Oracle database

Sample service code to connect with the database from AEM as a cloud service. It can be included in the path

path {0} » project folder » core » service

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 Configuration

Step 1: Go to AEM OSGi configuration (http://localhost:4502/system/console/configMgr) in local.

Step 2: Search for “JDBC Connection pool” and configure the below values with respect to your DB (refer to screenshot for sample values)

  • JDBC Driver class
  • JDBC Connection URL
  • Username and Password
  • Datasource name

Step 3: Follow the steps in the link below and convert the OSGi configuration into .cfg.json file and add the same into your project configuration as per AEMaaCS Standard.

https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/implementing/deploying/configuring-osgi.html?lang=en#generating-osgi-configurations-using-the-aem-sdk-quickstart

recommendation-more-help
3d58f420-19b5-47a0-a122-5c9dab55ec7f