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.
Solved! Go to Solution.
You are correct!!! Just reference the different configured names in the API.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
"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");
Views
Replies
Total Likes
See this article for more information -
https://helpx.adobe.com/experience-manager/using/datasourcepool.html
Views
Replies
Total Likes
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.
You are correct!!! Just reference the different configured names in the API.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies