No data source found with name (after asking 0 providers) | Community
Skip to main content
kirill_techZone
Level 2
December 6, 2018
Solved

No data source found with name (after asking 0 providers)

  • December 6, 2018
  • 8 replies
  • 5897 views

Hello.

I made everything needed that is described in this article: Adobe Experience Manager Help | Querying MySQL data using an Adobe Experience Manager 6.4 DataSourcePool

I can successfuly get data source instance only after component activation. If I do it during the activation I get this error message: "No data source found with name 'MyDatabase' (after asking 0 providers)".

Here below is the source code. I have no errors getting dataSource if I only call getConnection() method. Activation method is problematic.

I use Adobe Experience Manager (6.2.0.SP1-CFP16) version.

Could I get some assistance? Thanks in advance.

================== THE SOURCE CODE ==================

DataBaseComponent.java:

package test.components;

import java.sql.Connection;

public interface DataBaseComponent {

       Connection getConnection();
}

DataBaseComponentImpl.java:

package test.components.impl;

import org.osgi.service.component.annotations.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.Connection;

import com.day.commons.datasource.poolservice.DataSourcePool;
import com.day.commons.datasource.poolservice.DataSourceNotFoundException;

import test.components.DataBaseComponent;

@Component (

   immediate = true,
   scope = ServiceScope.SINGLETON
)

public class DataBaseComponentImpl implements DataBaseComponent {

        private static final String DATA_SOURCE_NAME = "MyDatabase";

        @Reference
        private DataSourcePool dataSourcePool;
        private DataSource dataSource;

        private Logger logger = LoggerFactory.getLogger(getClass());

        public Connection getConnection() {

              Connection con = null;
              if (dataSource != null) {

                  try {

                       con = dataSource.getConnection();
                       return con;
                   } catch (Exception e) {

                       logger.error(e.getMessage());
                  }

             } else {

                  try {

                       dataSource = (DataSource) dataSourcePool.getDataSource(DATA_SOURCE_NAME);
                       con = dataSource.getConnection();
                       logger.info("dataSource in instantiated ...");
                  } catch (Exception e) {

                       logger.error(e.getMessage());
                  }

                  return con;
             }

             return null;
        }

        @Activate
        public void activate() {

              try {

                       dataSource = (DataSource) dataSourcePool.getDataSource(DATA_SOURCE_NAME);
                       logger.info("activation step: dataSource in instantiated ...");
             } catch(DataSourceNotFoundException e) {

                       logger.error(e.getMessage());
              }

       }

}

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 smacdonald2008

As shown in the vidoe - you need the driver file bundle and OSGi bundle - both in Active state.

8 replies

smacdonald2008
Level 10
December 6, 2018

In the article - we have this that works --

@Component

public class EmployeeImpl implements EmployeeInter {

I will show all working in a video

Just noticed that you said you have 6.2. This article is for 6.4.

Ratna_Kumar
Level 10
December 6, 2018

Hi,

I will test that article and post back my findings.

But all HELPX articles are tested by many community people and it always works!!

Thanks,

Ratna Kumar.

arunpatidar
Community Advisor
Community Advisor
December 6, 2018

I tested your code in 6.3, working fine for me. I am able to instantiated dataSource inside activate method.

you can find my code and invoking servlet at

DSConnectionImplWA.java

DSConnectionWA.java

TestDataBaseConnectionSevlet.java

Servlet output

Logs

Arun Patidar
arunpatidar
Community Advisor
Community Advisor
December 6, 2018

Hi Ratna,

That article should be fine because there is no Activate method to instantiated dataSource

Arun Patidar
smacdonald2008
Level 10
December 6, 2018

I just tested and it works as is (a big part of AEM HELPX community articles is we test them regularly to ensure they work).

A video will be posted here and in Article too.

kirill_techZone
Level 2
December 6, 2018

Thank you all for your answers. I supply PostreSQL driver by the same OSGi bundle. And at the time when my bundle gets reinstalled the JDBC driver does so too. Can it be the case?

smacdonald2008
Level 10
December 6, 2018

Here is the video that shows it all working....

We will also put this into the article too to show it all working...

smacdonald2008
smacdonald2008Accepted solution
Level 10
December 6, 2018

As shown in the vidoe - you need the driver file bundle and OSGi bundle - both in Active state.