“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
A much better solution is to use one of the many implementations of a "connection pool".
Originally posted by Jesper de Jong:
And Frank and Paul didn't even mention the thread concurrency issues you'll get yourself into.
Database connections cannot be used in multiple threads simultaneously. If multiple clients are connecting to your servlet at the same time, the servlet container will run your servlet in multiple threads at the same time and you will get into trouble if you use the same database connection at the same time for all clients.
Originally posted by rathi ji:
Could you please explain this a bit more. What problem can occur?
There is no emoticon for what I am feeling!
Originally posted by rathi ji:
Could you please explain this a bit more. What problem can occur?
Originally posted by Jesper de Jong:
The Connection object simply wasn't made for concurrent access by multiple threads. The Connection object represents a single connection to a database. Suppose that two threads try to send an SQL statement to the database simultaneously through the same connection. That is like two people trying to talk through the same telephone to someone on the other side. The person on the other side most likely isn't going to understand what they're saying.
Originally posted by rathi ji:
But If I am creating Connection object in doGet() method (local object) then every thread will gets it's own copy. So it is not sharing of same Connection object....
The problem may occurs if there are too many Connection objects are alive (too many thread are alive).....
Hope my point is clear.
So if the application is not for too many user then we can create Connection object inside doGet() method and use it....
Originally posted by Ben Souther:
You're going to have even more problems if all of your threads are trying to share a single connection.
Originally posted by rathi ji:
How can it be possible if I am creating Connection object locally. Every thread has it's own copy.
Jesper de Jong wrote:Connection pooling
You can just call "conn.close()" on the connection like in the example above - when you do that, the connection is not really closed; it's only returned to the pool.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |