Java Reference
In-Depth Information
Context ctx = new InitialContext();
ctx.bind("jdbc/pool/customerDB ", cpds);
The logical name associated with cpds has the subcontext pool added
under the subcontext jdbc, which is similar to adding a subdirectory to
another subdirectory in a hierarchical file system.
After the ConnectionPoolDataSource object has been registered with a JNDI naming
service, deploy a DataSource object implemented to work with it.
Only two properties need to be set for the DataSource object, since the information
required for connection has already been set in the ConnectionPoolDataSource
object. These are as follows:
The dataSourceName is then set to the logical name of the
ConnectionPoolDataSource, as shown here:
PooledDataSource ds = new PooledDataSource();
ds.setDescription("Customer database pooled connection source");
ds.setDataSourceName("jdbc/pool/customerDB ");
Context ctx = new InitialContext();
ctx.bind("jdbc/customerDB", ds);
You have now deployed a DataSource object that an application can use to get
pooled connections to the database.
It is especially important to close pooled connections in a finally block,
so that even if a method throws an exception, the connection will be
closed and put back into the connection pool.
Another situation in which using a DataSource object is required is when you need to
implement distributed transactions. In such cases, as discussed in the next few
paragraphs, the use of a DataSource object with built-in distributed-transaction
capabilities is the best solution.
Distributed Transactions
In a three-tier architecture, it is sometimes necessary to access data from more than
one database server in a distributed transaction. This situation can be ha ndled very
effectively using a DataSource implemented to produce connections for distributed
transactions in the middle tier.
As with connection pooling, two classes must be deployed:
Search WWH ::

Custom Search