Expand my Community achievements bar.

SOLVED

AEM having connection to more than DB connection

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

View solution in original post

6 Replies

Avatar

Level 10

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

Avatar

Level 3

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.

Avatar

Level 10

"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");

Avatar

Level 3

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.

Avatar

Correct answer by
Level 10

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