• Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB : Similar to Context level attributes of Servlet technology

 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I could not frame a proper subject as my question is a little bit vague. In past I created a ServiceLocator object which is based on Singleton pattern and during the Servlet Init() method, I called this class instance to look me up a DataSource. As I wanted the DataSource to be a shareable across all the client requests, so once I got the DataSource object I stored that as an attribute at ServletContext level in init method. So for any subsequent need of DataSource, I would just get the attribute from Context and invoke the connection from that. I'm wondering if this is possible with EJB.

I wanted to use my old ServiceLocator to look me up the Objects bound to JNDI, but I do not want each of my enterprise beans to call method on ServiceLocator every time. Rather I want my DataSource to be bound to some context, so that I can just retrieve the object just like I did in servlets. Is this possible.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every EJB server enables the data source to be bound to its JNDI tree. Indeed, the server provider may say that in some circumstances it is manadatory or highly recommended to obtain JDBC connections through a data source on the JNDI tree.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you say here. But my question is looking up JNDI tree everytime it is needed, takes some network resources etc. Instead when we know that there is only object of DataSource is needed across pool of enterprise beans, why do we want to put the code to look up JNDI for each and every bean. Is there any way to register the looked up DataSource object locally with in the contexts of the beans, so that by a ordinary method call, all beans can retrieve the Datasource object.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know that a JNDI lookup will result in a network roundtrip?

Even if JNDI lookups take a long time, and you will only find out during testing or live running, the solution is not obvious. You may be able to do some application caching, eg in the ejbCreate method, or use EJB container caching (probably best). For instance, WebSphere Application Server provides JNDI caching.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!