DataSource Connection Pool - NPE | Community
Skip to main content
Level 4
August 25, 2016
Solved

DataSource Connection Pool - NPE

  • August 25, 2016
  • 3 replies
  • 1162 views

I must be doing something wrong, but I'm having trouble figuring it out ......

I'm trying to get a connection to a db connection pool but I get a Null Pointer Exception on the DataSourcePool.  

Here is the code:

@Component
@Service(value = DatabaseConnectionImpl.class)
public class DatabaseConnectionImpl {

    protected static final Logger log = LoggerFactory.getLogger(DatabaseConnectionImpl.class);

    @Reference
    private DataSourcePool source;

    public Connection getDataBaseConnection(String dataSourceName) {
    Connection conn = null;
    try {

        // SOURCE IS NULL

        DataSource dataSource = (DataSource) source.getDataSource(dataSourceName);

        // SOURCE IS NULL

        conn = dataSource.getConnection();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
    }

    public void executeQuery() {
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        con = getDataBaseConnection("connectionDataSource");
        stmt = con.createStatement();
        rs = stmt.executeQuery("select abc from table1");
        while (rs.next()) {
        System.out.println("Jurisdiction=" + rs.getString(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
        if (rs != null)
            rs.close();
        if (stmt != null)
            stmt.close();
        if (con != null)
            con.close();
        } catch (Exception e) {
        e.printStackTrace();
        }
    }
    }

}

Any advice is appreciated.  

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

It looks like you did not configure the DataSourcePool on the AEM Configuratiohn view. 

See this article - it steps you through this use case: 

https://helpx.adobe.com/experience-manager/using/datasourcepool.html

We also have a HELPX Video so you can watch all of the steps and a working AEM sample (video in table at start of the article)

https://www.youtube.com/watch?v=yYo1epoZp_4&feature=youtu.be

Hope these help you. 

3 replies

smacdonald2008
smacdonald2008Accepted solution
Level 10
August 25, 2016

It looks like you did not configure the DataSourcePool on the AEM Configuratiohn view. 

See this article - it steps you through this use case: 

https://helpx.adobe.com/experience-manager/using/datasourcepool.html

We also have a HELPX Video so you can watch all of the steps and a working AEM sample (video in table at start of the article)

https://www.youtube.com/watch?v=yYo1epoZp_4&feature=youtu.be

Hope these help you. 

kautuk_sahni
Community Manager
Community Manager
August 25, 2016

Hi 

Please have a look at the the Helpx Article,

Link:-  https://helpx.adobe.com/in/experience-manager/using/datasourcepool.html

// Injecting a DataSourcePool Service into an Adobe Experience Manager OSGi bundle

This article will give you step by step approach for injecting DataSourcePool.

 

Another Link:- https://helpx.adobe.com/experience-manager/using/custom-sling-servlets1.html

//Injecting a DataSourcePool into Adobe Experience Manager Sling Servlets

 

I hope this will help you.

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
Level 4
August 25, 2016

Many Thanks to Scott MacDonald and Kautuk Sahni for all of their advice on working with Connection Pools in AEM.

The connection pool project to create the bundle is a little awkward to say the least.  However, it does now work!!!!!

Thanks again for all the hard work you put into this forum.  It is light years ahead of creating a ticket with Adobe to get these things resolved.

-Dean