Application scope bean Vs. EJB

I want to cash some application data - and was thinking of using a singleton bean with application scope in my controller and JSP page.
<jsp:useBean id="myBean" scope="application" class="bean.datacash" />
I am not very familiar with EJBs - and wanted to know if it would be a mistake not to use an EJB to perform that task.
Is there a downside to my approach?
Thanks for any insight
Mike

Entity EJB'S are used as a kind of data persistence, mostly to Databases. In a way it's like a kind of cache but any changes you make to an EJB are usually persisted to the underlying data storage on a regular basis.
Session EJB's are more for business logic and depending on the type of Session Bean you use can either remain persisted in a session state or used and thrown away.
EJB's also require alot of code so depending on your application requirements it is kind of a big decision.
Is the data your wanting to cache to be shared by all users using the application? Are there any race conditions that may be met by concurrent access to the data? What do you plan to do with the cached data store it in a database or just make it available to share?
If you want to look into using EJB's then your going to require an EJB Container to run them in (this can be standalone like OpenEJB or as part of an J2EE Application server like JBoss or Oracle Application Server to name just a couple).
Using the Singleton Javabean can keeps things simple but once you start to synchronize methods with multiple users you can find some slowing down of the logic as the singleton tries to complete a request before getting onto the next.
The [url http://jakarta.apache.org]Jakarta team offer a package called commons pooling which is designed as a pooling framework (also a way of caching) that can come in handy, I've used it to create a custom JDBC Connection pooler and it works quite well when you get the hang of it.
Another thing to consider is the load the application may be under at it's busiest time. How many concurrent users do you expect to access the application?
Sorry I can't give you a real answer as that is entirely up to you and your requirements. If you have a small app that has around 20-50 users then EJBs are bit of an overkill. If you find you'll have 100+ users with lots of data to manage then EJB's are probably a good option.
If you have a look at [url http://www.theserverside.com]the serverside web site you may find a couple of articles on data persistence or check out some of the online magazines which also have articles on this same topic.
Anthony

Similar Messages

  • Looking for best practice on application scope beans

    Hey – a portal newbie here. I’ve got some application scope beans that need to be initialized on startup. First thought was to create a servlet that would set the bean. Then I saw the GlobalApp setting, but I think that looks like it is more session scope than application… Looking to be corrected here if I am wrong.
    Is there a place where these type of things traditionally happen? Read only, so no cluster worries (I think) Using WLP 8.1 SP4 and looking for best practices. Thanks for the help!

    To support "code sharing" you need an integrated source code control system. Several options are out there but CVS (https://www.cvshome.org/) is a nice choice, and it's completely free and it runs on Windows, Linux, and most UNIX variants.
    Your next decision is on IDE and application server. These are usually from a single "source". For instance, you can choose Oracle's JDeveloper and Deploy to Oracle Application Server; or go with free NetBeans IDE and Jakarta Tomcat; or IBM's WebSphere and their application server. Selection of IDE and AppServer will likely result in heated debates.

  • Application-scope bean finalizer ?

    Hi,
    Using JSF on GlassFish. Have an application-scope bean that creates a javax.persistence.EntityManager and holds it for the duration of the application.
    When I redeploy, however, I get strange exceptions from TopLink which suggest that the state of the persistence provider is not correctly/fully cleared when undeploying. I suspect this because if I redeploy again, it (usually) works (unusually, I have to repeat the process 3 or 4 times).
    So I'm thinking of adding calls to EntityManager.close or something, when the application bean is terminated. Is there such a thing as a bean finalizer, or some method that's called by the app server when the application is undeployed ?
    BTW, the exception happens in this line of code:
    List<Quantity> qList = em.createQuery("SELECT q FROM Quantity q").getResultList();
    and the exception is:
    java.lang.ClassCastException: cannot cast class com.xxxxxx.Quantity to com.xxxxxx.Quantity ! (needless to say that I have not changed the class inbetween)
    Cheers,
    K

    Hi,
    Using JSF on GlassFish. Have an application-scope bean that creates a javax.persistence.EntityManager and holds it for the duration of the application.
    When I redeploy, however, I get strange exceptions from TopLink which suggest that the state of the persistence provider is not correctly/fully cleared when undeploying. I suspect this because if I redeploy again, it (usually) works (unusually, I have to repeat the process 3 or 4 times).
    So I'm thinking of adding calls to EntityManager.close or something, when the application bean is terminated. Is there such a thing as a bean finalizer, or some method that's called by the app server when the application is undeployed ?
    BTW, the exception happens in this line of code:
    List<Quantity> qList = em.createQuery("SELECT q FROM Quantity q").getResultList();
    and the exception is:
    java.lang.ClassCastException: cannot cast class com.xxxxxx.Quantity to com.xxxxxx.Quantity ! (needless to say that I have not changed the class inbetween)
    Cheers,
    K

  • Accessing Application Scope from an EJB

    I'm not sure of the best way of doing this:
    The set up:
    We have a rather large J2EE application that is composed of only servlets and JSPs at the moment (When the application began development EJB was in its infancy and not feasible for the development of the app). Some of the new deployments of the app involve distributed webserver over 4 JVMs and 2 machines. When a user connects the servers load balancing decides which particular JVM they should be connected to. We perform large data caching into the servlet application scope to avoid having to access the database every time a change is made by a user to certain tables in our database. The data caches are stored in hashtables in the servlet application scope.We seem to have no way to determine what other JVMs are running or which JVM a user is currently connected to.
    The problem:
    Apparently the application scope is for a particular JVM. This means that when a user makes a change to a data cache the change doesn't effect users in the other 3 JVMs of the application, only the users who, due to load balancing, happened to be connected to the same JVM as the one where the change was made.
    Our possible solution:
    We were thinking that we could set up a queue (such as MQSeries from IBM) and any time a change is made to one of these data caches put a message on the stack saying that the particular table had been updated. Then a MDB would be listening asynchronously and digest the message from the Queue and recache the tables from the database.
    So here are my questions:
    1. Would this work? Would putting a MDB in the EJB Container give each JVM the MDB in the first place, or would this mean it would pick one of the JVMs to deploy the MDB on and still not fix our problem.
    2. If this does work, how do I access the hashtables stored in the Servlet Application scope from the MDB. I can't seem to find any method available to the MDB (or any EJBs for that matter) that can access the servlets Application scope.
    3. Is this the best way to do this? Or is there a better way to share data between seperate JVMs that I don't have a URL to (It's all ambiguous and handled by the server as to which JVM actually gets the connection.)
    Just in case this is needed:
    We are running IBM WebSphere 5 Application Server and the database could be anything from DB2 to Oracle.
    Thanks in advance for any possible help.

    You wont get any api for directly accessing Servlet application objects from any ejb. I dont think MDB either solves your problem directly. Indirectly you can place a request to some servlet (should be there for each JVM and web application ) and update your application scope variable. I would suggest you to cache the data in database if the size of cache is large. otherwise any open caching tools may help you.

  • Initializing application scope beans on startup

    Hi All,
    I need to initialize an application scope managed bean on startup, and not have to wait for it to be referenced the first time.
    I was thinking of using ContextListener, but you dont have access to the faces context in the listener.
    And you cant even extend FacesServlet, since its final.
    Any ideas on how this can be achieved.
    Thanks for your help!!

    Try with FacesContext.getCurrentInstance() or http://www.jsffaq.com/Wiki.jsp?ptitle=How+to+access+FacesContext+from+the+Filter+or+Servlet%3F&page=HowToAccessFacesContextFromTheFilterOrServlet

  • How can a session scope bean access an application scope bean - help

    Hello,
    I have a JSP page that has references to an Application Scope and a Session Scope bean...
    <jsp:useBean id="myWebApp" scope="application" class="com.my.web.WebApplication" />
    <jsp:useBean id="myWebSession" scope="session" class="com.my.web.WebSession" />
    I would like the WebSession access methods in the WebApplication ... Can/ How does the WebSession object lookup the reference to WebApplication object?

    I don't think you should make the WebSession implement the session listener.
    The session listener will be created once, at the start of the servlet context and would be listening to all sessions. So you would have one instance of the WebSession belonging to the context (though not as an attribute), and others belonging to each session. This will be confusing in your code, I think.
    I would suggest having a different class act as the HttpSessionListener. Perhaps do something like this:
    public class WebSessionInjector implements HttpSessionListener {
      public void  sessionCreated(HttpSessionEvent e) {
        WebApplication webApp = (WebApplication)(e.getSession().getServletContext().getAttribute("webapp"));
        WebSession mySession = new WebSession();
        mySession.setWebApplication(webApp);
        e.getSession().setAttribute("mySession", mySession);
      public void sessionDestroyed(HttpSessionEvent e) { ... }
    }You may be able to use the WebApplication object itself as the listener ...
    Or, you could make the WebSession implement the HttpSessionBindingListener and use the valueBound event much like the sessionCreated event above, but from inside the WebSession object:
    public class WebSession implements HttpSessionBindingListener {
      public void  valueBound(HttpSessionBindingEvent e) {
        WebApplication webApp = (WebApplication)(e.getSession().getServletContext().getAttribute("webapp"));
        this.setWebApplication(webApp);
      public void valueUnbound(HttpSessionBindingEvent e) { ... }
    }

  • How-to remove application scope bean

    I've found the following snippets to get rid of a session bean (to have it removed / recreated):
    snippet 1:
    FacesContext
         .getCurrentInstance()
         .getApplication()
         .createValueBinding( "#{yourBeanName}").setValue(FacesContext.getCurrentInstance(), null );snippet 2:
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("userBean");Would that work for application scoped beans as well? Is there a way of getting a list of all jsf beans? I need to re-initialize the web-application without deployment :S
    thanks,
    david

    Yeah, I've tried it and it's looking good.
    Is there any way I can get a list of (application scoped) jsf beans?

  • Killing a bean with application scope?

    Is there any way to kill a bean with application scope?
    I am writing a web application, that most likely the browser window will never be closed. The problem is a user is authenticated onto our site but because the bean containing the users acct info is application scope, if another user logs in it still shows the old user as logged in. So on logout i would like to kill the application, the bean, servletContext...anything that will refresh the bean and the corresponding jsp. Any help is much appreciated.

    There is an indirect way if you know servlets. The "application" implicit object that you use in your JSP is the javax.servlet.ServletContext object. Instead of declaring your bean using the jsp:useBean tag, directly put your objects into the ServletContext using ServletContext.putAttribute() method. Objects put in the ServletCOntext are available to all pages in the application. Just use the ServletContext.getAttribute() method to retrieve this bean in any other page.
    To "kill the bean", just reset the value of this object to null by calling putAttribute(null).

  • Updating Beans in Application Scope

    The problem is that i dont know how to update a bean in an application scope:
    in EA4 i use this instruction to update a bean property:
    // get the faces context
    FacesContext facesContext = FacesContext.getCurrentInstance();
    // update a property in application scope
    (Util.getValueBinding("BeanName.propertyName")).
    setValue(facesContext,"text");
    and it worked ok in jsp:
    <h:output_text valueRef="BeanName.propertyName" />
    Now in version 1.0 i wrote:
    Application app = facesContext.getApplication();
    errorAplicacion = (ErrorAplicacion)app.createValueBinding("#{beanName}").
    getValue(facesContext);
    How should i update this value in application scope to be used in the jsp ?
    I use in jsp:
    <h:output_text value="#{BeanName.beanProperty}"/>
    But i receive in logs:
    Expression BeanName.beanProperty does not follow the syntax #{...}
    And the result is null
    Thanks a lot
    German

    If you are quoting your 1.0 code literally, you've got a case mismatch between "#{beanName}" when you create the value binding expression and "#{BeanName}" in the JSP page. As with most things in Java, the names are case sensitive.
    Craig McClanahan

  • Setting Beans in Application Scope

    How do I set beans in application scope at the start of a JSF web app? I'd like to be able to do something similar to the Struts plugin.

    Hi,
    I also tried to use a servletcontextlistener. The problem is, that I cannot
    put any beans in the facescontext, because a NullpointerExcpetion
    is thrown. In the contextInitialized-Method I do the following:
    FacesContext context = FacesContext.getCurrentInstance();
    ApplicationFactory factory =
         (ApplicationFactory) FactoryFinder.getFactory(
                   FactoryFinder.APPLICATION_FACTORY);
    Application application = factory.getApplication();
    ValueBinding binding = application.createValueBinding( "#{Bean}" );
    In the createValueBinding-method a Exception is thrown. So I cannot call
    createValueBinding when I am not in a facescontext.
    Can anyone help me?
    Thanks
    Andy

  • Application scope java beans not behaving....

    I am using WL 5.1, sp8, on NT....
              I am running jsp code that declares java beans of application scope....
              However, it doesn't appear to be resolving the application scope
              properly, it seems to instantiate a new Bean each time I go to
              a new page within the same application....
              One thing I am not clear on, is whether I have properly configured
              the WebApp....I have just been assuming that all pages within
              the same path sub-directory are of the same application, but that
              doesn't appear to be working....
              I haven't found any weblogic.properties settings for defining WebApps,
              that will share application scope, etc....
              I assume there is a simple answer here, I appreciate any help...
              Thanks,
              Jason Rosenberg
              

    Jason,
              WLS 5.1.0 is up to service pack level 12 now, which you should go to
              http://support.bea.com/welcome.jsp and login to download from the "Supports
              Download" section. Note that you need to be a paying customer to see this
              section. After applying sp12 you should take a look at our web app docs
              here http://bernal.beasys.com/release/docs51/classdocs/webappguide.html for
              instructions on setting up a web app and registering it in wls 5.1.
              "Jason Rosenberg" <[email protected]> wrote in message
              news:[email protected]...
              > I am using WL 5.1, sp8, on NT....
              >
              > I am running jsp code that declares java beans of application scope....
              >
              > However, it doesn't appear to be resolving the application scope
              > properly, it seems to instantiate a new Bean each time I go to
              > a new page within the same application....
              >
              > One thing I am not clear on, is whether I have properly configured
              > the WebApp....I have just been assuming that all pages within
              > the same path sub-directory are of the same application, but that
              > doesn't appear to be working....
              >
              > I haven't found any weblogic.properties settings for defining WebApps,
              > that will share application scope, etc....
              >
              > I assume there is a simple answer here, I appreciate any help...
              >
              > Thanks,
              >
              > Jason Rosenberg
              >
              >
              >
              >
              >
              >
              >
              

  • How to init an application scope backing bean once app server startup?

    Hi guys,
    I got an application scope backing bean but I don't know how to initialize it once the app server startup ! I need all the webpage to be able to get the 'defaultCountry' value once the app server startup !
    <managed-bean>
    <managed-bean-name>applicationScopeBean</managed-bean-name>
    <managed-bean-class>com.borneo.webapp.action.ApplicationScopeBean</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
    <managed-property>
    <property-name>defaultCountry</property-name>
                   <property-class>java.lang.String</property-class>
    <value>Singapore</value>
    </managed-property>
    </managed-bean>
    Pls help, Thanks !
    regards,
    Mark

    write a context listener and set up your web.xml as such
    <listener>
    <listener-class>com.package.ContextListener</listener-class>
    </listener>
    Then your class should inmplement ServletContextListener interface.
    The 2 methods need to be implemented
    public void contextDestroyed(ServletContextEvent sc_event) { }
    public void contextInitialized(ServletContextEvent sc_event) { }
    Hope that helps

  • Is Weblogic Cluster  supports Application scope Objects?

              Our Application is mainly depends on Application scope objects. On one m/c, it
              is perfect.
              Now, we want to support the failover , load balancing ... (High Availability).
              I know, weblogic cluster supports HttpSessions, EjbObjects ... etc. What about
              Application scope objects.
              we are planning for weblogic server which supports the our requirements.
              If not, what is the solution to make it work by using Weblogic server.
              for eg: I can give what type of application we are developing --- similar to
              textchat.
              our application is real time application.
              

    Even a singleton is not good enough as singletons are good only in the VM they were
              created it. One way is to have a distributed object such as a stateful session bean of
              which only one instance exists. U can create it at startup and store its handle in
              some persistent storage as well as a singleton. This ensures that even if the
              singleton doesnt contain it the refgerence to it can be obtained.
              There will be some problems with this setup as people should not allow it to create
              instances of it . Any such endeavor will lead to a CacheFullException.
              But essentially that seems possible.
              Even the 2.3 Servlet specification mandates that ServletContext and Application level
              object is only valid inside the JVM which created it. HttpSessions are replicated
              SessionContexts are not and are not likely to be in any J2EE application server
              Sameer
              Devi wrote:
              > One more comment from my side,
              > U mentioned "EXCEPT FOR JNDI" , if this is possible, how can we acheive?
              > Can U give brief description ? Take textchat example itself? since, our application
              > is almost have same mechanism. If it is < 1 sec delay means acceptable for this
              > release.
              >
              > Thanks,
              > Devi
              >
              > "Cameron Purdy" <[email protected]> wrote:
              > >Weblogic doesn't support replicated / synchronized app scope objects
              > >across
              > >a cluster except for JNDI, which is probably not a good solution for
              > >what
              > >you are describing.
              > >
              > >If you need a real-time shared fault-tolerant fail-overable high-performance
              > >load-balanced high-scale data store, then let me know when you find one
              > >;-)
              > >.... seriously, there are not a whole lot of good general purpose solutions
              > >in this space. There are some things to look at but they are all expensive:
              > >
              > >Persistent PowerTier on top of Oracle - a caching entity EJB server
              > >Jasmine (or whatever it is called this week) - an object oriented
              > >distributed data store
              > >....
              > >
              > >Peace,
              > >
              > >--
              > >Cameron Purdy
              > >Tangosol Inc.
              > ><< Tangosol Server: How Weblogic applications are customized >>
              > ><< Download now from http://www.tangosol.com/download.jsp >>
              > >
              > >
              > >"Devi" <[email protected]> wrote in message
              > >news:[email protected]...
              > >>
              > >> Our Application is mainly depends on Application scope objects. On
              > >one
              > >m/c, it
              > >> is perfect.
              > >> Now, we want to support the failover , load balancing ... (High
              > >Availability).
              > >>
              > >> I know, weblogic cluster supports HttpSessions, EjbObjects ... etc.
              > > What
              > >about
              > >> Application scope objects.
              > >> we are planning for weblogic server which supports the our requirements.
              > >>
              > >> If not, what is the solution to make it work by using Weblogic server.
              > >> for eg: I can give what type of application we are developing ---
              > >similar to
              > >> textchat.
              > >> our application is real time application.
              > >>
              > >
              > >
              

  • Best Performance? - having a single stateless java pojo as delegate- give it application scope?

    I'm curious about which is best from a performance standpoint -<br /><br />All of our flex calls access a single java pojo. In my remoting-config.xml I'm currently declaring the destination a property scope of 'application': <br /><br /><destination id="UIServicesDelegate"><br />    <channels><br />      <channel ref="my-amf"/><br />      <channel ref="my-local-amf"/ <br />    </channels> <br />     <properties><br />        <factory>spring</factory><br />        <source>uiServicesDelegateBean</source><br />        <scope>application</scope><br />    </properties><br /></destination><br /><br />All of the UIServiceDelegate methods are stateless however, so I'm wondering if I'm gaining anything by giving it scope session. Since their all stateless I"m assuming application scope would be the best from a performance standpoint? I'm assuming in this case only one object will ever be instantiated? <br /><br />Assuming it was between Session and Request scope, is there a lot of overhead instantiating the new server side object each time? I would assume performance would be better using the Session in this case, with the only draw back of some server-side ram being chewed up storing the object in the Session. <br /><br />If you want "singleton" type approach, I figure just using application scope is the preferred approach?

    You wont get any api for directly accessing Servlet application objects from any ejb. I dont think MDB either solves your problem directly. Indirectly you can place a request to some servlet (should be there for each JVM and web application ) and update your application scope variable. I would suggest you to cache the data in database if the size of cache is large. otherwise any open caching tools may help you.

  • Facing error as Not in an application scope - start Orion with the -userThr

    hi all,
    iam facing this error when an jsp which is in Oracle portal(emebedded in iframe code) is updating a flag in database through a datasource configured on portal side.
    Naming Service=Not in an application scope - start Orion with the -userThreads switch if using user-created threads
    regards,
    Ravi

    I encounter the same issues when using the TimerTask to access the JNDI tree. I have found success when I move my business logic to an EJB and call my EJB from my TimerTask. It's a bit more work, but not much. The EJB inherits it's identity based on it's configuration and has no trouble accessing the JNDI tree whenever it's called. Try this and see if your problem doesn't go away.
    Hope this helps,
    Perry Tew

Maybe you are looking for