Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Object singletons in Servlets/Beans

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Object singletons in Servlets/Beans

    Hi Everyone,

    I am new to Java and I am not sure if there are issues relating to singleton classes in servlets/beans especially in a clustered environment.
    Here is a simple example of a hashmap in a singleton class called GlobalModuleInfoList and both a servlet and a bean registers themselves and access it.

    This example might be silly, but can someone tell me if it will cause problems in a clustered environment or not.

    Thank you
    Eric


    @WebServlet("/Example")
    public class Example extends HttpServlet {
    	private static ModuleInfo myInfo = GlobalModuleInfoList.registerModule("Example", "1.0");
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              myInfo.incUsageCounter();
            }
          //....


    @Singleton
    @LocalBean
    public class MyBean {
    	private static ModuleInfo myInfo = GlobalModuleInfoList.registerModule("MyBean", "1.0");
        //....

    public class GlobalModuleInfoList {
    	private static HashMap<String, ModuleInfo> modules = new HashMap<String, ModuleInfo>();	
    	public synchronized static ModuleInfo registerModule(String name, String version) {
    		ModuleInfo m = null; m = modules.get(name);
    		if (m==null) {m = new ModuleInfo(version, version);modules.put(name, m);}
    		return m;						
    	} 
    }

    public class ModuleInfo {
    	String name;
    	String version;
    	int usageCounter;
    	public ModuleInfo(String name, String version) {this.name=name;	this.version=version;}	
    	public synchronized int incUsageCounter() {usageCounter++; return usageCounter;}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Object singletons in Servlets/Beans

    Depending upon the server environment, different class loaders or JVM's might load the classes - for read/write static variables this creates a problem as different instances of the variables might exist. There may be further problems with data synchronization, if multiple threads attempt to read/write at the same time.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object singletons in Servlets/Beans

    Quote Originally Posted by copeg View Post
    Depending upon the server environment, different class loaders or JVM's might load the classes - for read/write static variables this creates a problem as different instances of the variables might exist. There may be further problems with data synchronization, if multiple threads attempt to read/write at the same time.
    Thanks for your feedback.

    I also found this response from Mike Baranczak that makes the same point.
    You should generally avoid static variables in Java EE or servlet containers, because the class loading can be a bit tricky - you may wind up with multiple copies when you don't expect it, or you may be sharing a single copy between different applications, or you may be keeping stuff in memory when you redeploy your application. You can make an exception in cases where the variable isn't exposed outside the class, and you don't really care how many copies of it you have (for example, logger objects).

Similar Threads

  1. Net-beans-java
    By Tanmaysinha in forum Java IDEs
    Replies: 2
    Last Post: November 6th, 2011, 05:45 AM
  2. HOW TO MAKE a TIC TAC TOE GAME in Net beans as a desktop application ?
    By jude_pinas in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2011, 12:51 AM
  3. Net Beans OR Eclipse
    By sanjuds in forum Java IDEs
    Replies: 6
    Last Post: August 16th, 2011, 04:36 AM
  4. [SOLVED] Is this possible? (regarding beans and JSP)
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 31st, 2011, 12:58 AM