简体   繁体   English

数据库连接池(带Java)

[英]Database Connection Pooling (w/ Java)

I would like to know your thoughts on whether this type of methodology could be practically implemented. 我想知道您对这种方法是否可以实际实施的想法。 I am very knew to database pooling, so forgive me for any misunderstandings. 我对数据库池非常了解,所以请原谅我的任何误解。 I have multiple classes calling a dbData class (which connects to a database and has helper functions, like frequently uses gets and updates and etc). 我有多个调用dbData类的类(该类连接到数据库并具有帮助函数,例如经常使用的gets和update等)。 I need to keep all the get and update functions in dbData class, but keep a pool of active connections I can re-use. 我需要将所有get和update函数保留在dbData类中,但要保留一个我可以重用的活动连接池。 Which means I will instantiate the dbData class multiple times, each time checking whether an appropriate connection exists, if not, create a new one and put it into the pool. 这意味着我将多次实例化dbData类,每次检查是否存在适当的连接,如果不存在,则创建一个新连接并将其放入池中。

My question is this how and where you would store this pool. 我的问题是,如何以及在何处存储此池。 I could perhaps accomplish this if dbData would not be instantiated more than once and keeps one persistent pool object. 如果dbData不会被多次实例化并保留一个持久性池对象,我也许可以完成此操作。 But in my case, I have multiple dbData instances which should all connect to a single pool. 但就我而言,我有多个dbData实例,这些实例都应连接到单个池。 I thought about serializing the pool object, but this seems ridiculous. 我曾考虑过将池对象序列化,但这似乎很荒谬。 Is this possible (what is shown in the pic)? 这可能吗(图片中显示了什么)? It seems I am having trouble with the object-oriented part of this design. 看来我在设计的面向对象部分遇到了麻烦。

The applications uses multithreading with Class1 and Class2. 应用程序对Class1和Class2使用多线程。

I would not like to use any external libraries if possible. 如果可能,我不想使用任何外部库。

数据库池img

If it is a standalone app, then I would create a seperate service with a static collection that keeps the connection and does all the handling of those. 如果它是一个独立的应用程序,那么我将使用静态集合创建一个单独的服务,该集合可保持连接并进行所有处理。 Then the dbData class can call the static method to get a connection. 然后dbData类可以调用静态方法来获得连接。 The service then itself takes care of creating new connections if required. 然后,如果需要,服务本身负责创建新的连接。 If your dbData instances are running in parallel you have to think about synchronized access (if required). 如果dbData实例并行运行,则必须考虑同步访问(如果需要)。

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

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