background image

DataSource Objects and Connection Pools

<< Resource Connections | Resource Injection >>
<< Resource Connections | Resource Injection >>

DataSource Objects and Connection Pools

You inject resources by using the @Resource annotation in an application. For information on
resource injection, see the following sections of this Tutorial:
"Declaring Resource References" on page 94
"Updating Data in the Database" on page 708
, for information on injecting a
UserTransaction
resource
"JMS Connection Factories" on page 902
,
"JMS Destinations" on page 902
, and
"Using
@Resource
Annotations in Java EE Components" on page 954
, for information on injecting
JMS resources
You can use a deployment descriptor to override the resource mapping that you specify in an
annotation. Using a deployment descriptor allows you to change an application by repackaging
it, rather than by both recompiling the source files and repackaging. However, for most
applications, a deployment descriptor is not necessary.
DataSource
Objects and Connection Pools
To store, organize, and retrieve data, most applications use a relational database. Java EE 5
components may access relational databases through the JDBC API. For information on this
API, see
http://java.sun.com/products/jdbc
.
In the JDBC API, databases are accessed by using DataSource objects. A DataSource has a set of
properties that identify and describe the real world data source that it represents. These
properties include information such as the location of the database server, the name of the
database, the network protocol to use to communicate with the server, and so on. In the
Application Server, a data source is called a JDBC resource.
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. In a basic DataSource implementation, a call to the getConnection method returns
a connection object that is a physical connection to the data source.
If a DataSource object is registered with a JNDI naming service, an application can use the
JNDI API to access that DataSource object, which can then be used to connect to the data
source it represents.
DataSource
objects that implement connection pooling also produce a connection to the
particular data source that the DataSource class represents. The connection object that the
getConnection
method returns is a handle to a PooledConnection object rather than being a
physical connection. An application uses the connection object in the same way that it uses a
connection. Connection pooling has no effect on application code except that a pooled
connection, like all connections, should always be explicitly closed. When an application closes
a connection that is pooled, the connection is returned to a pool of reusable connections. The
next time getConnection is called, a handle to one of these pooled connections will be returned
if one is available. Because connection pooling avoids creating a new physical connection every
time one is requested, applications can run significantly faster.
DataSource
Objects and Connection Pools
The Java EE 5 Tutorial · September 2007
1010