javaosgiconnection-poolinghikaricpapache-commons-dbcp

Connection Pool for OSGI bundle


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: enter image description here

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".

enter image description here

Should I just remove static from all methods in Connector class? Why everyone uses static in connection pool?


Solution

  • For an OSGi service you generally should use an interface to announce it. This way the user of the service is not bound to your implementation class.

    Using a static method to return a connection is generally a bad idea in OSGi. You will want to supply the configuration of the datasource using an OSGi configuration. These configurations are supplied at runtime and can even go away at runtime. So you need to implement this in a way to cope with a configuration or configuration change.

    A good practice is to offer a factory for the connection as a service once the configuration is given to you by the configiration admin and remove the service when the config goes away.

    Doing all this with plain OSGi APIs is very cumbersome. Try to avoid registering services manually as well as using service trackers. Both APIs are difficult to use correctly. Instead use a dependency injection framework like declarative services.

    Your case of managing DataSources and pools is especially complicated. You should not do this yourself. There are two existing projects that already provide what you need.

    As your question suggests you try to build microservices on OSGi I also recommend the enroute microservice tutorial. It includes an example of using transaction control.