Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DriverManager and Datasource.

 
udhaya kanagaraj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,
I am having a doubt regarding Drivermanager and Datasource.
In case of JDBC connection in which situation we use Drivermanager and in which situation we will use Datasource.
Can anyone clearly tell me the difference between Drivermanager and Datasource.

Thanks in advance
Regards,
Udhaya
 
Annie Smith
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to http://www.oracle.com/technology/tech/java/sqlj_jdbc/htdocs/jdbc_faq.htm#04_01
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a misleading article. While there's nothing outright incorrect in it, it explains little, and advocates practices that are thoroughly out of fashion (like hard-coding the driver). To note:
- DataSource can use JNDI, but doesn't have to. DriverManager can't.
- Both can support connections pools and distributed transactions, but only DataSource makes that support visible to the host app (via the PooledConnection and XAConnection interfaces).
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think DataSource should always be used to obtain a Connection. One problem with DriverManager is that DriverManager.getConnection can cause a deadlock. In a server platform, all DriverManager calls are class-synchronized including many frequent calls that all drivers make, and JDBC drivers do a lot of synchronization internally. One long-waiting call can stop all JDBC work in the whole JVM and cause deadlocks.

I'm accustomed to obtaining a DataSource from a container's JNDI tree, but people working without a container should be able to instantiate the DB's DataSource class and set the required attributes (like server name, database name, port number).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the minimum environment surrounding a DataSource? Can we use them in a straight POJO application? We use them in an EJB container and the vendor framework builds up dependencies that make it impossible to unit test much of the code outside the EJB container.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stan James:

You don't need any kind of container or JNDI to use a DataSource. The drawback is that -w/o JNDI- you will have to write driver-specific code, because DataSource does not have another generic way to obtain connections besides JNDI. But for unit testing this might be a perfectly acceptable tradeoff. E.g., for the DB2 driver, check out this article to see how it's done.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I may be able to use this in some testing scenarios where I thought I had to run in the whole darned framework just to get a datasource.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic