DataSourcePool in WorkflowProcess | Community
Skip to main content
crich2784
Level 6
May 26, 2021
Solved

DataSourcePool in WorkflowProcess

  • May 26, 2021
  • 1 reply
  • 710 views

I am trying to dynamically connect to a datasource.  I'm using the AEM archetype.

 

public class mystuff implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(mystuff.class);
@3214626
private DataSourcePool dsp;

@9944223
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {

String dataSourceName = "MYDB";
DataSource ds = null;
Connection connection = null;
try {

if (dsp == null ) {
   log.info("DataSourcePool is null");
} else {
   for(Iterator<String> it = dsp.getAllJndiDataSourceNames().iterator(); it.hasNext();) {
      String dsname = it.next();
       log.info("Found datasource " + dsname);
    }
     log.info("Using DataSourcePool service lookup to get connection pool " + dataSourceName);
     ds = (DataSource) dsp.getDataSource(dataSourceName);

 

 

No datasources are being found and no connection made.  

I have the MYDB configured just fine.  I am using it in a Servlet by doing this annotation.

 

@3214626(target = "(&(objectclass=javax.sql.DataSource)(datasource.name=MYDB))")
private DataSource ds;

 

However, i do not want to hard-code the datasource.name.  Any ideas!!??

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

Hi @crich2784 

 

Did you create the OSGi config with com.day.commons.datasource.jdbcpool.JdbcPoolService and added the DB details into it?

The JDBC Connections Pool service is a factory. Therefore, if you use a sling:OsgiConfig node to configure the connection service, the name of the node must include the factory service PID followed by -alias. The alias that you use must be unique for all configuration nodess for that PID. An example node name is com.day.commons.datasource.jdbcpool.JdbcPoolService-mydb

Please have a look here:

https://experienceleague.adobe.com/docs/experience-manager-64/developing/platform/jdbc.html?lang=en

Thanks!

1 reply

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
May 27, 2021

Hi @crich2784 

 

Did you create the OSGi config with com.day.commons.datasource.jdbcpool.JdbcPoolService and added the DB details into it?

The JDBC Connections Pool service is a factory. Therefore, if you use a sling:OsgiConfig node to configure the connection service, the name of the node must include the factory service PID followed by -alias. The alias that you use must be unique for all configuration nodess for that PID. An example node name is com.day.commons.datasource.jdbcpool.JdbcPoolService-mydb

Please have a look here:

https://experienceleague.adobe.com/docs/experience-manager-64/developing/platform/jdbc.html?lang=en

Thanks!