AEM having connection to more than DB connection | Community
Skip to main content
Level 3
May 13, 2016
Solved

AEM having connection to more than DB connection

  • May 13, 2016
  • 6 replies
  • 2621 views

I have a scenario to interact with more than one DB. But in this scenario, in my java class, how to get the data source, connection details for respective database from JDBC Connection Pool. Any suggestions would be helpful.

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

You are correct!!! Just reference the different configured names in the API. 

6 replies

smacdonald2008
Level 10
May 13, 2016

Are you trying to connect to multiple Databases using a Single DataSourcePool? 

 In this use case. configure multiple connections and use different DataSourcePool objects. 

ie:

Database1Config -> DataSourcePool1

Database2Config -> DataSourcePool2

// etc

Level 3
May 13, 2016

But how to get these objects. Is it via PID, i need to get the req object. One more thing, i tried by just passing different data source to data connection object it worked. Never mapped the Database1Config to any pool. Just configured 2 database details in JDBC Pool. Is it fine?

Below is code snippet

    public Connection getConnection(ConfigurationAdmin configAdmin,
            DataSourcePool dataSourceService, DataSource, dataSource) {
        LOGGER.info("In getConnection method ");
        Connection connection = null;
        String datasrc = "";
        try {
            if (ds != null) {
             connection = ds.getConnection();
            
                
            }
        } catch (Exception e) {

            LOGGER.error("Exception occured", e);
            
        }
        return connection;
        

    }

 

Can you please details on its working.

smacdonald2008
Level 10
May 13, 2016

"i tried by just passing different data source to data connection object it worked" - it worked because that is the proper way to do it.  You do not need to pass other objects - just the name of the connection. 

For example -- to get a DataSourcePool instance - you use code like this: 

//Inject the DataSourcePool right here!
  dataSource = (DataSource) source.getDataSource("Customer");
con = dataSource.getConnection();
return con;

 

Customer maps to the configuration you set in DataSourcePool connection properties.

 

You can create as many DataSoucePool connections as you want - each connecting to a different database and each with a different name!

 

 

 

(note - the red arrow should be pointing to the row above)

 

So in your code -- you would do:

dataSource = (DataSource) source.getDataSource. getDataSource("PLACE NAME OF YOUR CONNECTION HERE");

smacdonald2008
Level 10
May 13, 2016
Level 3
May 17, 2016

Thanks a lot. So to brief.

Connection details for multiple Database to be configured in JDBC pooling, and JAVA class add the Datasource name to the datasource object and get the connection. I don't have to worry about the the database url and username/password as its already taken care in JDBC pooling.

Also at the end in finally block, just close the connection. Please correct if am wrong.

smacdonald2008
smacdonald2008Accepted solution
Level 10
May 17, 2016

You are correct!!! Just reference the different configured names in the API.