Java Reference
In-Depth Information
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/customerDB");
Connection con = ds.getConnection("myUserName", "myPassword");
The BasicDataSource object described repesents a vendor's
implementation of the basic DataSource, which may have a vendor
specific name. The Opta2000 driver, for example, calls it a
TdsDataSource. The Connection object that the basic implementation of
the DataSource.getConnection method returns is identical to a
Connection object that the DriverManager.getConnection method returns.
Note
Using a DataSource object is optional unless you are writing applications that include
connection pooling or distributed transactions. In such cases, as discussed in the
next few paragraphs, the use of a DataSource object with built-in connection pooling
or distributed-transaction capabilities offers obvious advantages.
Connection Pooling
Creating and destroying resources frequently involves significant overhead and
reduces the efficiency of an application. Resource pooling is a common way of
minimizing the overhead of creating a new resource for an operation and discarding it
as soon as the operation is terminated. When resource pooling is used, a resource
that is no longer needed after a task is completed is not destroyed but is added to a
resource pool instead, making it available when required for a subsequent operation.
Because establishing a connection is expensive, reusing connections in this way can
improve performance dramatically by cutting down on the number of new connections
that need to be created.
The JDBC 2.0 API introduces the ConnectionPoolDataSource interface. This object is
a factory for PooledConnection objects. Connection objects that implement this
interface are typically registered with a JNDI service.
To deploy a DataSource object to produce pooled connections, you must first deploy
a ConnectionPoolDataSource object, setting its properties appropriately for the data
source to which it produces connections:
ConnectionPoolDataSource cpds = new ConnectionPoolDataSource();
cpds.setServerName("Jupiter");
cpds.setDatabaseName("CUSTOMERS ");
cpds.setPortNumber(9001);
cpds.setDescription("Customer database");
The ConnectionPoolDataSource object is then registered with the JNDI naming
service:
Search WWH ::




Custom Search