Expand my Community achievements bar.

Issue with database connection pool

Avatar

Level 4

I have created multiple data sources through sling:OsgiConfig nodes in the run mode configuration.

11.png

I can also see these configurations in configurations manager screen.

1.png

2.png

I am trying to get database connection using below code:

@Reference

    private DataSourcePool dspService;

....

getConnection(){

     DataSource ds = (DataSource) dspService.getDataSource("abc-xyz");

.....

}

I get an error:

com.day.commons.datasource.poolservice.DataSourceNotFoundException: No data source found with name 'abc-xyz' (after asking 0 providers).

If I open the config manager and save the configurations again, connection works successfully. Why is the behavior shown ?

I tried the following solution.

I created a bundle activator and put the code to programmatically update the configurations

ServiceReference serviceRef = context

.getServiceReference(ConfigurationAdmin.class.getName());

ConfigurationAdmin configAdmin = (ConfigurationAdmin) context

.getService(serviceRef);

String filter = '(' + ConfigurationAdmin.SERVICE_FACTORYPID + '='

+ "com.day.commons.datasource.jdbcpool.JdbcPoolService"

+ ')';

Configuration[] allLoggerConfigs = configAdmin

.listConfigurations(filter);

for (Configuration conf : allLoggerConfigs) {

conf.setBundleLocation(conf.getBundleLocation());

Dictionary prop = conf.getProperties();

log.info("Initializing : " + prop.get("datasource.name"));

conf.update(prop);

But I think the above is not required and should work without the above code. Please help

13 Replies

Avatar

Level 10

Hi,

You've posted this question in our Experience Cloud community. Is there a more specific community that this question might fit better in? I'm not familiar with the connection you're trying to make but it appears that it may be related to AEM or Analytics? If this is the case, I'm happy to move the question to one of those communities where more people are likely to see it and thus provide solutions. Please let me know if you'd like me to move your question.

Thanks,
Jantzen

Avatar

Level 4

Yes Jantzen,

This question is on Adobe AEM. Please move the question to an appropriate community.

Thanks,

Prashant

Avatar

Level 10

See this article that describes how to successfully work with a DataSourcePool.

Scott's Digital Community: Injecting a DataSourcePool Service into an Adobe Experience Manager OSGi ...

You can see it all in action too with the video in the the above URL!

PS -- NOT NEEDED

I created a bundle activator and put the code to programmatically update the configurations

  1. ServiceReference serviceRef = context 
  2. .getServiceReference(ConfigurationAdmin.class.getName()); 
  3. ConfigurationAdmin configAdmin = (ConfigurationAdmin) context 
  4. .getService(serviceRef); 
  5. String filter = '(' + ConfigurationAdmin.SERVICE_FACTORYPID + '=' 
  6. + "com.day.commons.datasource.jdbcpool.JdbcPoolService" 
  7. + ')'
  8. Configuration[] allLoggerConfigs = configAdmin 
  9. .listConfigurations(filter); 
  10. for (Configuration conf : allLoggerConfigs) { 
  11. conf.setBundleLocation(conf.getBundleLocation()); 
  12. Dictionary prop = conf.getProperties(); 
  13. log.info("Initializing : " + prop.get("datasource.name")); 
  14. conf.update(prop); 

Avatar

Level 10

I am also going to update our DataSourcePool article to work with latest AEM Version - AEM 6.4.

Avatar

Level 4

Hi Scott,

I have done all these. I am able to get the database connection. The problem starts when I save the data source configuration in OSGi config nodes and push them thru maven build. At this point, I will be able to see my data source configurations in the configuration manager screen. But when I try to get the data source, I get an error mentioned above. "No data source found". This is the problem. Now when I go back to configuration admin screen and open my jdbc configuration, save it again, then my db connection starts working fine. I am using 6.1 if that is of any help.

Avatar

Level 4

Hi Prashant,

Try deleting the relevant .config/.cfg files from <cq-installation-dir>/crx-quickstart/launchpad/config/ and then do a fresh maven build. Sometimes the config files take priority and are over-written only after you save the configuration from configMgr.

Also, when we save a configuration from /system/console/configMgr, it gets saved as a nt:file instead of sling:osgiConfig, and nt:file takes priority over sling:osgiConfig nodes, try searching the repository to see if some nt:file configuration isn't overriding yours.

Third, try uploading the configuration as nt:file (instead of sling:osgiConfig) through your maven build and observe the behaviour.

Let me know what you find.

Avatar

Level 10

See the video - we do not use OSGi config nodes. We set the information in the DataSourcePool config screen as shown in both the artilce and video.

Avatar

Level 4

Hi Scott,

Thanks for your reply. Yes, I understand you are not using sling:osgiConfig nodes. You are setting up configurations directly in the osgi config manager screen. When I do this, my database connection works as expected. Now if I want database configurations to be ready after every build, I need to save them thru sling:osgiConfig nodes and push them thru build. That is when I am facing this issue. After the maven build, if I go to the config manager screen , I am able to see all my configurations for all my databases. But some how DataSourcePool is not picking them up.

Avatar

Level 10

If you want to use sling OSGi nodes to pass in info and the DataSourcePool not wirking, then i would look at using Java API to open the connection object to the database. That should work.

Avatar

Level 4

Hi Rima,

Thanks for your reply. I will try your solutions. But what I am not understanding is, when I got the above error, I suspected that my datasource sling:OSGiconfig nodes may not be pushed properly thru maven build and are being overwritten with somethign else. So, I debugged this issue and saw if my configurations are present in configuration admin service. I have ascertained this using the following code:

@Reference

ConfigurationAdmin configAdmin;

.....

...

String filter = '(' + ConfigurationAdmin.SERVICE_FACTORYPID + '='

+ "com.day.commons.datasource.jdbcpool.JdbcPoolService"

+ ')';

Configuration[] allLoggerConfigs = configAdmin

.listConfigurations(filter);

for (Configuration conf : allLoggerConfigs) {

Dictionary prop = conf.getProperties();

log.info("Configs " + prop.get("datasource.name"));

}

Surprisingly, I am able to see that all my database configurations which I have pushed thru maven build using sling:osgiConfig nodes are present. I think this shows that, none of the other config's or nt:file are overriding my config's which are pushed thru maven build.

So the problem here is, all data source configurations are present, but they are simply not being made available to the "com.day.commons.datasource.poolservice.DataSourcePool" service.

One more point here to note is that:

We configure data sources for this service : com.day.commons.datasource.jdbcpool.JdbcPoolService

We try to connect to database thru this service : com.day.commons.datasource.poolservice.DataSourcePool

Since the service where properties are configured, and the service which is using them are different, this problem may be happening. When the sling:osgi config nodes are installed, JdbcPoolService is aware of this event but not DataSourcePool. If there is some we can make DataSourcePool aware of the existance of data source configuration, then I think it would solve the issue.

Avatar

Level 4

I understand, thanks for explaining this so well.

There is one more thing, if the config folder is deeper in the folder tree than four levels of depth, then the configs don't work. Can you please check?

Thanks

Rima Mittal

Avatar

Level 4

Hi Rima,

My sling:osgiConfig is present in the below hierarchy:

/apps/<project_name>/<run_mode>/osgi/<module_name>/<brand>/

Could this be the reason for this issue ? I will try moving my configurations a couple of level up and try DB connection after that. I will update this thread with my findings.

Thanks a lot.

Avatar

Level 2

@prashantonkar   were you able to resolve this issue ? I am facing the same issue and none of the solutions worked. If you can share your thoughts would be helpful.