You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Balažic <xe...@gmail.com> on 2009/11/17 16:43:53 UTC

synchronizing in tomcat

Hi!

Does tomcat offer any mechanism for locking servlet executions?

So if one servlet does:
 - get 10 EUR from account 1 (read account 1 balance; substract 10;
store new balance)
 - add 10 EUR to account 2 (...)

How can I make sure no other servlet does (read account 1 balance;
sustract 10; store new balance) at the same
time and cause lost account money?

The account data is in a database, accessed over hibernate (or
directly, if needed).

Regards,
David

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


Re: synchronizing in tomcat

Posted by Leon Rosenberg <ro...@googlemail.com>.
How would you do it in a non-servlet environment?

Exactly the same works here too.

regards
Leon

On Tue, Nov 17, 2009 at 4:43 PM, David Balažic <xe...@gmail.com> wrote:
> Hi!
>
> Does tomcat offer any mechanism for locking servlet executions?
>
> So if one servlet does:
>  - get 10 EUR from account 1 (read account 1 balance; substract 10;
> store new balance)
>  - add 10 EUR to account 2 (...)
>
> How can I make sure no other servlet does (read account 1 balance;
> sustract 10; store new balance) at the same
> time and cause lost account money?
>
> The account data is in a database, accessed over hibernate (or
> directly, if needed).
>
> Regards,
> David
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

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


RE: synchronizing in tomcat

Posted by Joseph Morgan <jo...@ignitesales.com>.
I completely agree with Chuck, and completely disagree with Neil.  

Never rely upon static state in servlet classes unless you have complete
control over class loading, which you shouldn't, otherwise you'd write
your own servlet container.

As Chuck says, it is not a Tomcat issue, but one for the DB.


-----Original Message-----
From: Caldarale, Charles R [mailto:Chuck.Caldarale@unisys.com] 
Sent: Tuesday, November 17, 2009 10:13 AM
To: Tomcat Users List
Subject: RE: synchronizing in tomcat

> From: Neil Aggarwal [mailto:neil@JAMMConsulting.com]
> Subject: RE: synchronizing in tomcat
> 
> At first thought, I would create a class
> with synchronized static methods to do each of your
> processing tasks.  That way, you are guaranteed
> only one method is executing at a time.

That's probably not going to work, since the posted scenario sounds more
like a shopping cart situation, requiring lockout across multiple
requests.  Regardless, this has nothing to do with Tomcat.

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


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


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


RE: synchronizing in tomcat

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Neil Aggarwal [mailto:neil@JAMMConsulting.com]
> Subject: RE: synchronizing in tomcat
> 
> At first thought, I would create a class
> with synchronized static methods to do each of your
> processing tasks.  That way, you are guaranteed
> only one method is executing at a time.

That's probably not going to work, since the posted scenario sounds more like a shopping cart situation, requiring lockout across multiple requests.  Regardless, this has nothing to do with Tomcat.

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


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


RE: synchronizing in tomcat

Posted by Neil Aggarwal <ne...@JAMMConsulting.com>.
> Does tomcat offer any mechanism for locking servlet executions?

I think you are going to have to take care of this in your
application code.

At first thought, I would create a class
with synchronized static methods to do each of your
processing tasks.  That way, you are guaranteed
only one method is executing at a time.

You have to be careful to avoid deadlock
so you better call the methods in a pre-set
order.

	Neil

--
Neil Aggarwal, (281)846-8957, http://UnmeteredVPS.net
Host your tomcat app on a CentOS VPS for only $25/month!
Unmetered bandwidth, 7 day no risk trial, Google Checkout


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


Re: synchronizing in tomcat

Posted by Pid <pi...@pidster.com>.
On 17/11/2009 15:43, David Balažic wrote:
> Hi!
>
> Does tomcat offer any mechanism for locking servlet executions?
>
> So if one servlet does:
>   - get 10 EUR from account 1 (read account 1 balance; substract 10;
> store new balance)
>   - add 10 EUR to account 2 (...)
>
> How can I make sure no other servlet does (read account 1 balance;
> sustract 10; store new balance) at the same
> time and cause lost account money?
>
> The account data is in a database, accessed over hibernate (or
> directly, if needed).

The term you are looking for is "transaction".  GIYF.


p



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


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