RE: AW: Preventing concurrent execution of an Action

2004-12-18 Thread Yee, Richard K, CTR,, DMDCWEST
Why don't you create the file with a temporary filename and return a link to
the file you created. If you do this, you will not have to lock your action.


-Richard


-Original Message-
From: Behrang Saeedzadeh [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, December 14, 2004 3:25 PM
To: Struts Users Mailing List
Subject: Re: AW: Preventing concurrent execution of an Action


Hi

Thanks for the responses.

> You don't need your locked flag, it's provided by java object monitors 
> automatically.

I actually used the lock so if the updateXML is being executed the next
request won't even try and wait to aquire the lock for it and simply return.

> Last question: if two users click the link simultaneously, what should 
> happen? In your code, the second user overwrites the changes of the 
> first user (silently).

I guess what I have done ignores the second click as the control returns
from the doExecute method before reaching the updateXml method.

Regards,
Behrang S.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: AW: Preventing concurrent execution of an Action

2004-12-14 Thread Behrang Saeedzadeh
Hi

Thanks for the responses.

> You don't need your locked flag, it's provided by java object monitors
> automatically.

I actually used the lock so if the updateXML is being executed the
next request won't even try and wait to aquire the lock for it and
simply return.

> Last question: if two users click the link simultaneously, what should
> happen? In your code, the second user overwrites the changes of the first
> user (silently).

I guess what I have done ignores the second click as the control
returns from the doExecute method before reaching the updateXml
method.

Regards,
Behrang S.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



AW: Preventing concurrent execution of an Action

2004-12-14 Thread Leon Rosenberg
Small correction, 
In your example there is a difference whether you have a multiprocessor
machine. On a single-proc machine it will work fine (prevent second user
from overwriting), on a multi-processor machine it's undefined.

Regards
Leon

> -Ursprüngliche Nachricht-
> Von: Durham David R Jr Contr 805 CSPTS/SCE
> [mailto:[EMAIL PROTECTED]
> Gesendet: Dienstag, 14. Dezember 2004 23:46
> An: Struts Users Mailing List
> Betreff: RE: Preventing concurrent execution of an Action
> 
> You might be able to get away with simply adding 'synchronized' to the
> execute method's signature.
> 
> 
> synchronized public ActionForward execute(...) {
>  updateXml();
> }
> 
> private void updateXml() { ... }
> 
> 
> The lock/key threading techniques have to do with, I think, a more
> complex threading issue than the one you've presented.
> 
> 
> - Dave
> 
> 
> > -Original Message-
> > From: Behrang Saeedzadeh [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, December 14, 2004 4:34 PM
> > To: [EMAIL PROTECTED]
> > Subject: Preventing concurrent execution of an Action
> >
> > Hi
> >
> > I've an action that creates an XML file on the server. It's executed
> > by clicking on a link. I don't want multiple instances of it to be
> > executed concurrently.
> >
> > Does an approach like the following work?
> >
> > public class UpdateXmlAction ...
> > {
> >private static final boolean locked;
> >
> >public void execute(...) {
> >
> >if (UpdateXmlAction.locked) {
> >return;
> >}
> >
> >Synchronized(UpdateXmlAction.class) {
> >locked = true;
> >updateXml();
> >locked = false;
> >}
> >
> >}
> > }
> >
> > Thanks in advance.
> >
> >
> > --
> >
> > Behrang Saeedzadeh
> > http://www.jroller.com/page/behrangsa
> >
> > -
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



AW: Preventing concurrent execution of an Action

2004-12-14 Thread Leon Rosenberg
Small correction, 
In your example there is a difference whether you have a multiprocessor
machine. On a single-proc machine it will work fine (prevent second user
from overwriting), on a multi-processor machine it's undefined.

Regards
Leon

> -Ursprüngliche Nachricht-
> Von: Durham David R Jr Contr 805 CSPTS/SCE
> [mailto:[EMAIL PROTECTED]
> Gesendet: Dienstag, 14. Dezember 2004 23:46
> An: Struts Users Mailing List
> Betreff: RE: Preventing concurrent execution of an Action
> 
> You might be able to get away with simply adding 'synchronized' to the
> execute method's signature.
> 
> 
> synchronized public ActionForward execute(...) {
>  updateXml();
> }
> 
> private void updateXml() { ... }
> 
> 
> The lock/key threading techniques have to do with, I think, a more
> complex threading issue than the one you've presented.
> 
> 
> - Dave
> 
> 
> > -Original Message-
> > From: Behrang Saeedzadeh [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, December 14, 2004 4:34 PM
> > To: [EMAIL PROTECTED]
> > Subject: Preventing concurrent execution of an Action
> >
> > Hi
> >
> > I've an action that creates an XML file on the server. It's executed
> > by clicking on a link. I don't want multiple instances of it to be
> > executed concurrently.
> >
> > Does an approach like the following work?
> >
> > public class UpdateXmlAction ...
> > {
> >private static final boolean locked;
> >
> >public void execute(...) {
> >
> >if (UpdateXmlAction.locked) {
> >return;
> >}
> >
> >Synchronized(UpdateXmlAction.class) {
> >locked = true;
> >updateXml();
> >locked = false;
> >}
> >
> >}
> > }
> >
> > Thanks in advance.
> >
> >
> > --
> >
> > Behrang Saeedzadeh
> > http://www.jroller.com/page/behrangsa
> >
> > -
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


AW: Preventing concurrent execution of an Action

2004-12-14 Thread Leon Rosenberg
Replace 
synchronized(UpdateXmlAction.class)
with 
synchronized(this) 

and you have the same behaviour, as if you would make the whole method
synchronized. 
You don't need your locked flag, it's provided by java object monitors
automatically. 

BUT, synchronizing doExecute in an action is a _very_ bad idea, since the
actions are in fact designed to have multiple threads running with it (it
would work in most cases, but it's fine).

Better in your case: just make updateXml() synchronized.

Ah, and also: if you have basic variables you check in different threads,
make them volatile (and don't make them static, since you have exactly one
instance of an action, it could be an instance variable in this case).

Last question: if two users click the link simultaneously, what should
happen? In your code, the second user overwrites the changes of the first
user (silently).

Regards
Leon


> -Ursprüngliche Nachricht-
> Von: Durham David R Jr Contr 805 CSPTS/SCE
> [mailto:[EMAIL PROTECTED]
> Gesendet: Dienstag, 14. Dezember 2004 23:46
> An: Struts Users Mailing List
> Betreff: RE: Preventing concurrent execution of an Action
> 
> You might be able to get away with simply adding 'synchronized' to the
> execute method's signature.
> 
> 
> synchronized public ActionForward execute(...) {
>  updateXml();
> }
> 
> private void updateXml() { ... }
> 
> 
> The lock/key threading techniques have to do with, I think, a more
> complex threading issue than the one you've presented.
> 
> 
> - Dave
> 
> 
> > -Original Message-
> > From: Behrang Saeedzadeh [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, December 14, 2004 4:34 PM
> > To: [EMAIL PROTECTED]
> > Subject: Preventing concurrent execution of an Action
> >
> > Hi
> >
> > I've an action that creates an XML file on the server. It's executed
> > by clicking on a link. I don't want multiple instances of it to be
> > executed concurrently.
> >
> > Does an approach like the following work?
> >
> > public class UpdateXmlAction ...
> > {
> >private static final boolean locked;
> >
> >public void execute(...) {
> >
> >if (UpdateXmlAction.locked) {
> >return;
> >}
> >
> >Synchronized(UpdateXmlAction.class) {
> >locked = true;
> >updateXml();
> >locked = false;
> >}
> >
> >}
> > }
> >
> > Thanks in advance.
> >
> >
> > --
> >
> > Behrang Saeedzadeh
> > http://www.jroller.com/page/behrangsa
> >
> > -
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



AW: Preventing concurrent execution of an Action

2004-12-14 Thread Leon Rosenberg
Replace 
synchronized(UpdateXmlAction.class)
with 
synchronized(this) 

and you have the same behaviour, as if you would make the whole method
synchronized. 
You don't need your locked flag, it's provided by java object monitors
automatically. 

BUT, synchronizing doExecute in an action is a _very_ bad idea, since the
actions are in fact designed to have multiple threads running with it (it
would work in most cases, but it's fine).

Better in your case: just make updateXml() synchronized.

Ah, and also: if you have basic variables you check in different threads,
make them volatile (and don't make them static, since you have exactly one
instance of an action, it could be an instance variable in this case).

Last question: if two users click the link simultaneously, what should
happen? In your code, the second user overwrites the changes of the first
user (silently).

Regards
Leon


> -Ursprüngliche Nachricht-
> Von: Durham David R Jr Contr 805 CSPTS/SCE
> [mailto:[EMAIL PROTECTED]
> Gesendet: Dienstag, 14. Dezember 2004 23:46
> An: Struts Users Mailing List
> Betreff: RE: Preventing concurrent execution of an Action
> 
> You might be able to get away with simply adding 'synchronized' to the
> execute method's signature.
> 
> 
> synchronized public ActionForward execute(...) {
>  updateXml();
> }
> 
> private void updateXml() { ... }
> 
> 
> The lock/key threading techniques have to do with, I think, a more
> complex threading issue than the one you've presented.
> 
> 
> - Dave
> 
> 
> > -Original Message-
> > From: Behrang Saeedzadeh [mailto:[EMAIL PROTECTED]
> > Sent: Tuesday, December 14, 2004 4:34 PM
> > To: [EMAIL PROTECTED]
> > Subject: Preventing concurrent execution of an Action
> >
> > Hi
> >
> > I've an action that creates an XML file on the server. It's executed
> > by clicking on a link. I don't want multiple instances of it to be
> > executed concurrently.
> >
> > Does an approach like the following work?
> >
> > public class UpdateXmlAction ...
> > {
> >private static final boolean locked;
> >
> >public void execute(...) {
> >
> >if (UpdateXmlAction.locked) {
> >return;
> >}
> >
> >Synchronized(UpdateXmlAction.class) {
> >locked = true;
> >updateXml();
> >locked = false;
> >}
> >
> >}
> > }
> >
> > Thanks in advance.
> >
> >
> > --
> >
> > Behrang Saeedzadeh
> > http://www.jroller.com/page/behrangsa
> >
> > -
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> 
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]