Distributed computing environment usually uses naming and directory services to obtain the components and resources enjoyed by * * *. Naming and Directory Services associate names with locations, services, information, and resources.
Naming services provide name-object mapping. Directory services provide information about objects and search tools needed to locate them. There are many naming and directory service implementations with different interfaces.
Java Naming and Directory Interface or JNDI provides a common interface for accessing different naming and directory services. See URL java.sun.com/products/jndi/serviceproviders.html for a list of vendors that support access to naming and directory services through JNDI interfaces.
JNDI(Java Naming and Directory Interface)
JNDI is very important when developing enterprise beans, because the access to EJB is done through JNDI's naming service. Use a naming service to find objects related to a specific name. In the EJB context, the naming service finds an enterprise bean and names it. Therefore, it is very important to understand JNDI when developing EJB applications. In addition, JDBC can use JNDI to access relational databases.
Attachment: JNDI tutorial
/products/JNDI/tutorial/index . html
(Download)
/products/JNDI/docs. html # tutorial
JDBC2.0 extension API( 1)
[Author: unknown date of addition: 2001-8-2414:11:50]
Source: www.csdn.net.
JDBC 2.0 API is divided into two parts: JDBC 2.0 core API and JDBC 2.0 standard extension API. The core API is in java.sql, which is the basic function realized in the original version. The standard extension API is in javax.sql, and some new interfaces specified by JDBC2.0 specification are in it. Of course, JDBC2.0 has also made some changes to the original version of the java.sql core. But not very big. The original JDBC 1.0 program can run on JDBC2.0 without modification. This is always a good style of Java. The latest JDBC software package can be downloaded from sun's website.
The extended API of JDBC2.0 adds some important functions of data access and data source access. Some of them are mainly used in enterprise computing. With the new expansion package of JDBC2.0, JDBC provides a general data access method from JAVA2 platform.
First, let's look at how the API extended by JDBC standard is combined with JDBC2.0, which includes two packages:
1, java.sql package, which is the core API of JDBC2.0. It includes the original JDBC API(JDBC 1.0 version) and some new versions of 2.0 APIs. This package is provided in the Java 2 platform SDK.
2, javax.sql package, which is the standard extension API of JDBC2.0. This package is brand-new and is provided separately in Java 2 Platform SDK, Enterprise Edition.
The core API of JDBC2.0 includes the API of JDBC 1.0, and on this basis, it adds some functions and enhances some performance. Java language provides a unified data access method in the front end of database computing, and the efficiency has also been improved.
JDBC is backward compatible, and the program of JDBC 1.0 can run on JDBC2.0 without modification. However, if a program uses the new features of JDBC2.0, it must run on JDBC2.0.
To sum up, the new features of JDBC core API have done work in two aspects. One is to support some new functions, and the other is to support SQL3 data types.
1. In terms of supporting new functions, including that the result set can be scrolled backward and the data can be updated in batches. In addition, it also provides character stream operation of UNICODE character set.
2. In supporting SQL3 data types: including new SQL3 data types and increasing the storage of persistent objects.
In order to access data and operations more conveniently, the new feature of JDBC is that the design of application programs is easier. For example, operations on data blocks can significantly improve the performance of database access. The newly added BLOB, CLOB and array interfaces can be large data types that the application can operate, without any other processing by the client before storage. In this way, the use efficiency of memory is significantly improved.
Let's introduce the standard extension API of JDBC2.0, which is divided into the following aspects:
1, DataSource interface: a data source interface that works with Java directory service (JNDI). It provides a logarithmic blowback finger? Meimei? Wife Chili ā? br & gt; 2. Connection pooling: You can reuse connections instead of using a new connection for each request.
3. Distributed transaction: A transaction involves multiple database servers.
4.Rowsets:JavaBean component contains result sets, which are mainly used to transmit data to thin customers or provide scrollable result sets.
Let's introduce one by one:
First, the DataSource interface is a better way to connect data sources:
JDBC 1.0 originally used the DriverManager class to generate a connection to a data source. JDBC2.0 uses an alternative method. With the implementation of DataSource, the code becomes smaller, more refined and easier to control.
The DataSource object represents a real data source. Depending on the implementation of the data source, the data source can be a relational database, a spreadsheet or a table file. When the DataSource object is registered in the name service, the application can obtain the DataSource object through the name service and use it to create a connection with the DataSource represented by the datasource.
Information about the data source and how to locate it, such as the name of the database server, which machine it is located on, port number, etc. Are contained in the property of the datasource object. In this way, the design of the application program is more convenient, because there is no need to write the name of the driver into the program. Usually, the driver name includes the name of the driver provider, which is usually done in the DriverManager class. If you want to port the data source to another database driver, you can easily modify the code. All that needs to be modified is to change the related properties of DataSource. The code that uses the DataSource object does not need to be changed.
The DataSource object is configured by a system administrator or someone with appropriate permissions. Configure the data source, including setting the properties of the data source, and then register in the JNDI name service. In the process of registering the DataSource object, the system administrator needs to associate the DataSource object with a logical name. The name can be arbitrary, usually a name that can represent the data source and is easy to remember. In the following example, the name is InventoryDB, and by convention, the logical name is usually in the sub-context of jdbc. The full name of such a logical name is jdbc/ InventoryDB.
Once the data source object is configured, the application designer can use it to create a connection with the data source. The following code fragment demonstrates how to obtain a data source object with JNDI context, and then how to use the data source object to establish a connection with the data source. The first two lines use JNDI API, and the third line uses JDBC API:
context CTX = new initial context(); data source ds =(data source)CTX . lookup(" JDBC/inventory db "); connection con = ds . getconnection(" my password "," my username ");
In the basic DataSource implementation, the connection object returned by the DataSource.getConnection method is the same as that returned by the DriverManager.getConnection method. Because of the convenience provided by DataSource, we suggest using DataSource object to obtain a connection object. We hope that all database drivers based on JDBC2.0 technology include a basic implementation of DataSource, so that it can be easily used in applications.
For ordinary application designers, whether to use the DataSource object is just a matter of choice. However, for application designers who need to use connection pooling or distributed transactions, they must use the DataSource object to obtain the connection, for the reasons we will mention below.
Second, the Connection pooling (connection pooling):
Connection pooling is a mechanism. When an application closes a connection, the connection will be recycled instead of destroyed, because establishing a connection is a resource-intensive operation. If the recycled connections can be reused, the number of newly created connections will be reduced and the running performance will be significantly improved.
Suppose an application needs to establish a connection to a data source named EmpolyeeDB. The code for obtaining a connection using connection pooling is as follows:
context CTX = new initial context(); data source ds =(data source)CTX . lookup(" JDBC/EmployeeDB "); connection con = ds . getconnection(" my password "," my username "); Except for the logical name, we found that the code is the same as the example given above. Different logical names can be connected to different databases. Whether the connection returned by the getConnection method of the DataSource object is a connection in the connection pool depends entirely on the implementation method of the DataSource object. If the DataSource object implementation works with a middle-tier server that supports connection pooling, the DataSource object will automatically return the connection in the connection pooling, and the connection can also be reused.
Whether to use connection pooling to get connections is no different from application code. Using this Connection connection is no different, the only difference is to close a connection in the finally statement block of java. Closing connections in finally is a good programming habit. In this way, even if the method throws an exception, the connection will be closed and recycled into the connection pool. The code should look like this:
Try {…
}catch(){…
} Finally {if(con! = null)con . close(); }
Third, distributed transactions:
Getting a connection that supports distributed transactions is very similar to getting a connection in a connection pool. Similarly, the difference lies in the implementation of DataSource, not in the way of obtaining the connection in the application. Assuming that the implementation of DataSource can work with the middle-tier server supporting distributed transactions, the connection code is as follows:
context CTX = new initial context(); data source ds =(data source)CTX . lookup(" JDBC/EmployeeDB "); connection con = ds . getconnection(" my password "," my username "); For performance reasons, if a data source can support distributed transactions, it can also support connection pool management.
From the point of view of the application designer. It makes no difference whether the connection of distributed transactions is supported or not. The only difference is that on the boundary of a transaction (where the transaction starts and ends), the start or end of the transaction is controlled by the transaction server. The application should not do anything that might interfere with the service. The application cannot directly call the transaction commit or rollback operation, nor can it use the automatic commit mode of the transaction (automatically call the commit or rollback when the database operation is completed).
When a connection participates in a distributed transaction, you can't do the following code (con stands for a connection that supports distributed transactions).
con . commit(); Or con.rollback (); Or con.setAutoCommit (true); For normal connections, the default is auto-commit mode. For connections that support distributed transactions, the default value is not autocommit mode. Note that even if Connection supports transactions, it can be used without transactions. Restrictions on transaction boundaries are established only in the case of distributed transactions.
When configuring DataSource supporting connection pool, it involves configuring ConnectionPoolDataSource object, which is the middle layer of managing connection pool in three-tier architecture. Similarly, when configured to support distributed transactions, you need to configure XADataSource, which is the object used by the middle tier to manage distributed transactions. ConnectionPoolDataSource and XADataSource are provided by driver providers and are transparent to application designers. Like the basic data source, the system administrator configures ConnectionPoolDataSource and XADataSource objects.
Fourth, the result set:
Result set objects are containers for data rows. According to its purpose, it can be achieved in many ways. Rowsets and their related interfaces are slightly different from the standard extension API of JDBC2.0. They are not part of the driver. The rowset is implemented at the top of the driver and can be implemented by anyone else.
Any type of rowset implements the rowset interface, which extends the ResultSet interface. In this way, the rowset object has all the functions of the ResultSet object. You can get a column of values in the database through the getXXX method, modify a column of values through the updateXXX method, and move the cursor to make the current row become another row.
Of course, we are more interested in the new functions provided by the rowset interface. As a JavaBean component, the RowSet object can add or delete listeners and get or set their property values. One of the attributes is a string, which represents a query request to the database. The rowset interface defines the method of setting parameters and also provides the method of executing this request. This means that the rowset object can execute the query request and calculate according to the result set it produces. Similarly, a rowset can also be calculated from any tabular data source, so it is not limited to relational databases.
After obtaining data from the data source, the rowset object can be disconnected from the data source and the rowset can be serialized. In this way, rowsets can be delivered to thin clients through the network.
Rowsets can be reconnected to the data source so that changes can be saved back to the data source. If a listener is generated, the listener will be notified when the current row of the rowset is moved or the data is modified. For example, a graphical user interface module can be registered as a listener. When a rowset changes, the graphical user interface is notified and can be modified to conform to the rowset it represents.
According to different needs, the rowset interface can be implemented in many ways. Java software has written a CachedRowSet implementation, which can be obtained from/developer/earlyaccess/CRS/index.html.
Unlike the CachedRowSet class, the JDBCRowSet class always maintains a connection to the data source. In this way, it is as simple as adding a layer around ResultSet, but the driver based on JDBC technology looks like a simple JavaBean component.
Summary: JDBC2.0 standard extension API is registered with JNDI name service through DataSource, which extends JDBC technology into a brand-new concept. Make the application code more refined and easy to control. The new API supports connection pooling and distributed transactions. Finally, it enables java applications to spread result sets on the network, thus integrating non-scrollable results into scrollable rowsets.