Oracle Database connection from AEM as a cloud service

Description

The below steps contains the required maven dependency, OSGi service and configurations required to connect the Oracle database from AEM as a cloud service.

Resolution

To connect the oracle database from AEM as a cloud service, please follow the below 3.

  • Adding Maven dependency in pom
  • AEM Service to connect with Oracle database
  • OSGi Configuration

Adding Maven dependency in pom

Step 1: Include the below maven dependency in your projects main pom.xml

dependency
                groupIdcom.oracle.database.jdbc/groupId
                artifactIdojdbc-bom/artifactId
                version21.5.0.0/version
                typepom/type
                scopeimport/scope
            /dependency

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

dependency
            groupIdcom.oracle.database.jdbc/groupId
            artifactIdojdbc8/artifactId
        /dependency
        dependency
            groupIdcom.oracle.database.jdbc/groupId
            artifactIducp/artifactId
        /dependency
        dependency
            groupIdcom.oracle.database.xml/groupId
            artifactIdxdb/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 0

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 configuration2 in local

Step 2: Search for “JDBC Connection pool” and configuration 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 3 and convert the osgi configuration into “.cfg.json” file and add the same into your project configuration as per AEMaaCS Standard.

2 http://localhost:4502/system/console/configMgr

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

On this page