It's not a secret anymore!*
The moose likes Servlets and the fly likes How to handle concurrent write/read to a db from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to handle concurrent write/read to a db from a servlet" Watch "How to handle concurrent write/read to a db from a servlet" New topic
Author

How to handle concurrent write/read to a db from a servlet

Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Hi all,
in my application i have two databases. lets call them a & b.from database a i will be just reading data and displaying it to the user using a sevlet.no writing has to be done.but on database b i have to perform writing as well as reading operations.since multiple users will be accessing this database i have a few questions
i) how do i take care of the cuncurrency issue?
ii)will the cuncurrency issue come in only when two users try to write something to the database at the same time or is it a problem even when multiple users try to read at the same time?
iii) should i implement connection pooling?i dont expect a lot of cuncurrent users(around 50 or so).is there any threshold value for the no. of concurrent users beyond which it is advisable to implement connection pooling.
Thanks in advance.
Subbu

I am using mySql and Jrun with IIS 4.0


Subbu
Robert van der Vliet
Greenhorn

Joined: Jun 30, 2001
Posts: 4
Hi Subbu,
Typically your db will take care of concurrent writes at a file level. This is unlikely to be a problem for you unless users regularly overwrite the same piece of data. The database is a bit of a black box this way unless you're doing something pretty demanding with it.
Database access with multiple users is easy to get wrong with servlets (I fell into every trap :-). DO implement connection pooling -> Marty Hall (from Sun) does a good one available at http://www.coreservlets.com/. Watch out, though, it doesn't handle reconnection to the database if your db goes down very well. Watch out for synchronizing your database access methods, too.
Also use a servlet engine that runs multiple threads for each servlet so that access to that servlet does not stop as all the users queue up around that one db access method (or use SingleThreadModel (but remember, it's expensive ito resources)).
Or you can just ignore all of this tiresome stuff and borrow someone else's and use Resin from www.caucho.com. They do a good connection pool all for you.
Cheers, Rob
 
jQuery in Action, 2nd edition
 
subject: How to handle concurrent write/read to a db from a servlet
 
Similar Threads
NX: cacheless design to keep things simple?
j2ee without ejb
How to handle concurrent write/read to a db
Question about shared DB connection
Database security