Java Reference
In-Depth Information
 
An XADataSource, which produces XAConnections supporting distributed transactions
 
A DataSource object that is implemented to work with it
DataSources implemented to produce connections for distributed transactions are
almost always implemented to produce connections that are pooled as well. The
XAConnection interface, in fact, extends the PooledConnection interface.
The XADataSource object needs to be deployed first. This is done by creating an
instance of XATransactionalDS and setting its properties, as shown here:
XATransactionalDS xads = new XATransactionalDS();
xads.setServerName("Jupiter");
xads.setDatabaseName("CUSTOMERS");
xads.setPortNumber(9001);
xads.setDescription("Customer database");
Next, the XATransctionDS needs to be registered with the JNDI naming service, as
shown here:
Context ctx = new InitialContext();
ctx.bind("jdbc/xa/CustomerDB", xads);
The logical name associated with xads has the subcontext xa added
under the subcontext jdbc, in the same way as the subcontext pool is
added in the connection-pooling example.
Note
Finally, the DataSource object is implemented to interact with xads, and other
XADataSource objects are deployed:
TransactionalDS ds = new TransactionalDS();
ds.setDescription("Customers distributed transaction connections
source");
ds.setDataSourceName("jdbc/xa/CustomerDB ");
Context ctx = new InitialContext();
ctx.bind("jdbc/CustomerDB", ds);
Now that instances of the TransactionalDS and XATransactionalDS classes have
been deployed, an application can use the DataSource to get a connection to the
CUSTOMERS database. This connection can then be used in distributed
transactions. The following code to get the connection is very similar to the code to
get a pooled connection:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/CustomerDB");
Connection con = ds.getConnection("myUserName", "myPassword");
Search WWH ::




Custom Search