views:

57

answers:

1

We have implemented client server socket based application to process multiple shopping cart requests. Daily we receive thousands of shopping cart requests.

For this we implemented multi threaded architecture to process requests concurrently. We are using Oracle Connection Pool for data base operations and we set optimal value for connection pool size. As per our business process we have a main database table and we need to update same set of rows by multiple threads using multiple connection sessions concurrently. Now are getting some dead lock issues because of multiple threads will try to update the data on same rows using multiple connection sessions concurrently and also we are some other primary key violations on tables. Sometimes data base is also getting locked by inserting same data in multiple connection sessions concurrently.

Please suggest me good approach to handle above problems immediately.

+1  A: 

There are a few different general solutions to writing multithreaded code that does not encounter deadlocks. The simplest is to ensure that you always lock resources in the same order.

A deadlock occurs when one session holds a lock on A and wants a lock on B while another session holds a lock on B and wants a lock on A. If you ensure that your code always locks A before B (or B before A), you can be guaranteed that you won't have a deadlock.

As for your comment about primary key violations, are you using something other than an Oracle sequence to generate your primary keys? If so, that is almost certainly the problem. Oracle sequences are explicitly designed to provide unique primary keys in the case where you have multiple sessions doing simultaneous inserts.

Justin Cave
Thanks Cave for Prompt response. We implemented some wait and notify mechanism on specific code piece for each Thread. By this approach dead locks are not happening. But the disadvantage in this approach is if any thread takes significant time that will impact on other threads as this process will be a serial fashion.If we wrote all the database operations in Stored procedure, While all threads accessing this Store procedure, Is it place any lock for each thread?
Kiran Kumar
We are getting some primary violations in Child table not in Parent table. This issue we will take care by running process again. The main problem is Data base getting locked when each thread try to insert or update data Child table in each different connection session simultaneously. Even some times system was also getting hanged. We are adding all the insert or update sql statements in Batch. Actually in this scenario we should get dead lock but data base is not throwing any exceptions. For this process we have not implemented above wait and notify mechanism.
Kiran Kumar