EJB2会话bean中的连接处理 [英] Connection handling in EJB2 session beans

查看:219
本文介绍了EJB2会话bean中的连接处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始维护一个运行在OC4J上的旧EJB2应用程序。这包括EJB doclet和其他可怕的东西。目前,每个方法创建一个 ConnectionFactory ,为 Datasource 查询JNDI,然后创建一个连接。这导致了大量的锅炉板代码。



我现在的问题是:每个无状态会话bean只能执行一次,并重用相同的连接是否安全? ejbCreate()将从JNDI获取连接,然后在 ejbRemove()中关闭它。

这是好还是坏设计?

解决方案

提出的设计会有不可预测的行为,由容器。无状态会话bean由容器(在大多数情况下)和同一个实例可以提供给多个请求。



方法 ejbCreate() ejbRemove ()是在第一次初始化bean时调用的。当它从池中分别删除。因此,它可以在 ejbCreate()中打开一个连接,但不能关闭它&服务请求。



但是,如果连接已打开, bean在池中保持空闲,它会不必要地占用资源,可能会出现异常,如套接字超时,太多打开的连接等。



编辑:从 核心J2EE模式 - 服务定位器


使用服务定位器对象抽象所有JNDI使用并隐藏
初始上下文的复杂性创建,EJB主对象查找,
和EJB对象重新创建。多个客户端可以重用Service
Locator对象来降低代码复杂性,提供单点
控制,并通过提供缓存设施来提高性能。



I recently started maintaining an old EJB2 application running on OC4J. That includes EJB doclet and other horrible horrible things. Currently, each method creates a ConnectionFactory that queries JNDI for a Datasource, which then creates a connection. This leads to a lot of boiler plate code.

My question now is: is it safe to do this only once per stateless session bean, and reuse the same connection? ejbCreate() would get the connection from JNDI, and then close it in ejbRemove().
Would this be good or bad design?

解决方案

The proposed design will have unpredictable behavior, as the life cycle methods are handled by the container. Stateless session bean are pooled by the container(in most cases) & same instance can be served to multiple requests.

The methods ejbCreate() and ejbRemove() are invoked by the container when the bean is initialized first time & when it is removed from the pool respectively. Therefore it may open a connection in ejbCreate(), but may not close it & service requests with the same connection.

But, if connection is opened & the bean remains idle in the pool, it will hold up resource unnecessarily, may end up in exceptions like socket timeout, too many opened connections etc.

Its better to write a generalize method for opening/closing connection, to utilize the resources properly.


Edit : From Core J2EE Patterns - Service Locator

Use a Service Locator object to abstract all JNDI usage and to hide the complexities of initial context creation, EJB home object lookup, and EJB object re-creation. Multiple clients can reuse the Service Locator object to reduce code complexity, provide a single point of control, and improve performance by providing a caching facility.

这篇关于EJB2会话bean中的连接处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆