NOTE: The material in this chapter is based on JDBCtm API Tutorial and Reference, Second Edition: Universal Data Access for the Javatm 2 Platform, published by Addison Wesley as part of the Java series, ISBN 0-201-43328-1.
A DataSource
object is the representation of a data source in the Java programming language. In basic terms, a data source is a facility for storing data. It can be as sophisticated as a complex database for a large corporation or as simple as a file with rows and columns. A data source can reside on a remote server, or it can be on a local desktop machine. Applications access a data source using a connection, and a DataSource
object can be thought of as a factory for connections to the particular data source that the DataSource
instance represents. The DataSource
interface provides two methods for establishing a connection with a data source.
Using a DataSource
object is the preferred alternative to using the DriverManager
for establishing a connection to a data source. They are similar to the extent that the DriverManager
class and DataSource
interface both have methods for creating a connection, methods for getting and setting a timeout limit for making a connection, and methods for getting and setting a stream for logging.
Their differences are more significant than their similarities, however. Unlike the DriverManager
, a DataSource
object has properties that identify and describe the data source it represents. Also, a DataSource
object works with a Javatm Naming and Directory Interfacetm (JNDI) naming service and is created, deployed, and managed separately from the applications that use it. A driver vendor will provide a class that is a basic implementation of the DataSource
interface as part of its JDBC 2.0 or 3.0 driver product. What a system administrator does to register a DataSource
object with a JNDI naming service and what an application does to get a connection to a data source using a DataSource
object registered with a JNDI naming service are described later in this chapter.
Being registered with a JNDI naming service gives a DataSource
object two major advantages over the DriverManager
. First, an application does not need to hardcode driver information, as it does with the DriverManager
. A programmer can choose a logical name for the data source and register the logical name with a JNDI naming service. The application uses the logical name, and the JNDI naming service will supply the DataSource
object associated with the logical name. The DataSource
object can then be used to create a connection to the data source it represents.
The second major advantage is that the DataSource
facility allows developers to implement a DataSource
class to take advantage of features like connection pooling and distributed transactions. Connection pooling can increase performance dramatically by reusing connections rather than creating a new physical connection each time a connection is requested. The ability to use distributed transactions enables an application to do the heavy duty database work of large enterprises.
Although an application may use either the DriverManager
or a DataSource
object to get a connection, using a DataSource
object offers significant advantages and is the recommended way to establish a connection.
There are major advantages to connecting to a data source using a DataSource
object registered with a JNDI naming service rather than using the DriverManager
facility. The first is that it makes code more portable. With the DriverManager
, the name of a JDBC driver class, which usually identifies a particular driver vendor, is included in application code. This makes the application specific to that vendor's driver product and thus non-portable.
Another advantage is that it makes code much easier to maintain. If any of the necessary information about the data source changes, only the relevant DataSource
properties need to be modified, not every application that connects to that data source. For example, if a database is moved to a different server and uses a different port number, only the DataSource
object's serverName
and portNumber
properties need to be updated. A system administrator could keep all existing code usable with the following code fragment. In practice, a system administrator would probably use a GUI tool to set the properties, so the following code fragment illustrates the code a tool might execute internally.
Context ctx = new InitialContext()ds.setPortNumber("940");DataSource ds = (DataSource)ctx.lookup("jdbc/AcmeDB"); ds.setServerName("my_new_database_server");
The application programmer would not need to do anything at all to keep all of the applications using the data source running smoothly.
Yet another advantage is that applications using a DataSource
object to get a connection will automatically benefit from connection pooling if the DataSource
class has been implemented to support connection pooling. Likewise, an application will automatically be able to use distributed transactions if the DataSource
class has been implemented to support them.