This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes Synchronization or Lock? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization or Lock?" Watch "Synchronization or Lock?" New topic
Author

Synchronization or Lock?

bilal haider
Ranch Hand

Joined: Feb 08, 2011
Posts: 47

I have a database table that logs the changes in other table. table structure is as following.
Log_Table(id, table_name, operation, flag)
values(1,Customer,1,1);
values(2,Customer,2,1);
values(3,Customer,1,1);
values(4,Customer,2,1);
values(5,Customer,1,1);

I update this table against a button on a web page. by doing following operations.

public List<Long> select_Changes()
{
1) select id from Log_Table where flag =1;
}
public void update_table(List<Long> ids)
{

2)update Log_Table set flag =0 where id in( ids)
}

Problem is that between first and second operation its up to the user to perform the operation. Meanwhile another user at same time does the same operations. now i want that for this user already selected rows for first user should not be selected by the second user. e.g second user when do the first operation should get

values(6,Customer,2,1);
values(7,Customer,1,1);

Please suggest what should i do? I need to lock the rows for any kind of operation after rows are get selected. I tried select for update clause but it did not solve the problem. Its in a web application.
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

There are many unknowns in your questions which potentially influence the solution you are looking for.

What you are trying to achieve seems to be this

1. User1 gets the list and is currently looking at. He/She still thinking what to do next
2. User2 also tries to get the list. But since the data needs action from user1, user2 is not even shown the list and has to wait.

In this scenario,

1. How do you know if user1 is DEFINITELY does the second action i.e. update log table ?
2. Even if user1 does the second action, is it allowed to block user2 till that time ?

My gut feeling is that you are far from thread related scenario and more into the database transactions. See if you can try something at transaction level and get help in that forum.


Warm Regards,
Rakesh
bilal haider
Ranch Hand

Joined: Feb 08, 2011
Posts: 47

yes i have to post it in transaction forum.
You understand my problem statement very well.
I am trying to achieve

1. User1 gets the list and is currently looking at. He/She still thinking what to do next
2. User2 also tries to get the list. But since the data needs action from user1, user2 is not even shown the list and has to wait.

And i am having trouble in knowing

1. How do you know if user1 is DEFINITELY does the second action i.e. update log table ?
2. Even if user1 does the second action, is it allowed to block user2 till that time ?
Answer for second point is yes i want to block user2. And if dont want to block then what is the solution?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization or Lock?