I'm trying to implement some kind of microservice archetecture using OSGI. I have UserDAO bundle, which get Connection from Connection Pool in another bundle (Connector). They're binded with OSGI service API. So questions is:
1) Should I register interface(IConnector) as a service or implementation of this interface(Connector):
serviceRegistration = bundleContext.registerService(IConnector.class.getName(), new Connector(), null);
VS
serviceRegistration = bundleContext.registerService(Connector.class.getName(), new Connector(), null);
Both of this variants work fine. If I register IConnector (interface) I just track it:
userDBConnectorTracker = new ServiceTracker(context, IConnector.class.getName(), this);
Same way if I register Connector(implementation of IConnector):
userDBConnectorTracker = new ServiceTracker(context, Connector.class.getName(), this);
2) I want use DBCP or HikariCP, but all of examples with them uses static methods without interface, like this:
public class HikariCPDataSource {
private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
static {
config.setJdbcUrl("jdbc:h2:mem:test");
config.setUsername("user");
config.setPassword("password");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds = new HikariDataSource(config);
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
private HikariCPDataSource(){}
}
I've tried this. But then I need to change my interface's method to static too, as a methods of implementation:
I can't use this, because, if I register Interface of my Connector class as a service, it will cause an error "static method in interface is not allowed".
Should I just remove static from all methods in Connector class? Why everyone uses static in connection pool?