File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes ConnectionPool or Singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ConnectionPool or Singleton" Watch "ConnectionPool or Singleton" New topic
Author

ConnectionPool or Singleton

Joy Chowdhury
Greenhorn

Joined: Oct 28, 2004
Posts: 4
EveryBody knows the concept of Connection Pooling and why it is used and blah blah..
but my question is that why can't we make a Singleton class of making a Connection and all other classes using it ..
Many people talk about memory and security issues ..If my aaplication don't have any issues with that ,can i go for a singleton Connection class and if not what are the disadvantages and why connection pooling concept outscore the singleton way of doing
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you used a singleton to provide a JDBC Connection you would effectively be serializing access to the database, because you'd have one and only one Connection for all your code to use. RDBMS's are multiuser by design, so why you would want to ignore this feature and make it a single user database I don't know.

I don't really see how Connection Pooling and "memory and security issues" are related. What memory and security features do you believe Connection Pooling gives you?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Joy Chowdhury
Greenhorn

Joined: Oct 28, 2004
Posts: 4
I know RDBMS are multiuser by design but if my application is small don't you think it is better to work with a single connection and not going for a connection pool which byitself makes the server slow and opening and closing the connection.my question is why people are not opting for single connection approach and whate are its disadvantages in doing and using it.and as for the "memory and security" issues these pints are always mentioned as the advantage Connection Pool has and i am not making out of my own i was only discusiing why..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Suppose your application has a method which includes an JDBC operation. This method gets the connection from your singleton and starts this opreration. This method doesn't complete till the JDBC operation has finished. Suppose that operation is a select from a table with half a million rows (spurious example I know, but done to illustrate a point, I hope you understand). No other method in your application can use the connection till this method has finished. Now I don't suppose that is a problem if your application is a single threaded system. I also think you'd be exposing your application to network issues. Suppose the connection fails. If you established it through a Singleton, the only way to reestablish the connection is to restart the application.

OK I'll concede Connection Pooling may reduce memory use, since Connection Pooling will probably create fewer Connection objects than a multithreaded application which explicitly creates a Connection every time it needs to use one (as a result of Connection reuse). I still dont see the link between Connection Pooling and security though. In fact Connection reuse will possibly expose your application to security problems, since a shared resource will not be tied to a specific authenticated process. (i.e. your using something like JAAS or J2 Security. One process which has permission to open a connection does, then another which doesn't comes along and reuses the already open connection).

I'll also concede that Connection Pooling is overkill for very small applications. If your application is simple then I'd just let let it manage Connections itself. Still, a single Connection object for the whole application is going to cause problems.
Joy Chowdhury
Greenhorn

Joined: Oct 28, 2004
Posts: 4
Thanks Paul..I got it cleared but still i think there are many issues which can be debated over this topic
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
yeah it is better to use singleton, if small app and only one user.
cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ConnectionPool or Singleton
 
Similar Threads
jdbc architechture
implementing connection pooling
Utility Classes in EJB
Singleton Connection Class
connction pooling