• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton class for Database Connection

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i write a Singleton class for obtaining a Database connection . Assume that there are 100 Usrs accessing the Application , and if one user closes the connection , for the other 99 users will the connection would be closed or not ??

Please guide me .
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using a connection pool and not a single connection since you have multiple users. In that case close() returns the connection to the pool rather than closing it.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya m I will be using a Connection pool to obtain a Database connection


(This is only same code which may contain errros )
Sample code



Please tell me now if a class obtains connection from this class and closes it , then what will happen to other users ??
 
scott miles
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote: In that case close() returns the connection to the pool rather than closing it.


Hi jeanne at http://www.coderanch.com/t/543975/JDBC/java/Getting-connection-pool Ulf said you should not close the connection Rather you should return it to pool explicitly. Not sure actually should we close the connection in case of connection pool/datasource.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64964
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In many connection pool implementations, the close() method is overloaded to return the connection to the pool rather than actually close the connection.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the connection to "other users". Aren't they all calling getConnection() themselves? If so, this gives them a separate connection.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scott miles wrote:
Jeanne Boyarsky wrote: In that case close() returns the connection to the pool rather than closing it.


Hi jeanne at http://www.coderanch.com/t/543975/JDBC/java/Getting-connection-pool Ulf said you should not close the connection Rather you should return it to pool explicitly. Not sure actually should we close the connection in case of connection pool/datasource.

All the connection pool implementations I have seen have wrapped the close method to return it to the connection pool. I'll reply more in the other thread.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the connection to "other users". Aren't they all calling getConnection() themselves? If so, this gives them a separate connection.


In this case the Singleton class is used for obtaining Database connection form COnnection Pooling .
so there will be only one instance of the Object and if one user closes the doesn't it close other user connections ??

Now as per my knowledge i understand that if one User needs to get only one Databse connection , then singleton is applicable there .

Means one connection per one User , but not for the Entire Application .

Please correct me if i am wrong .



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could put a connection in a thread local variable (per user) and close it befpre sending the connection back to the client.
 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,

Here each user will get individual database connection.Here you are making GetConnection class as Singleton and not connection object.
so no need to worry of connection closing..
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here each user will get individual database connection.Here you are making GetConnection class as Singleton and not connection object.


Excellent answer , this was the thing i was expecting , Thank you very much .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic