| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Castor JDO - ConfigurationThe Castor configuration file Transaction demarcation Local Mode Global Mode Sample Configuration File Prepared statement pooling Sample configurations for various databases Sybase JConnect (JDBC data source) PostgreSQL (JDBC data source) Oracle (JDBC Driver) mySQL (JDBC Driver) InstantDB JDOConfFactory - A programmatic way of configuring Castor JDO References The JDO Configuration DTD The Castor configuration fileThe default way to configure how Castor interacts with a specific database system is by using a configuration file. It specifies the means to obtain a connection to the database server, the mapping between Java classes and tables in that database server, and the service provider to use for talking to that server (For a more flexible, programmatic way without configuration files see section JDOConfFactory). The application will access the database(s) by its given name (database/name) and will be able to persist all objects specified in the included mapping file(s). The engine attribute specifies the persistence engine for this database server. Different database servers vary in the SQL syntax and capabilites they support, and this attribute names the service provider to use. The following names are supported in Castor:
Note: Castor doesn't work with JDBC-ODBC bridge from Sun. In particular, MS Access is not supported. The means to acquire a database connection is specified in one of three ways: as a JDBC driver URL, as a JDBC DataSource, or as a DataSource to lookup through JNDI. When Castor is used inside a J2EE application server it is recommended to use JNDI lookup (see the jndi element), allowing the application server to manage connection pooling and distributed transactions. The class mapping is included from an external mapping file, allowing multiple mappings to be included in the same database configuration, or two databases to share the same mappings. For concurrency and integrity reasons, two database configurations should never attempt to use overlapping mappings. It is recommended to use one database configuration per database server. The mapping file is specified using a URL, typically a file: URL. If the database configuration file and mapping file reside in the same directory, use a relative URL. Relative URLs also work if the database configuration and mapping files are obtained from the application JAR and reside in the same classpath. The driver element specifies the JDBC driver for obtaining new connections to the database server. The driver is obtained from the JDBC DriverManager and must be located in the class path. The JDBC URL locates the driver and provides the access properties. Additional properties may be specified using param elements (e.g. buffer size, network protocol, etc). Use the class-name attribute to specify the driver class for automatic registration with the JDBC DriverManager. If missing, the driver must be registered in any other means, including properties file, Class.forName(), etc. For example, to configure an Oracle 8 thin driver, use:
The data-source element specifies the JDBC DataSource for obtaining new connections to the database server. DataSources are defined in the JDBC 2.0 standard extension API which is included with Castor, and implement the interface javax.sql.DataSource. The DataSource implementation class name is specified by the class-name attribute and configured through Bean-like accessor methods specified for the param element. The DTD for the param element is undefined and depends on the DataSource being used. For example, to configure a PostgreSQL 7.1 DataSource, use:
The jndi element specifies the JDBC DataSource for obtaining new connections to the database server through a JNDI lookup. The JNDI environment naming context (ENC) is used to obtain a suitable DataSource.. When running inside a J2EE application server, this is the preferred method for obtaining database connections. It enables the J2EE application server to configure the connection, maintain a connection pool, and manage distributed transactions. For example, to specify a J2EE DataSource, use:
Transaction demarcationAs opposed to release pre 0.9.6, transaction demarcation is now configured in the JDO configuration file. As such, the user has to specify which transaction demarcation to use. Transactions when used with Castor JDO can either be local or global, and you instruct Castor to use a specific mode by supplying a <transaction-demarcation> element. Local ModeWhen using Castor JDO stand-alone and you want Castor to control transaction demarcation, please use the <transaction-demarcation> element as follows:
Global ModeWhen running inside a J2EE application server, and you want to use global (XA) transactions, please make use the <transaction-demarcation> element as follows:
In this mode, the <transaction-manager> element specifies the transaction manager that is used by your J2EE container to control these transactions. The following transaction managers are supported in Castor:
In addition to specifying the transaction manager name, it is possible to pass arbitrary name/value pairs to the transaction manager instance. Note: At the moment, only the JNDI transaction manager factory supports such an attribute. In this context, the jndiEnc attribute can be used to specify what JNDI ENC to use to lookup the transaction manager as shown below:
Sample Configuration FileThe following configuration file instructs Castor JDO to execute against an Oracle RDBMS using the thin (type 4) JDBC driver, and refers to three mapping files that define mappings for product, order and customer related data.
The following configuration file uses a connection obtained from the
J2EE application server and a single mapping file:
Prepared statement poolingCastor JDO uses JDBC prepared statements to execute SQL statements against the specified RDBMS of your choice. Per definition, Castor JDO does not provide any prepared statement pooling. As such, Castor relies on prepared statement pooling being provided by different means. One such way is to use Jakarta's Commons DBCP as database connection pool, and to turn prepared statement pooling on by configuring DBCP accordingly. Please check with Using Pooled Database Connections for general information about hot to use DBCP with Castor. Sample configurations for various databasesBesides the examples listed above, more configuraton examples can be found in the configuration files for the Castor JDO tests, which can be found in src/tests/jdo once you have downloaded and expanded the Castor source package. For each database (vendor) supported, you are going to find a database-specific JDO configuration file in this directory, e.g. src/tests/jdo/mysql.xml for mySQL or src/tests/jdo/oracle.xml for Oracle. Sybase JConnect (JDBC data source)
PostgreSQL (JDBC data source)
Oracle (JDBC Driver)
mySQL (JDBC Driver)
InstantDB
JDOConfFactory - A programmatic way of configuring Castor JDOMany applications need to connect to a database using varying user accounts or database instances. To accomplish this, the utility class JDOConfFactory and a JDOManager.loadConfiguration(org.exolab.castor.jdo.conf.JdoConf) method has been added to Castor. The following code snippet shows an example how to create a JDO configuration without the use of a default XML-based database configuration file:
As an alternative to using a org.exolab.castor.jdo.conf.Driver, you can also configure Castor to use a JDBC 2.0 DataSource:
ReferencesThe JDO Configuration DTDFor validation, the configuration file should include the following
document type definition. For DTD validation use:
The Castor JDO database configuration DTD is:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||