aspose file tools
The moose likes JDBC and the fly likes Opinions - Singleton or Connection Pool? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Opinions - Singleton or Connection Pool?" Watch "Opinions - Singleton or Connection Pool?" New topic
Author

Opinions - Singleton or Connection Pool?

Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Hi all,
So, I'm calling for opinions. Suppose I have the following two options to connect to a database:
  • create a Singleton class which creates and maintains a Connection object as an instance member; when an outside class requires connectivity, the class accesses the Singleton's Connection object; at the end of the application's lifecycle, the Singleton class releases (closes) the Connection
  • implement a Connection pool (self-explanatory, I hope)

  • Now, most of the time we'd want some type of Connection pool, but is there a case where it may be too much overhead? Suppose the number of users is minimal? Would it be better to implement some type of Singleton pattern, where the same Connection object is re-used, over and over?

    Thanks for your thoughts.
    Julian Kennedy
    Ranch Hand

    Joined: Aug 02, 2004
    Posts: 823
    The only situations that I can conceive of where having a single connection makes more sense is where you have only a single user or your database is in single user mode, i.e. can only support one connection at a time. There's really no overhead in using a connection pool. The implementation is typically all done for you in your JDBC driver or app server. You just have to call the methods. In a multi-user application, without connection pooling, you have no database concurrency.

    Jules
    Jeanne Boyarsky
    internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 26201
        
      66

    I would only use a singleton if you are running the application on a client's machine, like from an applet. If you have an app server available, there is no reason too rewrite connection pool code.

    I wouldn't use a singleton in the case Jules brought up about a single user database. If you decided to upgrade the database, you would have to update the code too. I like to keep my code as independent of the database as possible.


    [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
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: Opinions - Singleton or Connection Pool?
     
    Similar Threads
    JDBC 3.0 api Connection pooling
    Advantage of using Singleton over a class with static methods
    JDBC Connection pooling in postgresql
    Singleton Connection Class
    Singleton pattern