• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in closing connection

 
Amarjeet Anand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

let i have this class as a MODEL in jsp/servlet/Mysql project to get connection to database.



In another model class InsertToDatabase, i am using ConnectionProvider class to get connection to database and insert a record and after that I wanted to call ConnectionProvider.closeConnection() to close the opened connection for current user.

This InsertToDatabase class is called from a servlet.

Let user B requests for insertion of his record. At same time, user A also requests for insertion of his record.


Now I wanted to ask, since these methods and con variable are static, is there a chance of error at runtime when following sequence of steps ever happens....

1. User B requests for insertion and calls ConnectionProvider.getConnection().
2. user A requests for insertion and calls ConnectionProvider.getConnection().
3. user B's insert statement executes and closes the connection.
4. user A's insert statement executes and closes the connection.

So I think, at step 4, when A come to execute the insert statement it will see its connection closed (closed by B) because con is static variable.
As i am testing it locally, it gives good results. But at runtime, there will be a lot of users requesting for the same operation and con connection variable is static , so i got this doubt...

If the problem occurs due to multi threading problems, would you please be kind enough to provide the solution or refer to a link for best practices of data insertion post.

Thanks for going through this and expecting you valuable response.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest thing I can think of for is to use java.lang.ThreadLocal so even if you use static methods, each user is in fact getting its own dedicated connection. See:the javadoc:

https://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a chance of error at runtime if you use static variables for that? Short answer: Yes.

Really you should be using a connection pool rather than trying to do it yourself, since you're just reinventing the wheel here (and as you suspect, getting yourself into trouble). And ideally you should be configuring the connection pool into whatever you're running these servlets in -- Tomcat, Websphere, whatever -- and using its built-in facilities for getting a connection from the pool. Don't forget to return the connection when you're finished, too.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a connection pool, not Rube Goldberg solutions.
 
Amarjeet Anand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your suggestions... I got it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!