Environment
Adobe Experience Manager
Issue/Symptoms
How to connect the Oracle database from AEM as a cloud service?
Connecting the Oracle database from AEM as a cloud service involves the following:
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 project’s 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
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)
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.