简体   繁体   中英

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?

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, eg via the constructor. This is really the gist of dependency injection.

If you app. is eg a web app. stack like Django, you can still make it so that you internally pass a datasource to your middleware (aka 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 (eg JNDI). I think that's perfectly acceptable.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM