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:
In the JDBC API, databases are accessed via
DataSource
objects. ADataSource
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 theDataSource
instance represents. In a basicDataSource
implementation, a call to thegetConnection
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 thatDataSource
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 theDataSource
class represents. The connection object that thegetConnection
method returns is a handle to aPooledConnection
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 timegetConnection
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, it can help applications run significantly faster.A JDBC connection pool is a group of reusable connections for a particular database. Because creating each new physical connection is time consuming, the server maintains a pool of available connections to increase performance. When an application requests a connection, it obtains one from the pool. When an application closes a connection, the connection is returned to the pool.
Applications that use the Persistence API specify the
DataSource
object they are using in thejta-data-source
element of thepersistence.xml
file.This is typically the only reference to a JDBC object for a persistence unit. The application code does not refer to any JDBC objects. For more details, see Persistence Units (page 785).