Re: method level synchronization doesn't work
My feeling on this is, doing a sync on a data bean in this specific case is not worth it. If the transaction is complex, which this case isn't, use something like java transaction API. doing either a subquery or the prepared statement example is a far better way. doing a sync unnecessarily is a bad idea. I would advise not doing it, unless it's absolutely the last choice. peter On Fri, 1 Oct 2004 10:48:16 -0500, Mike Fuellbrandt <[EMAIL PROTECTED]> wrote: > Correct me if I'm wrong, but couldn't the code synchronize on a static > final member of the jsp? > > private static final String COUNTER_INCREMENT_LOCK = newString("lock"); > > and then you would use the second method from the first post to synchronize on? > > (It's been a long time since I considered synchronization so I could > be really wrong here...) > > Mike Fuellbrandt > > On Thu, 30 Sep 2004 16:43:33 -0500, QM <[EMAIL PROTECTED]> wrote: > > On Thu, Sep 30, 2004 at 05:31:25PM -0400, Malia Noori wrote: > > : Thank you for replying, but I would appreciate it if you could expand on > > : your explanation. Here is my original post: > > : > > : Actually, the data that I am modifying requires a transaction and > > : > synchronization. It increments a counter stored in the database. So, I > > : > have to do a select to get the current value, increment the counter, and > > : > then insert the new value. > > > > This needn't be done within code; you could perform a database-level > > transaction. For example, using raw SQL calls that would come down to: > > > > . set the Connection's autoCommit to false > > . do the DB work > > . call the Connection's commit() method > > > > I recall EJBs have their own transaction mechanism, and I'd be > > shocked[1] if something as popular as Hibernate didn't have one as well. > > Check the respective docs. > > > > Whatever the case, there should be no need to try to synchronize this at > > the app/code level. > > > > -QM > > > > [1] = I've been shocked before, though... ;) > > > > > > > > > > > > -- > > > > software -- http://www.brandxdev.net > > tech news -- http://www.RoarNetworX.com > > > > - > > 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]
Re: method level synchronization doesn't work
Correct me if I'm wrong, but couldn't the code synchronize on a static final member of the jsp? private static final String COUNTER_INCREMENT_LOCK = newString("lock"); and then you would use the second method from the first post to synchronize on? (It's been a long time since I considered synchronization so I could be really wrong here...) Mike Fuellbrandt On Thu, 30 Sep 2004 16:43:33 -0500, QM <[EMAIL PROTECTED]> wrote: > On Thu, Sep 30, 2004 at 05:31:25PM -0400, Malia Noori wrote: > : Thank you for replying, but I would appreciate it if you could expand on > : your explanation. Here is my original post: > : > : Actually, the data that I am modifying requires a transaction and > : > synchronization. It increments a counter stored in the database. So, I > : > have to do a select to get the current value, increment the counter, and > : > then insert the new value. > > This needn't be done within code; you could perform a database-level > transaction. For example, using raw SQL calls that would come down to: > > . set the Connection's autoCommit to false > . do the DB work > . call the Connection's commit() method > > I recall EJBs have their own transaction mechanism, and I'd be > shocked[1] if something as popular as Hibernate didn't have one as well. > Check the respective docs. > > Whatever the case, there should be no need to try to synchronize this at > the app/code level. > > -QM > > [1] = I've been shocked before, though... ;) > > > > > -- > > software -- http://www.brandxdev.net > tech news -- http://www.RoarNetworX.com > > - > 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: method level synchronization doesn't work
mysql definitely supports retrieval of generated keys through jdbc. http://dev.mysql.com/tech-resources/articles/autoincrement-with-connectorj.h tml > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: Friday, October 01, 2004 10:09 AM > To: [EMAIL PROTECTED] > Cc: Tomcat Users List > Subject: Re: method level synchronization doesn't work > > > On Fri, Oct 01, 2004 at 04:05:53PM +1200, [EMAIL PROTECTED] wrote: > > Is your primary key generated automatically? > > If so, the combined insert/query is: > > > >String stmt="INSERT INTO SOMEFILE (FLD1,FLD2,FLD3) > > VALUES(?,?,?)"; > >PreparedStatement sql = > > conn.prepareStatement(stmt,Statement.RETURN_GENERATED_KEYS); > > Does mysql actually support this? I know postgresql > does not support returning generated keys, but I was able to > work around it by tacking a select from the sequence > immediately after the insert: e.g. "insert into foo ... ; > select currval('foo_keyfield_seq');" Is postgres just behind > the times? > > eric > > - > 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: method level synchronization doesn't work
On Fri, Oct 01, 2004 at 04:05:53PM +1200, [EMAIL PROTECTED] wrote: > Is your primary key generated automatically? > If so, the combined insert/query is: > >String stmt="INSERT INTO SOMEFILE (FLD1,FLD2,FLD3) > VALUES(?,?,?)"; >PreparedStatement sql = > conn.prepareStatement(stmt,Statement.RETURN_GENERATED_KEYS); Does mysql actually support this? I know postgresql does not support returning generated keys, but I was able to work around it by tacking a select from the sequence immediately after the insert: e.g. "insert into foo ... ; select currval('foo_keyfield_seq');" Is postgres just behind the times? eric - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: method level synchronization doesn't work
Yup, latest 4.1 does support subqueries. But the folks around here have something against "gamma". ;) Actually.. that's me, but we've got 4.1 installed on our dev box and are playing. And I do this the simplest way... I use autoincrement. But I'd love subqueries for other reasons. > -Original Message- > From: Peter Lin [mailto:[EMAIL PROTECTED] > Sent: Thursday, September 30, 2004 10:24 PM > To: Tomcat Users List > Subject: Re: method level synchronization doesn't work > > > that would explain some of the problems you're having. I > thought the latest mysql supports subqueries. > > http://dev.mysql.com/doc/mysql/en/Subqueries.html > > > if you're not using the latest mysql, it might be a good idea > to upgrade, so you can do this the simple way. > > I hope that helps > > peter - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: method level synchronization doesn't work
yup, that's letting the auto_increment do its job (which is what I do). some folks don't believe in those, and use a separate primary key generating scheme. Many a time I've heard of this "update and retrieve in one select statement", and I think it always has a vaguely Oracle'ish feel to it. So whenever I hear of someone say something like "use one query to generate your next pk".. I always wonder. > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: Thursday, September 30, 2004 11:06 PM > To: Tomcat Users List > Subject: RE: method level synchronization doesn't work > > > > > > > > > Is your primary key generated automatically? > If so, the combined insert/query is: > >String stmt="INSERT INTO SOMEFILE (FLD1,FLD2,FLD3) > VALUES(?,?,?)"; >PreparedStatement sql = > conn.prepareStatement(stmt,Statement.RETURN_GENERATED_KEYS); >sql.setInt(1,fld1); >sql.setInt(2,fld2); >sql.setInt(3,fld3); >sql.execute(); >ResultSet rs = sql.getGeneratedKeys(); >if (rs.next()) { > // get generated key > int key=rs.getInt(1); >} > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: method level synchronization doesn't work
Is your primary key generated automatically? If so, the combined insert/query is: String stmt="INSERT INTO SOMEFILE (FLD1,FLD2,FLD3) VALUES(?,?,?)"; PreparedStatement sql = conn.prepareStatement(stmt,Statement.RETURN_GENERATED_KEYS); sql.setInt(1,fld1); sql.setInt(2,fld2); sql.setInt(3,fld3); sql.execute(); ResultSet rs = sql.getGeneratedKeys(); if (rs.next()) { // get generated key int key=rs.getInt(1); } |-+> | | "Mike Curwen"| | | <[EMAIL PROTECTED]| | | ndless.com> | | || | | 01/10/2004 02:14 PM | | | Please respond to| | | "Tomcat Users List" | | || |-+> >--| | | | To: "'Tomcat Users List'" <[EMAIL PROTECTED]> | | cc: | | Subject: RE: method level synchronization doesn't work | >--| Ok, since it's been brought forward... how do you update and retrieve all in one query? This is for: "I need a new primary key, and I need it to be thread safe." something that would run on mysql? unless you can get this magic query, it's not quite as simple as "let the databse handle it", is it? > -Original Message- > From: Peter Lin [mailto:[EMAIL PROTECTED] > Sent: Thursday, September 30, 2004 7:42 PM > To: Tomcat Users List; [EMAIL PROTECTED] > Subject: Re: method level synchronization doesn't work > > > that would be the easiest way. > > but you made it too easy, I thought people are suppose to > suffer and stumble :) > > peter > > > On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti > <[EMAIL PROTECTED]> wrote: > > Why not just let SQL do the update? Do the following: > > > > update my_table set counter_field = counter_field + 1 (where clause > > here if needed) > > > > (off the top of my head, my syntax might be off) > > > > In other words, why not let the database handle the > concurrency issue? > > That's kind of what they're there for (partly anyway). > > > > - > 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]
Re: method level synchronization doesn't work
that would explain some of the problems you're having. I thought the latest mysql supports subqueries. http://dev.mysql.com/doc/mysql/en/Subqueries.html if you're not using the latest mysql, it might be a good idea to upgrade, so you can do this the simple way. I hope that helps peter On Thu, 30 Sep 2004 21:14:07 -0500, Mike Curwen <[EMAIL PROTECTED]> wrote: > Ok, since it's been brought forward... > > how do you update and retrieve all in one query? This is for: "I need a new > primary key, and I need it to be thread safe." > > something that would run on mysql? > > unless you can get this magic query, it's not quite as simple as "let the > databse handle it", is it? > > > > > > -Original Message- > > From: Peter Lin [mailto:[EMAIL PROTECTED] > > Sent: Thursday, September 30, 2004 7:42 PM > > To: Tomcat Users List; [EMAIL PROTECTED] > > Subject: Re: method level synchronization doesn't work > > > > > > that would be the easiest way. > > > > but you made it too easy, I thought people are suppose to > > suffer and stumble :) > > > > peter > > > > > > On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti > > <[EMAIL PROTECTED]> wrote: > > > Why not just let SQL do the update? Do the following: > > > > > > update my_table set counter_field = counter_field + 1 (where clause > > > here if needed) > > > > > > (off the top of my head, my syntax might be off) > > > > > > In other words, why not let the database handle the > > concurrency issue? > > > That's kind of what they're there for (partly anyway). > > > > > > > - > > 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]
Re: method level synchronization doesn't work
Well, I'm not sure it was stated that a return value was needed. The way I read the original post was basically (to paraphrase): "I need to update a counter field, and the way I intend to do it is to retrieve the current value, modify it, and then write it out again as two separate queries". If that's the scenario, I don't see any reason the SQL statement I gave (assuming I got the syntax right) wouldn't work just fine. If your saying that you need to do the update and then retrieve the value for some reason, then I agree, it's not as straight-forward. One thing that MIGHT work, although I'd frankly have to go try it myself to be sure, is a subquery, something along the lines of: select (update table set counter=counter+1) as counter from table Even if the theory is correct, I'm not sure about how database-independant it may or may not be. -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com Mike Curwen wrote: Ok, since it's been brought forward... how do you update and retrieve all in one query? This is for: "I need a new primary key, and I need it to be thread safe." something that would run on mysql? unless you can get this magic query, it's not quite as simple as "let the databse handle it", is it? -Original Message- From: Peter Lin [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 7:42 PM To: Tomcat Users List; [EMAIL PROTECTED] Subject: Re: method level synchronization doesn't work that would be the easiest way. but you made it too easy, I thought people are suppose to suffer and stumble :) peter On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti <[EMAIL PROTECTED]> wrote: Why not just let SQL do the update? Do the following: update my_table set counter_field = counter_field + 1 (where clause here if needed) (off the top of my head, my syntax might be off) In other words, why not let the database handle the concurrency issue? That's kind of what they're there for (partly anyway). - 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]
RE: method level synchronization doesn't work
Ok, since it's been brought forward... how do you update and retrieve all in one query? This is for: "I need a new primary key, and I need it to be thread safe." something that would run on mysql? unless you can get this magic query, it's not quite as simple as "let the databse handle it", is it? > -Original Message- > From: Peter Lin [mailto:[EMAIL PROTECTED] > Sent: Thursday, September 30, 2004 7:42 PM > To: Tomcat Users List; [EMAIL PROTECTED] > Subject: Re: method level synchronization doesn't work > > > that would be the easiest way. > > but you made it too easy, I thought people are suppose to > suffer and stumble :) > > peter > > > On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti > <[EMAIL PROTECTED]> wrote: > > Why not just let SQL do the update? Do the following: > > > > update my_table set counter_field = counter_field + 1 (where clause > > here if needed) > > > > (off the top of my head, my syntax might be off) > > > > In other words, why not let the database handle the > concurrency issue? > > That's kind of what they're there for (partly anyway). > > > > - > 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: method level synchronization doesn't work
that would be the easiest way. but you made it too easy, I thought people are suppose to suffer and stumble :) peter On Thu, 30 Sep 2004 17:46:51 -0400, Frank W. Zammetti <[EMAIL PROTECTED]> wrote: > Why not just let SQL do the update? Do the following: > > update my_table set counter_field = counter_field + 1 (where clause here > if needed) > > (off the top of my head, my syntax might be off) > > In other words, why not let the database handle the concurrency issue? > That's kind of what they're there for (partly anyway). > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: method level synchronization doesn't work
Why not just let SQL do the update? Do the following: update my_table set counter_field = counter_field + 1 (where clause here if needed) (off the top of my head, my syntax might be off) In other words, why not let the database handle the concurrency issue? That's kind of what they're there for (partly anyway). Malia Noori wrote: Thank you for replying, but I would appreciate it if you could expand on your explanation. Here is my original post: Actually, the data that I am modifying requires a transaction and synchronization. It increments a counter stored in the database. So, I have to do a select to get the current value, increment the counter, and then insert the new value. So if two threads are accessing it at the same time, the counter will not be properly incremented. What's puzzling is that method level synchronization does not work while synchronizing on a block of code inside a method works. Regardless of what persistence mechanism I use (i.e. Hibernate, EJB, etc), I have to synchronize on the method that increments the counter stored in the database. I can't avoid this due to business logic. Please let me know if there is alternative to synchronization. Thanks, Malia -Original Message- From: QM [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 3:01 PM To: Tomcat Users List Subject: Re: method level synchronization doesn't work On Thu, Sep 30, 2004 at 01:14:26PM -0400, Malia Noori wrote: : I thought Tomcat instantiates only 1 instance of JSP servlet to handle all : requests. Isn't that the reason why it's not suggested to have instance : variables? Close, but not quite. ;) The servlet spec permits a container to create as many instances of a servlet class as it sees fit. The same goes for creating and destroying a servlet object several times over a context (webapp) lifetime. That said, a servlet's main methods -- doGet(), doPost(), service() -- may be called concurrently for the same servlet object. These methods are thus meant to be thread-safe, and as such they shouldn't modify object instance variables. (It's fine to call such variables, as long as they are read-only or at least treated as such.) : If it is the case that 2 instance of JSP servlets gets created : to handle each request, what's the best object to synchronize on? I missed your original post, so I can say only this: when you find yourself painted into a corner, it's time to revisit the design stage. -QM -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: method level synchronization doesn't work
On Thu, Sep 30, 2004 at 05:31:25PM -0400, Malia Noori wrote: : Thank you for replying, but I would appreciate it if you could expand on : your explanation. Here is my original post: : : Actually, the data that I am modifying requires a transaction and : > synchronization. It increments a counter stored in the database. So, I : > have to do a select to get the current value, increment the counter, and : > then insert the new value. This needn't be done within code; you could perform a database-level transaction. For example, using raw SQL calls that would come down to: . set the Connection's autoCommit to false . do the DB work . call the Connection's commit() method I recall EJBs have their own transaction mechanism, and I'd be shocked[1] if something as popular as Hibernate didn't have one as well. Check the respective docs. Whatever the case, there should be no need to try to synchronize this at the app/code level. -QM [1] = I've been shocked before, though... ;) -- software -- http://www.brandxdev.net tech news -- http://www.RoarNetworX.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: method level synchronization doesn't work
Thank you for replying, but I would appreciate it if you could expand on your explanation. Here is my original post: Actually, the data that I am modifying requires a transaction and > synchronization. It increments a counter stored in the database. So, I > have to do a select to get the current value, increment the counter, and > then insert the new value. So if two threads are accessing it at the same > time, the counter will not be properly incremented. What's puzzling is that > method level synchronization does not work while synchronizing on a block of > code inside a method works. > Regardless of what persistence mechanism I use (i.e. Hibernate, EJB, etc), I have to synchronize on the method that increments the counter stored in the database. I can't avoid this due to business logic. Please let me know if there is alternative to synchronization. Thanks, Malia -Original Message- From: QM [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 3:01 PM To: Tomcat Users List Subject: Re: method level synchronization doesn't work On Thu, Sep 30, 2004 at 01:14:26PM -0400, Malia Noori wrote: : I thought Tomcat instantiates only 1 instance of JSP servlet to handle all : requests. Isn't that the reason why it's not suggested to have instance : variables? Close, but not quite. ;) The servlet spec permits a container to create as many instances of a servlet class as it sees fit. The same goes for creating and destroying a servlet object several times over a context (webapp) lifetime. That said, a servlet's main methods -- doGet(), doPost(), service() -- may be called concurrently for the same servlet object. These methods are thus meant to be thread-safe, and as such they shouldn't modify object instance variables. (It's fine to call such variables, as long as they are read-only or at least treated as such.) : If it is the case that 2 instance of JSP servlets gets created : to handle each request, what's the best object to synchronize on? I missed your original post, so I can say only this: when you find yourself painted into a corner, it's time to revisit the design stage. -QM -- software -- http://www.brandxdev.net tech news -- http://www.RoarNetworX.com - 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: method level synchronization doesn't work
On Thu, Sep 30, 2004 at 01:14:26PM -0400, Malia Noori wrote: : I thought Tomcat instantiates only 1 instance of JSP servlet to handle all : requests. Isn't that the reason why it's not suggested to have instance : variables? Close, but not quite. ;) The servlet spec permits a container to create as many instances of a servlet class as it sees fit. The same goes for creating and destroying a servlet object several times over a context (webapp) lifetime. That said, a servlet's main methods -- doGet(), doPost(), service() -- may be called concurrently for the same servlet object. These methods are thus meant to be thread-safe, and as such they shouldn't modify object instance variables. (It's fine to call such variables, as long as they are read-only or at least treated as such.) : If it is the case that 2 instance of JSP servlets gets created : to handle each request, what's the best object to synchronize on? I missed your original post, so I can say only this: when you find yourself painted into a corner, it's time to revisit the design stage. -QM -- software -- http://www.brandxdev.net tech news -- http://www.RoarNetworX.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: method level synchronization doesn't work
I thought Tomcat instantiates only 1 instance of JSP servlet to handle all requests. Isn't that the reason why it's not suggested to have instance variables? If it is the case that 2 instance of JSP servlets gets created to handle each request, what's the best object to synchronize on? Synchronizing on empty string is not the best option because any other block of code using an empty string will cause thread contention. Thank you for your help! -Malia -Original Message- From: Jon Wingfield [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 11:04 AM To: Tomcat Users List Subject: Re: method level synchronization doesn't work I'd be surprised if synchronization was broken. Given one assumption I think I can explain what you are seeing. Assumption: The two concurrent requests are handled by two different instances of a jsp servlet. If there are two instances then each request will be able to sucsessfully obtain the monitor when calling the synchronized methods. So, why does using a synchronized block work? It works because you are synchronizing on a literal string. Literal strings get interned into a pool of objects by the jvm. So the mutex reference actually points to the same piece of memory for both instances of your jsp. Hence the synchronization works. Jon Malia Noori wrote: > Actually, the data that I am modifying requires a transaction and > synchronization. It increments a counter stored in the database. So, I > have to do a select to get the current value, increment the counter, and > then insert the new value. So if two threads are accessing it at the same > time, the counter will not be properly incremented. What's puzzling is that > method level synchronization does not work while synchronizing on a block of > code inside a method works. > > Thanks, > Malia > > -Original Message- > From: Peter Lin [mailto:[EMAIL PROTECTED] > Sent: Thursday, September 30, 2004 10:26 AM > To: Tomcat Users List; [EMAIL PROTECTED] > Subject: Re: method level synchronization doesn't work > > am I missing something, but looks like you're trying to build some > kind of web cache. why not use Hibernate or something that already > does caching for you instead? > > the only time I can see a need to sync, is if the request contains > data that requires a transaction. Which in that case, you're better > off doing an insert, then a second select query with the same > connection. > > or is the scenario a distributed objects setup? > > peter > > > On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori > <[EMAIL PROTECTED]> wrote: > >>Hi, >> >>I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC. > > I > >>have a JSP that calls a web bean. This web bean has a section of code > > that > >>I want to synchronize, but when I synchronize the method, Tomcat doesn't >>actually synchronize the threads. (I tried this by having 2 users access > > my > >>JSP at the same time). When I synchronize the code in the method by using > > a > >>mutex, it works. >> >>So, doing this doesn't work: >> >>public synchronized void amethod() >> >>{ >> >> //some code that access the database and needs to be synchronized >> >>} >> >>But doing this works: >> >>public void amethod() >> >>{ >> >>String mutex = ""; >> >>synchronize(mutex) >> >>{ >> >> //some code that access the database and needs to be synchronized >> >>} >> >>} >> >>Why does synchronization on the method doesn't work? Does Tomcat not > > allow > >>locking of object caused by method level synchronization? Any help will > > be > >>appreciated. >> >>Thanks, >> >>Malia >> - 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: method level synchronization doesn't work
you might want to consider using a stored procedure to make it look like a single call from tomcat. is the goal here to make sure the counter is incremented correctly? I'm guessing there's situations where the counter shouldn't be incremented? you can also use a subquery to increment the counter. there's plenty of articles on how to do this the simple way. I hope that helps peter On Thu, 30 Sep 2004 10:43:22 -0400, Malia Noori <[EMAIL PROTECTED]> wrote: > Actually, the data that I am modifying requires a transaction and > synchronization. It increments a counter stored in the database. So, I > have to do a select to get the current value, increment the counter, and > then insert the new value. So if two threads are accessing it at the same > time, the counter will not be properly incremented. What's puzzling is that > method level synchronization does not work while synchronizing on a block of > code inside a method works. > > Thanks, > Malia > > > > -Original Message- > From: Peter Lin [mailto:[EMAIL PROTECTED] > Sent: Thursday, September 30, 2004 10:26 AM > To: Tomcat Users List; [EMAIL PROTECTED] > Subject: Re: method level synchronization doesn't work > > am I missing something, but looks like you're trying to build some > kind of web cache. why not use Hibernate or something that already > does caching for you instead? > > the only time I can see a need to sync, is if the request contains > data that requires a transaction. Which in that case, you're better > off doing an insert, then a second select query with the same > connection. > > or is the scenario a distributed objects setup? > > peter > > On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori > <[EMAIL PROTECTED]> wrote: > > Hi, > > > > I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC. > I > > have a JSP that calls a web bean. This web bean has a section of code > that > > I want to synchronize, but when I synchronize the method, Tomcat doesn't > > actually synchronize the threads. (I tried this by having 2 users access > my > > JSP at the same time). When I synchronize the code in the method by using > a > > mutex, it works. > > > > So, doing this doesn't work: > > > > public synchronized void amethod() > > > > { > > > > //some code that access the database and needs to be synchronized > > > > } > > > > But doing this works: > > > > public void amethod() > > > > { > > > > String mutex = ""; > > > > synchronize(mutex) > > > > { > > > > //some code that access the database and needs to be synchronized > > > > } > > > > } > > > > Why does synchronization on the method doesn't work? Does Tomcat not > allow > > locking of object caused by method level synchronization? Any help will > be > > appreciated. > > > > Thanks, > > > > Malia > > > > > > - > 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]
Re: method level synchronization doesn't work
I'd be surprised if synchronization was broken. Given one assumption I think I can explain what you are seeing. Assumption: The two concurrent requests are handled by two different instances of a jsp servlet. If there are two instances then each request will be able to sucsessfully obtain the monitor when calling the synchronized methods. So, why does using a synchronized block work? It works because you are synchronizing on a literal string. Literal strings get interned into a pool of objects by the jvm. So the mutex reference actually points to the same piece of memory for both instances of your jsp. Hence the synchronization works. Jon Malia Noori wrote: Actually, the data that I am modifying requires a transaction and synchronization. It increments a counter stored in the database. So, I have to do a select to get the current value, increment the counter, and then insert the new value. So if two threads are accessing it at the same time, the counter will not be properly incremented. What's puzzling is that method level synchronization does not work while synchronizing on a block of code inside a method works. Thanks, Malia -Original Message- From: Peter Lin [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 10:26 AM To: Tomcat Users List; [EMAIL PROTECTED] Subject: Re: method level synchronization doesn't work am I missing something, but looks like you're trying to build some kind of web cache. why not use Hibernate or something that already does caching for you instead? the only time I can see a need to sync, is if the request contains data that requires a transaction. Which in that case, you're better off doing an insert, then a second select query with the same connection. or is the scenario a distributed objects setup? peter On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori <[EMAIL PROTECTED]> wrote: Hi, I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC. I have a JSP that calls a web bean. This web bean has a section of code that I want to synchronize, but when I synchronize the method, Tomcat doesn't actually synchronize the threads. (I tried this by having 2 users access my JSP at the same time). When I synchronize the code in the method by using a mutex, it works. So, doing this doesn't work: public synchronized void amethod() { //some code that access the database and needs to be synchronized } But doing this works: public void amethod() { String mutex = ""; synchronize(mutex) { //some code that access the database and needs to be synchronized } } Why does synchronization on the method doesn't work? Does Tomcat not allow locking of object caused by method level synchronization? Any help will be appreciated. Thanks, Malia - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: method level synchronization doesn't work
Actually, the data that I am modifying requires a transaction and synchronization. It increments a counter stored in the database. So, I have to do a select to get the current value, increment the counter, and then insert the new value. So if two threads are accessing it at the same time, the counter will not be properly incremented. What's puzzling is that method level synchronization does not work while synchronizing on a block of code inside a method works. Thanks, Malia -Original Message- From: Peter Lin [mailto:[EMAIL PROTECTED] Sent: Thursday, September 30, 2004 10:26 AM To: Tomcat Users List; [EMAIL PROTECTED] Subject: Re: method level synchronization doesn't work am I missing something, but looks like you're trying to build some kind of web cache. why not use Hibernate or something that already does caching for you instead? the only time I can see a need to sync, is if the request contains data that requires a transaction. Which in that case, you're better off doing an insert, then a second select query with the same connection. or is the scenario a distributed objects setup? peter On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori <[EMAIL PROTECTED]> wrote: > Hi, > > I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC. I > have a JSP that calls a web bean. This web bean has a section of code that > I want to synchronize, but when I synchronize the method, Tomcat doesn't > actually synchronize the threads. (I tried this by having 2 users access my > JSP at the same time). When I synchronize the code in the method by using a > mutex, it works. > > So, doing this doesn't work: > > public synchronized void amethod() > > { > > //some code that access the database and needs to be synchronized > > } > > But doing this works: > > public void amethod() > > { > > String mutex = ""; > > synchronize(mutex) > > { > > //some code that access the database and needs to be synchronized > > } > > } > > Why does synchronization on the method doesn't work? Does Tomcat not allow > locking of object caused by method level synchronization? Any help will be > appreciated. > > Thanks, > > Malia > > - 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: method level synchronization doesn't work
am I missing something, but looks like you're trying to build some kind of web cache. why not use Hibernate or something that already does caching for you instead? the only time I can see a need to sync, is if the request contains data that requires a transaction. Which in that case, you're better off doing an insert, then a second select query with the same connection. or is the scenario a distributed objects setup? peter On Thu, 30 Sep 2004 10:05:41 -0400, Malia Noori <[EMAIL PROTECTED]> wrote: > Hi, > > I am using Tomcat 4.1 and I am accessing MS SQL Server database via JDBC. I > have a JSP that calls a web bean. This web bean has a section of code that > I want to synchronize, but when I synchronize the method, Tomcat doesn't > actually synchronize the threads. (I tried this by having 2 users access my > JSP at the same time). When I synchronize the code in the method by using a > mutex, it works. > > So, doing this doesn't work: > > public synchronized void amethod() > > { > > //some code that access the database and needs to be synchronized > > } > > But doing this works: > > public void amethod() > > { > > String mutex = ""; > > synchronize(mutex) > > { > > //some code that access the database and needs to be synchronized > > } > > } > > Why does synchronization on the method doesn't work? Does Tomcat not allow > locking of object caused by method level synchronization? Any help will be > appreciated. > > Thanks, > > Malia > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]