Singleton vs ServletContext for storing application scope objects

Hi all,
I would like to share some bean objects between all sessions inside the same web application, i.e. all users should access the same beans.
Which is the best way, to store the beans, use a singleton ore put them into the ServletContent vs get/setAttribute?
How about large objects, are there any limitations?
Thanks
Mert
Edited by: mahmut1 on Oct 22, 2009 3:32 AM

I must confess I am a heretic. I would use a singleton. I try to use as few servlet services as possible when working with servlets; that way the code has a fighting chance of being reusable outside the servlet system.
As to "singletons are evil": well, if we are talking about ServletContext.setAttribute(), that is a singleton. It is a singleton hash table. The difference between the attribute hash table and a hash table singleton you write yourself is with attributes you don't automatically see the source code that defines the singleton, so you get to pretend you are not using a singleton.
    protected Hashtable myAttributes;Haha! There, I just showed how the singleton hash table is implemented in a popular web server!

Similar Messages

  • 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.
              > >>
              > >
              > >
              

  • Application scope objects(opinions wanted)

    Hi All
    Looking for educated opinions on the pros/cons of application scope objects. I am thinking about creating a read-only application scope hashmap representation of some static data in my database. When queried at the database level it can cause relatively long delays (3-4 seconds) and sometimes must be stored in many sessions on the server. I am thinking just one read-only object stored at app scope will cut down on the waste of memory, as well as skyrocket the speed of retrieving this data.
    Please give me your opinions!!! I can tell you a little more about the app if you need more info to make an educated statement...
    Thanks
    Greg B

    You can use the Properties Object for static data that can be persisted on a filesystem. Also, I assume that you can hold quite a bit of data too. A file read should be faster than a database query. Also if you do the connect once and save the connection for several reads, that works too. The only real slow operation, outside of large resultsets, would be the initial connect.
    I always design up two or three possible solutions and sometimes I protpotype each one. Sometimes one of the tests will prove to be the exact opposite choice of the one you thought would work.
    Now that I have written your response, I think that you could create a custom class to hold data, but would that not just be a copy of the properties object. If it's key/value you need, that's the choice.
    Stand back and ask, "What is the data type of the high level objects?"
    Also, SUN has good articles on almost everything in the JDK.
    David Whitehurst
    [email protected]

  • Application Scope Objects and Multithreading

    I will be creating a set of interlinking objects in application scope that are intended for read only. The objects will be accessed every time a user makes a request to the servlet which will be on every link of the site since the servlet is a controller.
    I am pretty sure that there are no concurrency issues here - that all requests will be able to access the objects quickly. Can someone confirm this for me.

    As long as the threads only read information from the objects and do not update them then you should be ok.

  • Common Practice For Storing Application Data Files

    During my application development targeting for XP and Linux time, I will store my application data files (Files where user may create, modify, rename, delete...) same path as my application directory. For instance, if my application is installed in :-
    C:\Program Files\MyApp\MyApp.jar
    All my application data files will be located in
    C:\Program Files\MyApp\Config\ directory.
    This works quite well until Vista introduce UAC. Vista will not allow write opration to C:\Program Files\MyApp\Config\ folder (Config folder is being created during installer execution). However, to support legacy application, Vista use a workaround (Virtualization) for application to write. For example, MyApp.jar want to perform write on
    Real path :-
    C:\Program Files\MyApp\Config
    Vista will redirect it to :-
    Virtual path
    C:\Users\<account>\AppData\Local\VirtualStore\Program Files\MyApp\Config
    Please refer to the links for more detail
    [http://www.codeproject.com/KB/security/acl_common_data.aspx?display=Print]
    [http://www.codeproject.com/KB/vista-security/MakingAppsUACAware.aspx]
    However, this introduce several additional problems [http://support.microsoft.com/kb/927387]
    May I know what is the common practice for storing an application data files? The below are my several thoughts
    (1) Writing to
    * Windows XP - %systemdrive%\Documents and Settings\All Users\Application Data
    * Windows Vista - %systemdrive%\ProgramData
    * Windows 2000 - %systemdrive%\Documents and Settings\All Users\Application Data
    * Windows Server 2003 - %systemdrive%\Documents and Settings\All Users\Application Data
    * Windows 98 - %systemdrive%\Windows\All Users\ Application Data
    which we may retrieve through "CommonApplicationData", using the API function SHGetFolderPath. However, how can we do that in Java? How to make compatible with Linux?
    (2) Writing to user home directory through "user.home" properties. Create a folder .MyApp and write the application data on it.
    Any input and suggestion are welcomed.
    Thanks!
    cheok

    >
    ...I will store my application data files (Files where user may create, modify, rename, delete...) same path as my application directory. >See [Store Application Settings and Components|http://sdnshare.sun.com/view.jsp?id=2305] for code that will put the required parts in a suitable sub-dir of user.home.

  • Help needed for storing and sorting objects.

    Hello
    I have an assignment and it is to create a guessing game, here is the question,
    In this assignment you are to write a game where a user or the computer is to guess a random
    number between 1 and 1000. The program should for example read a guess from the keyboard, and
    print whether the guess was too high, too low or correct. When the user has guessed the correct
    number, the program is to print the number of guesses made.
    The project must contain a class called Game, which has only one public method. The method must
    be called start(), and, when run it starts the game. The game continues until the user chooses to
    quit, either at the end of a game by answering no to the question or by typing 'quit' instead of a
    guess. After each game has been played, the program is to ask the user for a name and insert this
    together with the number of guesses into a high score list. When a game is started the program
    should print the entire high score list, which must be sorted with the least number of guesses first
    and the most last. Note, the list must be kept as long as the game-object is alive!
    each score also
    consists of the game time. In case there are two high scores with the same number of guesses, the
    game time should decide which is better. The game time starts when the first guess is entered and
    stops when the correct guess has been made. There should also be input checks in the program so
    that it is impossible to input something wrong, i.e. it should be impossible to write an non-numeric
    value then we are guessing a number, the only allowed answers for a yes/no question is yes or no,
    every other input should yield an error message an the question should be printed again.
    I understand how to code most of it, except I am not sure how to store the playerName, playerScore, playerTime and then sort that accordingly.
    I came across hashmaps, but that wont work as the data values can be the same for score.
    Is it only one object of lets say a highScore class, and each time the game finishes, it enters the values into an arrayList, I still dont understand how I can sort the array all at once.
    Should it be sorted once for score, then another array created and sorted again, I dont get it I am confused.
    Please help clarify this.

    Implode wrote:
    We had the arrayList/collections lecture today.
    I asked the teacher about sorting objects and he started explaining hashmaps and then he mentioned another thing which we will only be learning next term, I'm sure we must only use what we have learned.
    How exactly can this be done. I have asked a few questions in the post already.
    ThanksWell, there was probably a gap in the communication. Hash maps (or hash tables, etc.) are instance of Map. Those are used to locate a value by its unique key. Generally, to speed up access, you implement a hashing function (this will be explained hopefully in class). Think of name-value pairs that are stored where the name is unique.
    Contrast this with items that are sorted. Any List can be sorted because its elements are ordered. An ArrayList is ordered, generally, by the order you inserted the elements. However, any List can be given its own ordering via Comparable or Comparator. You can't do this with an ordinary Map. The purpose of a Map is speedy access to the name-value pairs, not sorting. The List likewise has different purposes, advantages, disadvantages, etc. List can be sorted.
    A Map is generally similar to a Set. A Set is a vanilla collection that guarnatees uniqueness of each element (note, not name-value pairs, but simple elements). There is one concrete class of Map that can be sorted, TreeMap, but I doubt your professor was referring to that. The values or the keys can be returned from the Map and sorted separately, but again, I doubt he was referring to that.
    Take a look at the Collections tutorial here on this site or Google one. It is fairly straightforward. Just keep in mind that things (generally) break down into Set, Map and List. There are combinations of these and different flavors (e.g., Queue, LinkedHashMap, etc.) But if you can learn how those three differ, you will go a long way towards understanding collections.
    (Oh, and be sure to study up on iterators.)
    - Saish

  • Install Boot Camp/OS X on SSD disk, use internal harddrive for storing applications for Boot Camp

    Hello everyone!
    I have a SSD drive that I have installed Yosemite on and have the following question:
    I have an iMac with an external SSD disk on 250 GB that I have connected in a thunderbolt enclosure and have an internal hard drive on 500 GB SATA drive.
    First I wonder if it's possible to have a dual boot on OS X and Boot Camp on my SSD disk?
    But here comes the tricky part. I want to have my internal hard drive (500 GB) on my iMac to use some sharing to Windows.
    So my idea is just boot to Boot Camp and use my internal hard drive, that I want to partition to two parts (one partition for OS X and one partition for Windows) to have the applications (like games/user applications) installed on so I don't fill up my SSD drive with all that data. I just want to have the SSD disk to boot in and use the speed for the Boot Camp system.
    I also have file vault activated on Yosemite and want to have encryption activated on both volumes (OS X/ Boot Camp) so it's secured.
    So is this idea possible to do or do I need to have all the user applications / games installed directly to my Boot Camp partition?
    Sorry for my grammar, hope you understand what I want to do. If not I will try to explain better and more detailed if needed.
    Thanks so much in advanced!

    Trendchaser wrote:
    Hello everyone!
    I have a SSD drive that I have installed Yosemite on and have the following question:
    I have an iMac with an external SSD disk on 250 GB that I have connected in a thunderbolt enclosure and have an internal hard drive on 500 GB SATA drive.
    First I wonder if it's possible to have a dual boot on OS X and Boot Camp on my SSD disk?
    Please see http://bleeptobleep.blogspot.com/2013/02/mac-install-windows-7-or-8-on-external. html for installing on an external Thunderbolt disk.
    But here comes the tricky part. I want to have my internal hard drive (500 GB) on my iMac to use some sharing to Windows.
    So my idea is just boot to Boot Camp and use my internal hard drive, that I want to partition to two parts (one partition for OS X and one partition for Windows) to have the applications (like games/user applications) installed on so I don't fill up my SSD drive with all that data. I just want to have the SSD disk to boot in and use the speed for the Boot Camp system.
    You can create a FAT/exFAT partition via Disk Utility on the 500GB SATA disk. After Windows is installed, format this partition to NTFS from Windows, and use it as Windows D: (or appropriate drive letter depending on your environment) and install Games/Applications. Be careful with the read-only HFS partitions which get drive letters assigned automatically. This partition cannot be resized using Windows tools or OSX Disk tools.
    I also have file vault activated on Yosemite and want to have encryption activated on both volumes (OS X/ Boot Camp) so it's secured.
    FV2 (and any other CoreStorage volumes) are unreadable in Windows. Only HFS+ volumes are supported by the Apple read-only HFS driver provided by BC drivers. Windows volumes require BitLocker encryption, FV2 cannot be used for such volumes.

  • What are the pros and cons of storing heavely used CFCs in the application scope?

    I've been storing all the required CFCs for a site in the application scope. During onApplicationStart I do something like this application.objSomeCfc =CreateObject('component', 'com.someCfc').init().
    Here is my reasoning.
    Get the CFCs initialized once and stored in memory for better performance.
    Using CreateObject several times on each page load can have a negative impact on performance.
    Having one place to create application scoped CFCs makes it easier to manage code.
    So is my thinking flawed? Are there any additional pros or cons for dealing with CFCs? Is there any docs, articles, blogs, videos, frameworks, ...... that I should check that may change my perspective on how I'm doing this?
    Thanks

    Your approach is fine, depending on how many and what type of CFCs you are talking about. If they are "singletons" - that is, only one instance of each CFC is needed to be in memory and can be reused/shared from multiple parts of your application - caching them in the application scope is common.  Just make sure they are thread safe ("var" or local.* all your method variables).
    You might consider taking advantage of a dependency injection framework, such as DI/1 (part of the FW/1 MVC framework), ColdSpring, or WireBox (a module of the ColdBox platform that can be used independently).  They have mechanisms for handling and caching singletons.  Then you wouldn't have to go to the application scope to get your CFC instances.
    -Carl V.

  • DynPage - Store data in application scope (for ALL users)

    Hi,
    I have a DynPage application.
    I want to share data between ALL the users of this portal application (I am looking for something equal to Application Object in ASP.NET).
    I thought of using 'Component Context storage' but according to note 802015 it's not recommended.
    <i>WE DO NOT RECOMMEND TO USE THE CONTEXT FOR STORING DATA, BECAUSE IT MIGHT
    BE RELEASED EACH TIME THE WEB SERVER NEEDS RESOURCES.</i>
    What about using session object with SCOPE_COMPONENT/SCOPE_CONTEXT (something like: componentSession.putValue("xxx",xxx,IPortalComponentSession.SCOPE_COMPONENT);
    So how can I get application object or something similar? (I don't want use DB/file)
    Regards,
    Omri

    Tested "session object with SCOPE_COMPONENT/SCOPE_CONTEXT" with no success.
    I thought of using servletContext but when dealing with portal applications the servletContext belongs to the "prt" servlet, meaning the value will be persistent as long as the portal is up...
    Any ideas?
    Omri

  • Can i use singleton for storing current login id

    hello,
    can i use a singleton for storing login userid temporarilary for application lifetime. Is there any issue if more than 2 users login at the same time as the singleton object will be static ! ..

    let me ask more precisely..
    is there anything of concern if i run my program(involving statics) by 2 jvm instances.
    public class Demo extends Thread
         static String[] myargs ;
         public static void main(String args[])
              myargs = args;     
              try
                   Demo.sleep(5000);
              catch(InterruptedException e)
              System.out.println(myargs[0]);
    }look at the pre code...what if i try to run it by using 2 command promptS
    giving different aruements (a bit quick)
    Edited by: rajat on Dec 28, 2007 7:14 AM

  • Storing int variable in application scope

    hi all,
    I have a requirement where i need store some counter(int) in the application scope. Please tell me how to do that. I know create instance of userbean in application scope, but how to do for integer. Thanks in advance.
    -Manjunath

    Hi,
    you can only store Java Objects in the various scopes. you cannot store the primitive data types. so convert the integer to Integer Object and then store that one using setAttribute. Dont forget to parse the retrieved object to Integer as you will get Object type.
    do this:
    int i=10; //you have
    Integer j = new Integer( i );
    request.setAttribute("name", j );
    Integer k =(Integer)requst.getAttribute("name");Hopes this will help you.
    Prasad Babu Dandu,
    BOB Technologies,
    Bangalore

  • Objects of application scope and clustering.

              I understand that weblogic 6.1 replicates session data to a secondary server in a
              cluster. This replication should include objects declared with <jsp:useBean ...
              scope="session"/> as well as objects that are explicitly maintained in the session
              with setAttribute().
              However, what about objects with application scope? Are they replicated to a secondary
              server when a cluster is in use?
              Thanks,
              -Dave.
              

    David Vazquez <[email protected]> wrote:
              > I understand that weblogic 6.1 replicates session data to a secondary server in a
              > cluster. This replication should include objects declared with <jsp:useBean ...
              > scope="session"/> as well as objects that are explicitly maintained in the session
              > with setAttribute().
              > However, what about objects with application scope? Are they replicated to a secondary
              > server when a cluster is in use?
              No, they are not replicated in the cluster. To implement replication you can use JavaGroups:
              http://sourceforge.net/projects/javagroups/ or Cameron's Coherence product:
              http://www.tangosol.com/products-clustering.jsp for example.
              > Thanks,
              > -Dave.
              Dimitri
              

  • Why files in Lightroom mobile and files in the creative cloud not the same? idea-  one place(stored date) for all application ??????

    Why files in Lightroom mobile and files in the creative cloud not the same? idea- one place(stored date) for all application ??????

    Lightroom Mobile is not a cloud storage service. You shouldn't treat it as a way of backing up your files. You are merely storing Smart Previews of your files in the cloud space, high-quality JPEGs of your files regardless of their original format on your desktop. The point is that they are there so you can continue editing them in a Lightroom-like environment even while away from your desktop/laptop computer. The files in Lightroom Mobile can only be used in Lightroom Mobile.
    This is very different from what is offered by the Creative Cloud storage, which can be used to synchronize your files between any device (that can support the individual files).
    The vast difference in how each service works and its intended use is why they are separate.

  • 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.

  • MAC OS:Word cannot locate the server application for AcroExch.Document.7.objects

    In MAC OS,installed Adobe reader ,When I double click a pdf file in Word2011,it shows"Word cannot locate the server application for AcroExch.Document.7.objects".what can i do to fix it?

    I found out through someone else. The "how to" find the place to add it was more challenging.
    Instructions:
    1. Take the Webarchive file and save it on your computer (anywhere is fine).
    2. Open MacMail and go to preferences>signatures and create a signature with any name and filler text. Close Macmail.
    3. Open your finder, go to the "Library" folder. If you cannot view your Library folder, open up "terminal" and simply paste in: chflags nohidden ~/Library/
    (this will allow you to view your Library folder from now on)
    4. In your library folder, go to Mail>Mail Data>signatures
    5.Copy the name of the signature file that is already in the signatures folder. It should look something like "079ADD83-A19F-4597-BBFC-4F35858894DE" After copying that name, delete the file.
    6. Go to the webarchive file you saved and rename it the name of the file you just deleted. Then paste that into the library>Mail>Mail Data>signatures folder.
    7. Open your macmail and see if it worked!
    If my directions are not clear, you may find the following article helpful from step 5:  http://code.coneybeare.net/how-to-make-an-html-signature-in-apple-mail-f
    Now here's how I managed to find my Library/Mail in OSX 10.7.3:
    Menu: Go
    Choose: Home
    Library
    Mail
    Vs
    Mail Data
    Signatures
    Hope this helps!

Maybe you are looking for

  • Error while transporting Infocube

    Hello all, i am getting following error while transporting infocube to testing system. Please see the bellow error message details. Error/warning in dict. activator, detailed log    > Detail Structure change at field level (convert table /BIC/DZMD_Y_

  • Mac mini slow scroll

    Very frustrating. Lately my mac mini (OSX 10.8.5) sluggishly scrolls or the pinwheel comes up and I can't scroll. I have restarted the computer 3 times the last 3 days with no improvement. The slow or stopped scroll happens on any page I'm visiting u

  • How to use class in JSP file?

    Hello All, I am new to Jsp. In Jsp file,I used class of myself. Why,the Constractor seems not working? Thanks in advance.

  • ICC profiles- iPhoto colour management issues.

    I read in one of the Mac magazines the following regarding the release of iPhoto 5.0.3 ...."images are no longer colour-shifted after editing;....". Does anyone know if this refers to a resolution to the long running problem whereby iPhoto would over

  • Screen artifacts in between tabs

    Are you guys noticing the strange graphical behavior when you restore your safari from the dock?