数据库连接池(带 Java)

发布于 2024-12-17 02:38:51 字数 484 浏览 0 评论 0原文

我想知道您对这种方法是否可以实际实施的想法。我对数据库池非常了解,所以请原谅我的任何误解。我有多个类调用 dbData 类(它连接到数据库并具有辅助函数,例如经常使用获取和更新等)。我需要将所有获取和更新函数保留在 dbData 类中,但保留一个可以重复使用的活动连接池。这意味着我将多次实例化 dbData 类,每次都检查是否存在适当的连接,如果不存在,则创建一个新连接并将其放入池中。

我的问题是如何以及在哪里存储这个池。如果 dbData 不会被实例化多次并保留一个持久池对象,我也许可以完成此任务。但就我而言,我有多个 dbData 实例,它们都应该连接到单个池。我想过序列化池对象,但这看起来很荒谬。这可能吗(如图所示)?看来我在这个设计的面向对象部分遇到了麻烦。

应用程序使用 Class1 和 Class2 的多线程。

如果可能的话,我不想使用任何外部库。

数据库池 img

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). I need to keep all the get and update functions in dbData class, but keep a pool of active connections I can re-use. 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.

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. But in my case, I have multiple dbData instances which should all connect to a single pool. 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.

I would not like to use any external libraries if possible.

db pool img

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-12-24 02:38:51

如果它是一个独立的应用程序,那么我将创建一个带有静态集合的单独服务,该集合保持连接并完成所有这些处理。然后dbData类可以调用静态方法来获取连接。然后,如果需要,该服务本身会负责创建新连接。如果您的 dbData 实例并行运行,您必须考虑同步访问(如果需要)。

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. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文