0

I am thinking of implementing connection pool for DB Connections using dbcp2.

But I am using different types of custom connection classes.

How can I override the Datasource class to use dynamic custom connection classes(Say MyConnectionClass1, MyConnectionClass2, etc..) based on the input?

1 Answers1

0

What you you mean by input? It's possible you are needed same DatasourceFactory?

public DatasourceFactory {
   public Datasource instance(Object sameParameter) {
      if(// condition one for sameParameter) {
         return new MyConnectionClass1DataSource();
      } else {
         return new MyConnectionClass2DataSource();
      }
   }
}
  • Input means that it specifies if ssl required or type of database. And The databases are not the same for every request. – Vishva N Sep 16 '22 at 07:18
  • Can You give more details on this answer. How can a connection class be returned as DataSourceFactory? – Vishva N Sep 16 '22 at 08:15
  • Sorry. I made mistake. Signature of method must be (return Datasource not DataSourceFactory): `public Datasource instance(Object sameParameter) { ... }` DatasourceFactory returns Datasource and Datasource.getConnection() returns connection. – Alexander Kashpirovsky Sep 16 '22 at 08:20
  • No Issues, I just need to change the connection class **"Connection"** to **"MyConnectionClass"** for the BasicDataSource. Need knowledge on that!! – Vishva N Sep 16 '22 at 08:23
  • When I call dataSource.getConnection() I should get MyConnectionClass as return type instead of Connection. – Vishva N Sep 16 '22 at 08:24
  • Ok. But you can specify several Datasources for different databases? – Alexander Kashpirovsky Sep 16 '22 at 08:27
  • Datasources may be Mysql, Postgres, Mongodb etc.. For handling that I have planned to create multiple ConnectionPools. The only issue is using my own Connection class inside the connection pool – Vishva N Sep 16 '22 at 08:32