简体   繁体   English

数据库连接池数据结构

[英]Database connection pooling datastructure

I would like to develop database connection pooling. 我想开发数据库连接池。

Could anyone please tell me about which data structure need to use to maintain the pool ? 有谁能告诉我需要使用哪种数据结构来维护池?

It should be implemented using Object Pool desing pattern . 它应该使用Object Pool desing模式实现 You can read about it in Kircher, Michael; 你可以在Kircher,Michael看到它; Prashant Jain; Prashant Jain; (2002-07-04). (2002年7月4日)。 "Pooling Pattern". “汇集模式”。 EuroPLoP 2002. Retrieved on 2007-06-09. EuroPLoP 2002.检索2007-06-09。 or in Object Pool Design Pattern . 或者在对象池设计模式中 Java implementation for ObjectPool and JDBCConnectionPool classes can be found here . 可以在此处找到ObjectPoolJDBCConnectionPool类的Java实现。

Object Pool is usually a singleton with two collections of objects (eg database connections) inside: 对象池通常是一个包含两个对象集合 (例如数据库连接)的单例

  1. unlocked - for free objects, which can be provided to the client by request 解锁 - 免费对象,可以通过请求提供给客户端
  2. locked - for locked objects, which are in use now 已锁定 - 对于现在正在使用的锁定对象

This collections can be implemented as Lists or HashTables or something else, depends on your needs. 此集合可以实现为ListsHashTables或其他内容,具体取决于您的需求。 For simple ObjectPool - LinkedList structure will be good enough. 对于简单的ObjectPool - LinkedList结构就足够了。

而不是开发自己的,为什么不使用像Commons DBCP这样广泛使用和经过充分测试的库。

You generally need: 您通常需要:

  • some kind of wrapper around the "raw" Connection objects to manage things like when the connection was last given out, diagnostic information, possibly a cache of your prepared statements for that connection etc-- you define this to include what you need 围绕“原始”Connection对象的某种包装来管理诸如最后一次发出连接时的内容,诊断信息,可能是为该连接准备好的语句的缓存等 - 你定义它包含你需要的东西
  • a collection to put the connection wrappers in that supports concurrent additions/removals-- any properly synchronized list would do, but a ConcurrentLinkedQueue would be a reasonable choice 用于放置连接包装器的集合,支持并发添加/删除 - 任何正确同步的列表都可以,但ConcurrentLinkedQueue将是一个合理的选择
  • a way to manage allocations from the pool-- consider using the Semaphore class 一种管理池中分配的方法 - 考虑使用Semaphore
  • possibly, grouping various pools togther into some "pool management class" (eg so you can just call "getConnection(databaseName, readOnly)" and it goes to the relevant pool) 可能,将各个池分组到一些“池管理类”(例如,所以你可以只调用“getConnection(databaseName,readOnly)”,然后进入相关的池)

On top of this, you can then build whatever logging/monitoring you require. 最重要的是,您可以构建所需的任何日志记录/监视。

There are also advocates for off-the-shelf connection pool frameworks. 还有提供现成的连接池框架的倡导者。 I know there are those who disagree, but I personally wouldn't go down this route-- a connection pool (a) really isn't difficult to write, (b) forms a key part of your system that you probably need to understand and customise. 我知道有些人不同意,但我个人不会走这条路 - 连接池(a)真的不难写,(b)构成你可能需要理解的系统的关键部分并定制。

Is an open source solution what you're looking for? 您正在寻找的开源解决方案是什么?

See Apache Database Connection Pool API 请参阅Apache数据库连接池API

See this Sun Developer Tutorial: Connection Pooling : 请参阅此Sun Developer教程: 连接池

In releases prior to JDBC 2.0 every database session requires a new connection and login even if the previous connection and login used the same table and user account. 在JDBC 2.0之前的版本中,即使先前的连接和登录使用相同的表和用户帐户,每个数据库会话也需要新的连接和登录。 If you are using a JDBC release prior to 2.0 and want to improve performance, you can cache JDBC connections instead. 如果您使用的是2.0之前的JDBC版本并希望提高性能,则可以缓存JDBC连接。

Not sure it's wise to create your own. 不确定创建自己的明智之举。

I've used c3p0 connection pooling (with Hibernate ) across many projects with great success 我已经在许多项目中使用了c3p0连接池(使用Hibernate )并取得了巨大的成功

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

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