Expand my Community achievements bar.

SOLVED

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

Avatar

Level 1

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 DataSource...

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());
              }

       }

}

1640660_pastedImage_15.png

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

View solution in original post

8 Replies

Avatar

Level 10

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.

Avatar

Level 10

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.

Avatar

Community Advisor

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

Screenshot 2018-12-06 at 11.55.48 PM.png

Logs

Screenshot 2018-12-06 at 11.55.42 PM.png



Arun Patidar

Avatar

Community Advisor

Hi Ratna,

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



Arun Patidar

Avatar

Level 10

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.

Avatar

Level 1

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?

Avatar

Level 10

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

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

Avatar

Correct answer by
Level 10

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