对象池:howto

发布于 2024-08-21 10:16:07 字数 418 浏览 6 评论 0原文

我需要实现一个由外部系统返回的会话池, 这样我就可以在需要时快速重用它们(创建会话需要一段时间)。 我使用数据源创建了一个数据库连接池(来自 Apache 的 DBCP),它是 已实施的解决方案。

在一般情况下,我们使用什么来池化任意对象,是否有实现的解决方案(即对象,而不是接口)来痛苦地处理任务?

第二个问题是,我们如何测试会话是否还活着?是否有我们在对象池中重写的特定方法来查询会话自己的方法?

第三个非常重要的问题是,对象池对象应该是静态的吗?我从系统中提取的一组对象必须在不同的 Web 应用程序之间共享。比如说,我们提取 5 个会话。应用程序 A 查询 POOL 并获取第一个可用的会话。现在还剩 4 节课。另一个应用程序 B 启动并查询 THE SAME POOL。等 游泳池是共用的。在同一台计算机上运行的同一 Web 应用程序的不同实例之间。

I need to implement a pool of Sessions that are returned by an external system,
so that I can reuse them quickly as soon as one is needed (creating a Session takes a while).
I've worked with datasource to create a pool of database connections (DBCP from Apache), and it was
an implemented solution.

What do we use in a general case to pool arbitrary objects, and are there implemented solutions, ie objects, not interfaces, to deal with the task painfully?

Second question would be, how do we test whether the Session is alive ? Is there a specific method that we override in the Object pool, that queries the Session's own methods?

The third, VERY IMPORTANT question, would be, should that object pooling object be static? A bundle of objects I extract from the system must be shared among different web applications. So, say, we extract 5 Sessions. App A queries the POOL and gets the first available Session. Now there are 4 Sessions left. Another App B starts and queries THE SAME POOL. etc The pool is shared. Among different instances of the same web app, running on the same machine.

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

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

发布评论

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

评论(2

萌辣 2024-08-28 10:16:07
  1. 对于通用对象池,
    您有一个 Apache Commons
    项目
    为此。
  2. 用于测试
    一个会话还活着,有
    不同的方式,但其中很多都是
    不可靠。而且可靠的一个
    (在 dual 上进行查询)速度很慢。
    你可以看看c3p0
    它内置了该功能。
  3. 只要您的许多 Web 应用程序位于同一个 WAR 文件中,我认为您就可以使用这个静态池对象。尽管就我个人而言,我更喜欢单例,而不是静态对象,因为静态对象除了实用方法和常量之外还有其他东西。

总的来说,我是 Hibernate 的忠实粉丝...您是否考虑过将它用于您的应用程序?您仍然可以通过它进行简单的 SQL 查询,并且它会为您处理池化和缓存。

  1. For a generic pool of objects,
    you have an Apache Commons
    project
    for that.
  2. For testing
    that a session is alive, there are
    different ways, but many of them are
    unreliable. And the reliable one
    (doing a query on dual) is slow.
    You can have a look at c3p0,
    which has that feature built-in.
  3. As long as your many webapps are in the same WAR file, I think you'd be OK to use this static pool object. Although, personally, I prefer singletons over static objects that have anything more than utility methods and constants.

In general, I'm a big fan of Hibernate... have you considered using it for your application? You can still make plain SQL queries through it, and it handles your pooling and caching for you.

迷途知返 2024-08-28 10:16:07

如果您使用的是 J2EE 应用程序服务器,请考虑构建一个实现 Java 连接器体系结构的组件 (JCA)。该组件的每个实例都访问一个会话,并且您将容器配置为最多创建五个(来自您的示例)实例。容器管理池和组件的生命周期。此外,部署在该应用程序服务器上的所有应用程序共享该组件池。

如果我没记错的话(已经有一段时间了)还有一种方法可以向容器发出实例死亡的信号。在这种情况下,容器会删除失效实例并实例化一个新实例。

一些非 J2EE 应用程序服务器支持 JCA 组件,因此即使您不使用传统的 J2EE 容器,也要检查它。

If you are using a J2EE application server then consider building a component implementing the Java Connector Architecture (JCA). Each instance of the component accesses a single Session and you configure the container to create at most five (from your example) instances. The container manages the pool and the component's lifecycle. Additionally, all applications deployed on that application server share the component's pool.

If I remember correctly (its been a while) there is also a way to signal the container that an instance died. In this scenario, the container removes the dead instance and instantiates a new one.

Some non-J2EE application servers have support for JCA components so check into it even if you are not using a traditional J2EE container.

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