ccleve
ccleve

Reputation: 15779

Generic way to get pooled JDBC DataSource?

I'm trying to get a pooled JDBC DataSource in my application. I'm writing an open-source library, and want to make configuration as, well, configurable as possible. I'd rather not use JNDI or force the use of any particular type of configuration file.

Right now, I do this:

MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
ds.setDatabaseName("blah");
ds.setUrl("jdbc:mysql://blaggablah");
ds.setUser("jimbob");
ds.setPassword("dweezil");

Obviously not ideal if you want to use something other than MySQL in the future.

Is there some kind of generic PooledDataSourceFactory that will take a few parameters, like database type and connection info, and give me a pooled DataSource?

Upvotes: 2

Views: 849

Answers (2)

Enno Shioji
Enno Shioji

Reputation: 26882

I'm not sure what kind of application (library?) you are creating, but unless it's a web app. stack or something, you could ask the client programmer to pass-in the datasource, e.g. via the constructor. This is really the gist of dependency injection.

If you app. is e.g. a web app. stack like Django, you can still make it so that you internally pass a datasource to your middleware (a.k.a DI). Such design facilitates modularity, and makes it easy to use that part of code in other projects. However, you'll have to go from configuration to DataSource somehow, so at some point you need to decide what the mechanism for that will be (e.g. JNDI). I think that's perfectly acceptable.

Upvotes: 1

Zagrev
Zagrev

Reputation: 2020

You can use a standard pooling/caching library and let the use configure it. Like Apache Jakarta Pool, or ehcache?

Upvotes: 0

Related Questions