| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| How to use Castor with(in) distributed J2EE transactionsOverview Intended audience Steps Make Castor participate in a J2EE transaction Make Castor participate in container-managed J2EE transaction Resource enlisting Tips References OverviewJ2EE applications depend on the J2EE container (hosting Servlet, EJB, etc) to configure a database connection (as well as other resource managers) and use JNDI to look it up. This model allows the application deployer to configure the database properties from a central place, and gives the J2EE container the ability to manage distributed transactions across multiple data sources. This HOW-TO shows how to seamlessly use Castor JDO in such a managed environment, and how to make Castor participate in a distributed transaction. Intended audienceAnyone who wants to use Castor JDO with(in) distributed J2EE transactions. StepsThe following sections highlight the steps necessary to use Castor JDO seamlessly in such a (managed) environment, and how to make Castor participate in a distributed transaction. Make Castor participate in a J2EE transactionThe following code fragment shows how to use JNDI to lookup a database and how to use a JTA UserTransaction instance to manage the J2EE (aka distributed) transaction:
Make Castor participate in container-managed J2EE transactionIf the transaction is managed by the container, a common case with EJB beans and in particular entity beans, there is no need to begin/commit the transaction explicitly. Instead the application server takes care of enlisting the database used by Castor JDO to insert domain entities into a database in the ongoing transaction and commiting/rolling back at the relevant time. The following code snippet relies on the container to manage the transaction.
As transaction enregistration is dealt with at the J2EE container, it is not necessary anymore to obtain a UserTransaction and start/commit the transaction manually. Resource enlistingInstead of constructing required resources directly, a typical J2EE
application uses the JNDI API to look up resources from centrally managed
place such as a naming and directory service. In such an environment,
Castor JDO takes on the role of a managed resource as well. It follows
that, instead of constructing a We thus recommend enlisting the JDOManager object under the java:comp/env/jdo namespace, compatible with the convention for listing JDBC resources. Tips
References
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||