Hi I have a question about multithreading in web app. In my app I have a Controller which passes parameters to a Business Logic class and it talks to the database, basically It loads the data to db and then make some query and receive the answer. So, I wanted to make those methods synchronised so that only one client could load the data to the database but now I am really confused because normally every request fires new thread(I think from what I have read) and I am afraid that for eg. Lets say I have two clients and client1 works with thread 1 and he accesses the new thread that is created with the help of Runnable Interface with synchronised method and client 2 works with thread 2 I am afraid that he can also access synchronised method because he has got his own thread. That has nothing to do with thread1. Is it possible that 2 clients will be able to access those synchronised methods of their own? Or should I destroy new Thread that I am creating and make all classes synchronised. So I am so confused how to protect a database from two clients writing to it? Thanks in advance.
-
3I highly recommend you to read the following: [How do servlets work? Instantiation, shared variables and multithreading](http://stackoverflow.com/q/3106452/1065197), specially the part **Threadsafety**. Please avoid manual synchronization of methods on a web application because it's a bad practice. – Luiggi Mendoza Sep 22 '15 at 22:29
-
You need to provide a code sample. What you're asking is not totally clear (ie are you synchronizing on the object or the class). – David Thielen Sep 22 '15 at 22:31
-
You shouldn't be creating any threads for this. You shouldn't be doing any synchronization. Each request should process normally, and let the database figure out if there are any concurrency issues. The database may error out one (or both) transactions if a conflict occurs, or may delay one until the other completes. The exact action taken by the database depends on the Transaction Isolation Level of the database connections. – Andreas Sep 22 '15 at 22:33
-
You need to control this with a transaction manager and isolation, not manual threads. You should write your code is if it were single threaded and let the app server deal with threads. – duffymo Sep 22 '15 at 22:34
2 Answers
Apache ActiveMQ can solve your problem.
What you will be doing is configure/install Apache ActiveMQ on your webserver. In that you can create a queue (say DBrequests) which will be responsible for holding your queries to be posted to database.
There will be Consumers and Producers. In your case client1 and client2 are producers (i.e it produces requests to be posted to databases). Your thread will put the requests into Queue.
Now your consumer will come into effect. Consumer thread will always be listening to queue if there is any message. Once the message is available you can use that message to fire a database query.
There is a hello world example here: http://activemq.apache.org/hello-world.html
You can do something like this. Using the parameters from controller build a query in HelloWorldProducer and fire that query in HelloWorldConsumer. Your problem should be solved.

- 169
- 11
Most databases synchronize requests, so as long as you put database statements in the same transaction if they need to deal with a self consistent database view, and as long as the database isolation level is set appropriately, you don't need to worry about thread synchronization. The exception would be if you use SQLite, which doesn't really support multithreading.

- 8,790
- 3
- 30
- 44