Kraken
Kraken

Reputation: 24233

Multiprocessing on web hosting

I have a java dynamic web app. I am exposing RESTful webservices for my android application.

The thing is that there are some services that do DB updates. Now, I want to host the application on public domain. I was wondering how parallel processing works on web hosting.

Say, my service /updateDB updates the database. Now, if there are two users who hit the same service at the same time, will the two of them run concurrently, because that will cause inconsistency in data. How exactly does the whole thing work.

Do I need to take care of synchronisation in my code?

Upvotes: 1

Views: 63

Answers (1)

kensen john
kensen john

Reputation: 5519

Why kind of database are you using? Certain database engines already have mechanisms in place to allow a transaction to be completed before another request over writes data. Most web developers do not have to worry about this because the application server (websphere, weblogic) and database (Mysql,Oracle) take care of these things for you.

(I am going to overly simplify this for you.) A request to the webservice may perform one or more actions on the DB. These actions can be clumped together and be called a transaction. A transaction can include one or more of the following INSERT, UPDATE, DELETE etc. e.g A new customer registers for your webservice. the following actions take place which can be considered into a transaction.

  1. Insert a new customer username password in the Customer table
  2. Insert customers address in Address table
  3. Update total customer count in Summary table

All the above actions can be completed as one transaction. If any of this fails then all actions will be reverted back automatically. Similarly if two customers registers simultaneously then the database will take care to not over write each other as well.

We can configure the database to make sure that every transaction should be completed before another transaction can dirty the data in a row.

In a database they are called ACID properties.

  • A - Atomicity - Every transaction must be complete, if anything in a transaction fails, then do not complete the transaction and also revert back every previous action within that transaction.
  • C - Consistency - make sure that every transaction that occurs will always update the database in a predefined manner. e.g. after every customer registration make sure that all the actions within it are executed
  • I - Isolation - if more than one request comes in, then they get executed on the database separately
  • D - Durabilty - after a transaction completes, the changes done should remain forever.

For example Mysql Database with the InnoDB engine supports this. There are other databases which support this as well.

You can read more here http://java.dzone.com/articles/beginners-guide-acid-and

This is a very vast topic in databases.

Programming language have APIS which will help you write code in this manner. But the basic take away is that databases and applications servers will do most of the work for you. You just have to make sure to design the code structure to identify transactions and commit them appropriately).

Java and other programming languages are aware of ACID properties in DB and will help you achieve that goal.

Read more here about how you use Java to achieve things we mentioned above. http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html

Similarly other languages have similar functionality and APIs.

In google search for "java database transaction" or "<your favorite language>database transaction"

Upvotes: 2

Related Questions