How a DriverManager class is implemented

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sankar2011
    New Member
    • Nov 2011
    • 18

    How a DriverManager class is implemented

    This is for the concept.
    So import statements/package statements have been avoided. Also only the necessary methods are given.
    Search **** for the comments

    Courtesy Effective Java by Joshua Bloch



    Code:
    /* ******Service interface  */
    public interface Connection {
    ... //***** Service-specific methods go here
    }
    
    /* ******  Following class implements a connection
    of a particular DriverImpl */
    public class ConnectionImpl implements Connection{
    }
    
    /* ******Service provider interface */
    public interface Driver {
    	Connection getConnection() throws SqlException; 
            public boolean acceptsURL(String url);
    
    }
    
    /* ****** Following class is a Driver implementation provided by a particular DataBase vendor. The static
    block is executed whenever class.forName(...) is called.*/
    public class DriverImpl implements Driver {
    	static {
    
        		try {
    
            		DriverManager.registerDriver(new Driver());
    
        		} catch (SQLException E) {
    
            		throw new RuntimeException("Can't register driver!");
    
        		}
    
    	}
    
    private Connection getConnection()throws SQLException{
    ......
    }
    
    public boolean acceptsURL(String url){
    ......
    }
    
    }
    
    
    
    
    
    /* ******Noninstantiable class for service registration and access */
    public class DriverManager {
    private DriverManager() { } // Prevents instantiation 
    // Stores service instances of Drivers
    private static final Set<Driver> drivers = new ConcurrentHashSet<Driver>();
    
    
    
    
    /****  This method is called in the static block defined 
    in the Driver class  */
    public static void registerDriver(Driver p){
    	drivers.put(p);
    }
    
    
    
    
    
    /*** The getConnection method which is called by our client code. This method calls getDriver() method */
    public Connection getConnection(String url,....) throws SqlException{
    	Driver d = getDriver(url);
    	return d.getConnection();	
    
    }
    
    private Driver getDriver(String url) throws SQLException{
    
    	Iterator<Driver> itr = drivers.iterator();
    	Driver d = null;	
    	while(itr.hasNext()){
    		d = itr.next();
    		if ( d.acceptsURL(url) ) 
    		    return d;
    	}
    	
      }
      throw new SQLException("No driver found for " + url);
    }
    
    }
    Last edited by Rabbit; Sep 9 '13, 08:55 PM. Reason: Please use code tags when posting code or formatted data.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Is this truely an Insight Article?

    I have the following issues with this article:

    1)
    This is for the concept.
    What does this mean?
    An insight article is, IMHO, not the place to be working on proof of concept.

    2)
    So import statements/package statements have been avoided.
    These "important statements" may very well be what a person seeking help on how to implement a DriverManager Class needs to understand. Insight articles are supposed to be where the details of a concept are explained, or at least, like an expanded help file.

    3)
    Also only the necessary methods are given.
    …and without some explanation of what these are, they are not really helpful to the novice programmer.

    4)
    Search **** for the comments
    …and the comments made in the code are not much to work with.

    5)
    Courtesy Effective Java by Joshua Bloch
    http://www.janeve.me/articles/class-...-getconnection
    IMNHO: An insight article should not rely on an outside link to provide the "meat and potatos" of the information.
    Worse yet, your link isn't even available to me and many like me as it is a blog site which is blocked by my work:
    Access Denied (policy_denied)
    Oops! Sorry, access is denied to the requested URL: http://www.janeve.me/articles/class-...-getconnection due to Category: Blogs/Personal Pages (Computers/Internet;Blogs/Personal Pages)

    Username: <redacted> / IP Address: 10.<redacted>

    The page you attempted to access is unrated or categorized in a manner that requires additional access. If unrated, please <redacted> to submit site for review. For other assistance, contact the IT Support Center at +1 <redacted> or <redacted>.co m. When notifying the Support Center include the category reported for the site you are trying to access.
    SO I must "assume" that there is something there worth reading; however, not valuable enough that my IT departement thought it worth letting thru the bloging filter.

    sankar2011, you did a much better job here Weak HashMap Summarized
    Last edited by zmbd; Sep 12 '13, 08:12 PM.

    Comment

    • sankar2011
      New Member
      • Nov 2011
      • 18

      #3
      Hi ZMBD,

      Thanks for your constructive criticism.
      1> I actually concentrated on the design principles followed for implementing this.
      2> I emphasized on the the basic flow of DriverManager registration and getting connection.

      I omitted the methods to make it simpler. Because one who gets the basic concept he may very well look into the original DriverManager code. This article will help him/her to understand the full implementation.

      Regards

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        sankar2011:
        Thank you taking the time to reply.
        I read thru your other articles and found them to be fairly well done... and well... I guess expecting the same writing styles as the others so I was somewhat disapointed when I read this one.

        I omitted the methods to make it simpler. Because one who gets the basic concept he may very well look into the original DriverManager code. This article will help him/her to understand the full implementation.
        Ah, yes, and the article is not available to me (nor do I expect many others) when working from the business/enterprise computers. Companies just simply block most blogs.
        Perhaps you could paraphrase what is in the link and add that informtion to the end. IMHO, having that information available within the article would make this article so much better for the novice. Furthermore, should that blog suddenly die, or the author for somereason kills the linked to article, your work would still have a great deal of value.

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          sankar2011:
          I am also disappointed. Please try to improve your work.

          Especially comments like this:
          Code:
           /*** The getConnection method which is called by our client code. This method calls getDriver() method */
          public Connection getConnection(String url,....) throws SqlException{
              Driver d = getDriver(url);
              return d.getConnection();    
          }
          This comment is redundant and adds no additional information.
          It is like writing:
          Code:
           /*** The gnod method which is called by our client code. This method calls x() method */
          public int gnod(){
              float x = x();
              return (int) x * 7;    
          }
          But what the comment should really have been is:
          "This method returns the number of days that is left for your credit card. It uses method x() to internally evaluate the number of weeks your credit card is valid"
          And a better comment, because you don't care about the internals and you know it's a method: "Return number of days left until your credit card becomes invalid."

          Comment

          • sankar2011
            New Member
            • Nov 2011
            • 18

            #6
            Ok. :-)

            I will definitely remember this. Thank you for taking time to go through my articles. This was written keeping in mind about a novice programmer. But yet. I really take this positively and remember this. Cheers!

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              sankar2011
              I am so glad that you are taking these comments in the spirit intended!

              About the novice... I quite often refer the novice to these articles - just because these articles are usually very well written and SOOOOOOOOOOO much better than the almost less than worthless use of bandwidth that most help files have become.

              Therefore, what you and others have done by writing these articles is extremely valuable to everyone!

              THANK YOU
              For taking the time and making the effort to work on these documents.

              Comment

              Working...