Java data Object application interface Castor use reference

Source: Internet
Author: User

Exolab's Castor is the current popular, open source JDO implementation package. It is mainly used to implement O/R Mapping. Using this software package can greatly reduce the burden of programmers in processing object-relational databases. This article describes the basic usage of Castor and explains it with a large number of code examples.

Content Summary:

· Open the JDO database client application

· Java EE application

· Using the JDO database object

· Distinguishing between instantaneous JDO objects and persistent JDO objects

· Oqlquery

· Create and update and delete

· Using JDO and XML

Open the JDO database

Castor JDO supports two types of environments, client applications and Java EE servers. Client applications are used to configure database connections and explicitly manage transactions. The Java EE application uses Jndi to obtain a pre-configured database connection and processes transactions using UserTransaction or container management transactions (CMT). If you've ever used JDBC in both environments, you should be familiar with the differences between the two models.

Client application

The client application is responsible for defining the JDO database configuration, and for explicitly managing transaction processing. The database is configured and connected to a mapping file through a separate XML file. In the example code I named the database file Database.xml, but you can use any other name. See the Castor JDO database configuration for more information.

Org.exolab.castor.jdo.JDO defines the database name and attributes and is used to open the database connection. On the

You can ask the JDO layer to mount which database configuration by setting the URL of the Setconfiguration file. Note: Castor JDO will only perform a load of the database configuration only once when it establishes multiple JDO objects with the same configuration.

The Org.exolab.castor.jdo.Database object represents an open connection to the database.

The thread Jdo object definition is not thread-safe, so you should not use the JDO object in concurrent multiple threads. Also, creating multiple Jdo objects only requires a small amount of extra work, and the JDBC connection is opened only once in each transaction. This mode of processing can greatly improve performance.

The following code fragment shows a common combination in client applications: "Open the database, execute SQL, and close the database." ”

JDO jdo;
Database db;
// 定义JDO对象
jdo = new JDO();
jdo.setDatabaseName( "mydb" );
jdo.setConfiguration( "database.xml" );
jdo.setClassLoader( getClass().getClassLoader() );
// 打开一个新的数据库连接
db = jdo.getDatabase();
// 开始处理事务
db.begin();
// 以下是一些业务逻辑
. . .
// 提交事务处理,并且关闭数据库
db.commit();
db.close();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.