简体   繁体   English

获取池JDBC DataSource的通用方法?

[英]Generic way to get pooled JDBC DataSource?

I'm trying to get a pooled JDBC DataSource in my application. 我正在尝试在我的应用程序中获取池化的JDBC数据源。 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. 我宁愿不使用JNDI也不强制使用任何特定类型的配置文件。

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. 如果将来想使用MySQL以外的其他东西,显然不理想。

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? 是否有某种通用的PooledDataSourceFactory可以接受一些参数,例如数据库类型和连接信息,并给我一个池化的DataSource?

I'm not sure what kind of application (library?) you are creating, but unless it's a web app. 我不确定您要创建哪种类型的应用程序(库?),但是除非它是一个Web应用程序。 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). 像Django这样的堆栈,您仍然可以做到这一点,以便您在内部将数据源传递给中间件(又名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). 但是,您必须以某种方式从配置转到DataSource,因此在某个时候,您需要确定其实现的机制(例如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? 像Apache Jakarta Pool还是ehcache?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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