You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mohan Radhakrishnan <Mo...@hclcomnet.co.in> on 2002/11/16 11:25:55 UTC

action calling Singleton

Hi,
    What effect does a Singleton class have when it is called by the Action?
I read that the Action is reused.
It is just a simple file save utility but multiple users might be calling it
from their actions at the same time.  Is it recommended to use a utitily
like this ? 

The upload utility is an example. If multiple users upload, and every file
is saved in the same directory, the java directory handle might have to be
shared. Is that right ? Won't it result in locking of the directory ?

Thanks,
Mohan

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: action calling Singleton

Posted by Andrew Hill <an...@gridnode.com>.
Hehe, your playing with fire there. ;-)
As you are aware each request is processed in its own thread.
The actual instance of the action that singleton is called from is
unimportant, you could have several threads calling it simultaneously - all
from the same actions code. What this means of course is that your singleton
probably needs to be threadsafe. Depending on what its doing this often
means making appropriate use of the "synchronized" keyword - either within
the code of the singleton itself, or by synchronizing in your code
everywhere you call it.
Having done this, only one thread will get to play with the singleton at a
time - the others blocking until they get a chance (beware of deadlocks!) -
this could have a performance impact if its code thats used a lot.
The next thing you need to be aware of is that your singleton is only a
singleton within that JVM (or ClassLoader to be precise) - if you are
running in a clustered environment each JVM will have its own version of
your singleton. If you have not thought through the consequences of this,
you will probably run into trouble.

I did a quick web search. Have a read of these - Ive not had time to look at
them probably myself but they should help your understanding.
http://www.javaworld.com/javaworld/jw-01-2001/jw-0112-singleton.html
http://www.javageeks.com/Papers/JavaStatics/JavaStatics.pdf


-----Original Message-----
From: Mohan Radhakrishnan [mailto:MohanR@hclcomnet.co.in]
Sent: Saturday, November 16, 2002 18:26
To: 'Struts Users Mailing List'
Subject: action calling Singleton


Hi,
    What effect does a Singleton class have when it is called by the Action?
I read that the Action is reused.
It is just a simple file save utility but multiple users might be calling it
from their actions at the same time.  Is it recommended to use a utitily
like this ?

The upload utility is an example. If multiple users upload, and every file
is saved in the same directory, the java directory handle might have to be
shared. Is that right ? Won't it result in locking of the directory ?

Thanks,
Mohan

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>