8 Replies Latest reply on Dec 6, 2018 11:48 AM by smacdonald2008

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

    kirillo71434335

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

             }

      }