Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a DataSource?

I'm not certain how to get a DataSource object. I was able to use the DriverManager method to obtain a connection to a SQL database running on localhost, but every time I try to use the DataSource method to do so I wind up getting exceptions (mostly for naming).

What I was wondering is:

  1. Is it possible to get a DataSource object for local hosted databases?
  2. Does the DataSource class need to be published, or is it like DriverManager where you just get a connection with no new class creation?
  3. Could you show an example?
like image 629
Sarah Szabo Avatar asked Jul 24 '13 07:07

Sarah Szabo


People also ask

How do you create a DataSource object?

To create and deploy a DataSource object, you need to perform these tasks: Create an instance of the appropriate DataSource implementation. Set the properties of the DataSource object. Register the object with the Java Naming and Directory Interface (JNDI) naming service.

What is a DataSource URL?

What is DataSource. A DataSource is a factory for connections to the physical databases. It is an alternative to the DriverManager facility. A datasource uses a URL along with username/password credentials to establish the database connection.


1 Answers

A DataSource allows getting a JDBC connection mostly from a pool of connections. A DataSource object represents a particular DBMS or some other data source, such as a file. If a company uses more than one data source, it will deploy a separate DataSource object for each of them. The DataSource interface is implemented by a driver vendor. You externalize DB connection properties file and fetch the object using JNDI. Using a Datasource you need to know only the JNDI name. The Application server cares about the details.

It can be implemented in three different ways:

  1. A basic DataSource implementation produces standard Connection objects that are not pooled or used in a distributed transaction.
  2. A DataSource implementation that supports connection pooling produces Connection objects that participate in connection pooling, that is, connections that can be recycled.
  3. A DataSource implementation that supports distributed transactions produces Connection objects that can be used in a distributed transaction, that is, a transaction that accesses two or more DBMS servers.

Like, in Spring, you can configure the datasource in an XML file and then (1) either inject it into your bean, (2) get it from ApplicationContext.

DataSource ds = (DataSource) ApplicationContextProvider.
                            getApplicationContext().getBean("myDataSource");
Connection c = ds.getConnection();

Suggested Reading:

  1. Connecting with DataSource Objects
  2. Why do we use a DataSource instead of a DriverManager?
  3. Data access with JDBC
  4. How to retrieve DB connection using DataSource without JNDI?
  5. Best way to manage DB connections without JNDI
like image 91
AllTooSir Avatar answered Sep 30 '22 17:09

AllTooSir