You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by syed shah <sf...@gmail.com> on 2009/06/07 18:53:33 UTC

how to force tomcat to make a single istance of a servlet

hi,
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

Re: how to force tomcat to make a single istance of a servlet

Posted by Rainer Jung <ra...@kippdata.de>.
On 08.06.2009 19:57, syed shah wrote:
> 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.

OK, then do not use "SingleThreadModel", because that creates a pool of
servlet instances and tries to make sure, each instance is only used by
a single request/thread at the same time. It's somehow the opposite of
what you need.

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

Regards,

Rainer

> On 6/8/09, Christopher Schultz <ch...@christopherschultz.net> wrote:
> 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. :(

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: how to force tomcat to make a single istance of a servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Syed,

On 6/8/2009 1:57 PM, syed shah wrote:
> I want to do this cause i am handling the synchronization myself

How are you managing the synchronization yourself? What needs to be
protected? Why can't you simply use synchronized access to a shared
resource and trust that the JVM will do its job properly?

> and infact i am using innodb so i dont want multiple instance of the
> servlet, although i can handle multiple threads in there.

The use of InnoDB does not seem relevant, here. Even if it were, InnoDB
is entirely threadsafe... otherwise it would be a pretty poor RDBMS
storage engine.

> 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
> constructor that's neat, i do it in the initialize.

Well, you said you didn't want multiple instances. Throwing an exception
in the constructor will certainly prevent that object from being used.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkoulZUACgkQ9CaO5/Lv0PB8gwCcDJAeSfdyte5jYhOt9nzKUGlp
dB4AoKDSz/e+ujbOramwl8qIiAlknMJw
=LMSE
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: how to force tomcat to make a single istance of a servlet

Posted by syed shah <sf...@gmail.com>.
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-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: how to force tomcat to make a single istance of a servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chuck,

On 6/8/2009 1:51 PM, Caldarale, Charles R wrote:
>> From: Christopher Schultz [mailto:chris@christopherschultz.net] 
>> Subject: Re: how to force tomcat to make a single istance of a
>> servlet
>> 
>> Usually, only one instance of your servlet will be created.
> 
> Unless using the SingleThreadModel (a really bad idea), the spec
> allows only one instance of a servlet per JVM.

I didn't know this was part of the spec... I just thought it made
practical sense.

Technically speaking, SRV.2.2 says that there must be only one instance
"per servlet declaration" so I suppose you could trick the container
into creating separate instances like this:

<servlet>
  <servlet-name>name1</servlet-name>
  <servlet-class>foo.bar.BazServlet</servlet-class>
</servlet>

<servlet>
  <servlet-name>name2</servlet-name>
  <servlet-class>foo.bar.BazServlet</servlet-class>
</servlet>

> I wonder what the OP's real problem is?

Yeah... when people ask questions like, it's usually because they are
trying to do something they shouldn't be doing.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkoulP0ACgkQ9CaO5/Lv0PB0iACeIlxZo96LikSxzD94gDzJVvBL
QRgAoItFLDUny7Iz1ul4/PZX25XwU+ak
=0nhG
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: how to force tomcat to make a single istance of a servlet

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Christopher Schultz [mailto:chris@christopherschultz.net]
> Subject: Re: how to force tomcat to make a single istance of a servlet
> 
> Usually, only one instance of your servlet will be created.

Unless using the SingleThreadModel (a really bad idea), the spec allows only one instance of a servlet per JVM.

I wonder what the OP's real problem is?

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.



Re: how to force tomcat to make a single istance of a servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org