JDBC2.0 expansion API (2)

zhaozj2021-02-17  50

Let's introduce the standard extension API of JDBC2.0. Standard expansion API is divided into the following aspects:

1, DataSource Interface: Data source interface works with the Java Name Directory Service (JNDI). It provides a loophochi ā? Br> 2, Connection Pool: You can reuse the connection, instead of using a new connection for each request.

3, Distrubute Transaction: A number of database servers involved in a transaction.

4, Rowsets: JavaBean component contains result sets, mainly to pass data to thin customers, or provide a result set that can scroll.

Let's introduce one by one.

First, the DataSource interface is a better way to connect data sources:

JDBC1.0 is originally used to use a DRIVERMANAGER class to generate a connection to the data source. JDBC2.0 uses an alternative method to use DataSource's implementation, the code change is smaller and more easily controlled.

A DataSource object represents a real data source. According to the implementation of DataSource, the data source can be both a dependency database, also spread, and a file that is also a form of form. When a DataSource object is registered into the name service, the application can get the DataSource object through the name service, and use it to generate a connection between the data source represented by DataSource.

About the information of the data source and how to locate the data source, such as the name of the database server, which machine, port number, etc. are included in the properties of the DataSource object. In this way, it is more convenient for the design of the application because it does not need hard to write the name of the driver to the program. Usually the drive name contains the name of the driver provider, and it is usually done in the DriverManager class. If the data source is transplanted to another database driver, the code is also easy to modify. The modifications you need to do are simply changing the relevant properties of DataSource. And the code using the DataSource object does not need to do any changes.

The DataSource object is configured by a system administrator or a person with appropriate permissions. Configure DataSource, including setting DataSource's properties, and then register it to the JNDI name service. During the process of registering the DataSource object, the system administrator needs to associate the DataSource object and a logical name. The name can be arbitrary, usually the name that can represent the data source and easy to remember. In the example below, the name is: InventoryDB, according to the convention, the logical name is usually in the sub context of JDBC. In this way, the full name of the logical name is: JDBC / InventoryDB.

Once the data source object is configured, the application designer can use it to generate a connection to the data source. The following code snippet exemplifies how to get a data source object with a JNDI context, and then how to generate a connection with the data source with a data source object. The beginning of the two lines is JNDI API, and the third line is the JDBC API:

CONTEXT CTX = New InitialContext (); DataSource DS = (DataSource) CTX.lookup ("JDBC / InventoryDB"); connection con = ds.getConnection ("mypassword", "myusername");

In a basic DataSource implementation, the Connection object returned by the DataSource.getConnection method and the Connection object returned by the DriverManager.getConnection method are the same. Because DataSource provides convenience, we recommend using the DataSource object to get a Connection object. We hope that the Database drivers based on JDBC2.0 technology contain a basic DataSource implementation so that it can be easily used in the application. For ordinary application designers, whether to use a DataSource object is just a choice problem. However, for applications that need to be used or distributed, you must use the DataSource object to get Connection, which will be mentioned below.

转载请注明原文地址:https://www.9cbs.com/read-29940.html

New Post(0)