Hi Christopher, I want to do this cause i am handling the synchronization myself and infact i am using innodb so i dont want multiple instance of the servlet, although i can handle multiple threads in there. I just wrote some code thats pretty similar to yours except that i use a int and check if its >1, also you increment it in the contructor that's neat, i do it in the initialize.
Thanks, best regards Fahad On 6/8/09, Christopher Schultz <ch...@christopherschultz.net> wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Syed, > > > On 6/7/2009 12:53 PM, syed shah wrote: > > I want to enforce single instance creation for the servlet because I have > > some code that serves the user requests and i want to implement caching > and > > handle synchronization myself, thanks and best regards Fahad > > > Usually, only one instance of your servlet will be created. You could > write some code to check for this, of course. > > Something like this should work: > > public class MyServlet extends HttpServlet > { > private static boolean _isInUse; > > public MyServlet() > throws ServletException > { > super(); > > synchronized(getClass()) { > if(_isInUse) { > throw new ServletException("Sorry, only one at a time"); > } > > _isInUse = true; > } > } > > ... > > public void destroy() > { > synchronized(getClass()) { > _isInUse = false; > } > } > } > > I'm not actually sure why you'd ever want to do this, though. :( > > - -chris > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.9 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAkotO68ACgkQ9CaO5/Lv0PBXBgCgm0hf3J73t7GCXsLl9KHY5spf > c5YAn1NSuZYMrk6r9FqFFdv8OqxQij/B > =DZ4r > -----END PGP SIGNATURE----- > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > >