|
|
Re: Singleton Pattern in EJB
|
Posted: Mar 27, 2007 11:56 PM
|
|
Hi, there is a answer, find in other forum.
---------------------------------------------------
Answer No, You cannot implement a Singleton in EJB. A way to do this could be to implement an RMI or CORBA object.
Comments and alternative answers
Comment on this FAQ entry
EntityBean as Singleton? Ravi Shankar, Nov 26, 2001 [replies:1] Wouldn't the following scheme work like a singleton? Define a read-only BMP EntityBean with <prim-key-class> as String. In the ejbFindByPrimaryKey() method, always return a hard-coded string (irrespective of the primary key value passed in). Wouldn't all clients then get a reference to the same EJB instance? Regards Ravi
Does it work? Did you try? Chris Duerr, Mar 22, 2005 The ejb specification states: at any time, there is only one client thread per bean instance active. Therefore I expect that there might more than one instance of an entity bean, even if there is only one data set to represent.
Setting the entitiy bean read-only means that there is no call to the (unneseccary) ejbStore(). A read-only bean still may be represented by more than one instance.
But this is blank theory, so does it in practice work? Did you try?
Use a stateless session bean with an instance pool size of 1. Dominic Cioccarelli, Feb 14, 2002 [replies:2] You can (in many application servers) determine the instance pool size for each bean. By implementing a stateless session bean with an instance size of 1, you effectively have a singleton. The downside is that setting the pool size is vendor specific.
Re: Use a stateless session bean with an instance pool size of 1. David Li, Feb 21, 2002 [replies:1] My impression is that a stateless session bean instance is not free threaded and does not allow a re-entry while another client is using it. The other way to view it is that if a single instance of a stateless session bean were a singleton and were able to serve multiple client simultanouly, why would you ever need to put more than one bean instances in a pool? Hope somebody can confirm my impression or tell me why I am wrong. Thanks David
Re[2]: Use a stateless session bean with an instance pool size of 1. Kirill Fakhroutdinov PREMIUM, May 7, 2002 You are right that this singleton EJB can not be multithreaded - as any other EJB. Only one client at a time would be able to access it. And because other EJBs can not wait because of the absence of synchronization mechanisms - they probably will be just failing (unless application server/container will be able to put them "on hold" - that I doubt).
P.S. Read-only singleton could be free-threaded, but read-write singleton usually has to be synchronized.
Use Of Singleton Pattern in EJB. santosh Terkhedkar, Sep 9, 2002 [replies:1] I think use of singleton pattern is possible in EJb, we can use JNDI to store arbitrary objects and lookup these objects through jndi,but use of singleton pattern in ejb is not useful. other way to achive this could be to write a java class and call this bean from within java class and make only one instance of this class.
Re: Use Of Singleton Pattern in EJB. Ankush Purwar, Jun 20, 2005 Using your approach write a java class and call this bean from within java class and make only one instance of this class doesn't work in clustering.
|
|