This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Singleton class for Database Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Singleton class for Database Connection" Watch "Singleton class for Database Connection" New topic
Author

Singleton class for Database Connection

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .


Save India From Corruption - Anna Hazare.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Jun 16, 2011
Posts: 70
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

Joined: Jan 10, 2002
Posts: 60766
    
  65

In many connection pool implementations, the close() method is overloaded to return the connection to the pool rather than actually close the connection.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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

Joined: Apr 18, 2009
Posts: 2234

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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

Joined: Oct 14, 2010
Posts: 218
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..


Thanks
Vipul Kumar
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton class for Database Connection
 
Similar Threads
Connection Pool
connection to DB
Improper use of singleton?
Understanding singleton pattern
Use of Singleton Classes