• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the concept of wrapper for JDBC

 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find people using the word wrapper for the JDBC access.

Is this a framework and what it conveys?

Thanks in advance
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puthriah,
A wrapper is just a Java class that calls the JDBC code for you. The idea is that callers shouldn't know that JDBC is being used. That way you can change to some other database technology later without affecting clients.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of an app which obtains a JDBC Connection and closes it after use. If connection pooling is being used, the app actually invokes methods of a class which wraps the real Connection. This is necessary because the Connection must be returned to the pool upon invocation of the close() method.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne and Roger.

I have a doubt. Appreciate if you can clarify....

This is necessary because the Connection must be returned to the pool upon invocation of the close() method


After the connection is used when we use conn.close(), we are killing the instance. It means one of the instances of the pool is killed.

Are we not reducing the connection sin pool? How can it return to pool?

Thanks in advance
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the close() method is not the default implementation. This is a trick used in wrapping classes - you provide the same API but different implementation. The default behaviour of close() is to close the Connection, but close() in the wrapper returns it to the pool instead.
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David

Thanks for the reply.

I am confused. Can you explain in detail what exaclty your anser is?


The default behaviour of close() is to close the Connection, but close() in the wrapper returns it to the pool instead


How this happens? I mean the logical answer.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this sample code. We're pretending to implement a Connection Pool by numbering the Connection (wrapper)s. Each Connection (wrapper) in the Pool knows it's own number and knows about the ConnectionPool class. Therefor a Connection wrapper looks like this:



So what we have a is a class that looks and feels exactly like a Connection directly from the database, but when you call close() it gets passed back to the ConnectionPool instead.

This is a very light example. Don't try to turn this into your own broker framework, there is so much more, but it illistrates the point we were talking about.

Buy Head First Design Patterns if you haven't already

Dave
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic