DataSource Objects and Connection Pools
To store, organize, and retrieve data, most applications use a relational database. J2EE components access relational databases through the JDBC API. For information on this API, see:
In the JDBC API, databases are accessed via
DataSourceobjects. ADataSourcehas 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
DataSourceobject can be thought of as a factory for connections to the particular data source that theDataSourceinstance represents. In a basicDataSourceimplementation, a call to thegetConnectionmethod returns a connection object that is a physical connection to the data source.If a
DataSourceobject is registered with a JNDI naming service, an application can use the JNDI API to access thatDataSourceobject, which can then be used to connect to the data source it represents.
DataSourceobjects that implement connection pooling also produce a connection to the particular data source that theDataSourceclass represents. The connection object that thegetConnectionmethod returns is a handle to aPooledConnectionobject 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 timegetConnectionis 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.The Application Server is distributed with a connection pool named
DerbyPool, which handles connections to the Derby database server. In this book, all the code examples that access a database useDataSourceobjects that are mapped toDerbyPool.