Most visited

Recently visited

Added in API level 1


Provides the API for server side data source access and processing from the Java TM programming language. This package supplements the java.sql package and, as of the version 1.4 release, is included in the Java Platform, Standard Edition (Java SE TM). It remains an essential part of the Java Platform, Enterprise Edition (Java EE TM).


  1. The DataSource interface as an alternative to the DriverManager for establishing a connection with a data source
  2. Connection pooling and Statement pooling
  3. Distributed transactions
  4. Rowsets

应用程序直接使用 DataSourceRowSet API,但连接池和分布式事务API由中间层基础架构在内部使用。

Using a DataSource Object to Make a Connection

The javax.sql package provides the preferred way to make a connection with a data source. The DriverManager class, the original mechanism, is still valid, and code using it will continue to run. However, the newer DataSource mechanism is preferred because it offers many advantages over the DriverManager mechanism.

这些是使用 DataSource对象建立连接的主要优点:

驱动程序供应商提供了DataSource实现。 一个特别DataSource对象表示特定的物理数据源,并且每个连接DataSource对象创建是该物理数据源的连接。

数据源的逻辑名称使用Java命名和目录接口TM (JNDI)API的命名服务进行注册,通常由系统管理员或执行系统管理员职责的人员进行注册。 应用程序可以通过查找已注册的逻辑名称来检索它想要的DataSource对象。 然后,应用程序可以使用DataSource对象创建到它所表示的物理数据源的连接。

一个DataSource对象可以实现与中间层基础架构一起工作,以便它产生的连接将被汇集起来以供重用。 使用这种DataSource实现的应用程序将自动获得参与连接池的连接。 一个DataSource对象也可以实现与中间层基础架构一起工作,以便它产生的连接可以用于分布式事务,而不需要任何特殊的编码。

Connection Pooling and Statement Pooling

Connections made via a DataSource object that is implemented to work with a middle tier connection pool manager will participate in connection pooling. This can improve performance dramatically because creating new connections is very expensive. Connection pooling allows a connection to be used and reused, thus cutting down substantially on the number of new connections that need to be created.

连接池是完全透明的。 它是在Java EE配置的中间层自动完成的,因此从应用程序的角度来看,不需要更改代码。 应用程序只是简单地使用DataSource.getConnection方法来获取连接池,并以与使用任何Connection对象相同的方式使用它。


The connection pool manager, a facility in the middle tier of a three-tier architecture, uses these classes and interfaces behind the scenes. When a ConnectionPoolDataSource object is called on to create a PooledConnection object, the connection pool manager will register as a ConnectionEventListener object with the new PooledConnection object. When the connection is closed or there is an error, the connection pool manager (being a listener) gets a notification that includes a ConnectionEvent object.

如果连接池管理器支持Statement池,为PreparedStatements ,这可以通过调用方法来确定DatabaseMetaData.supportsStatementPooling ,连接池管理器将注册为StatementEventListener与新的对象PooledConnection对象。 PreparedStatement关闭或出现错误时,连接池管理器(作为侦听器)将获取包含StatementEvent对象的通知。

Distributed Transactions

As with pooled connections, connections made via a DataSource object that is implemented to work with the middle tier infrastructure may participate in distributed transactions. This gives an application the ability to involve data sources on multiple servers in a single transaction.


These interfaces are used by the transaction manager; an application does not use them directly.

XAConnection接口来自PooledConnection接口,因此适用于池式连接的内容也适用于作为分布式事务一部分的连接。 中间层的事务管理器透明地处理所有事情。 应用程序代码的唯一变化是应用程序不能做任何干扰事务管理器处理事务的事情。 具体而言,应用程序无法调用方法Connection.commitConnection.rollback ,并且无法将连接设置为自动提交模式(即无法调用Connection.setAutoCommit(true) )。

应用程序不需要做任何特殊的事情来参与分布式事务。 它只是通过DataSource.getConnection方法创建与想要使用的数据源的连接,就像通常那样。 事务管理器在幕后管理事务。 XADataSource接口创建XAConnection对象,并且每个XAConnection对象都会创建一个事务管理器用来管理连接的XAResource对象。


The RowSet interface works with various other classes and interfaces behind the scenes. These can be grouped into three categories.
  1. Event Notification
    • RowSetListener
      A RowSet object is a JavaBeansTM component because it has properties and participates in the JavaBeans event notification mechanism. The RowSetListener interface is implemented by a component that wants to be notified about events that occur to a particular RowSet object. Such a component registers itself as a listener with a rowset via the RowSet.addRowSetListener method.

      RowSet对象更改其中一行时,更改其所有行或移动其光标时,它还会通知每个已注册的侦听器。 聆听者通过执行其调用的通知方法来做出反应。

    • RowSetEvent
      As part of its internal notification process, a RowSet object creates an instance of RowSetEvent and passes it to the listener. The listener can use this RowSetEvent object to find out which rowset had the event.

  2. Metadata
    • RowSetMetaData
      This interface, derived from the ResultSetMetaData interface, provides information about the columns in a RowSet object. An application can use RowSetMetaData methods to find out how many columns the rowset contains and what kind of data each column can contain.

      RowSetMetaData接口提供了有关设置列信息的方法,但应用程序通常不会使用这些方法。 当应用程序调用RowSet方法executeRowSet对象将包含一组新的行,并且其RowSetMetaData对象将在内部更新以包含有关新列的信息。

  3. The Reader/Writer Facility
    A RowSet object that implements the RowSetInternal interface can call on the RowSetReader object associated with it to populate itself with data. It can also call on the RowSetWriter object associated with it to write any changes to its rows back to the data source from which it originally got the rows. A rowset that remains connected to its data source does not need to use a reader and writer because it can simply operate on the data source directly.
    • RowSetInternal
      By implementing the RowSetInternal interface, a RowSet object gets access to its internal state and is able to call on its reader and writer. A rowset keeps track of the values in its current rows and of the values that immediately preceded the current ones, referred to as the original values. A rowset also keeps track of (1) the parameters that have been set for its command and (2) the connection that was passed to it, if any. A rowset uses the RowSetInternal methods behind the scenes to get access to this information. An application does not normally invoke these methods directly.

    • RowSetReader
      A disconnected RowSet object that has implemented the RowSetInternal interface can call on its reader (the RowSetReader object associated with it) to populate it with data. When an application calls the RowSet.execute method, that method calls on the rowset's reader to do much of the work. Implementations can vary widely, but generally a reader makes a connection to the data source, reads data from the data source and populates the rowset with it, and closes the connection. A reader may also update the RowSetMetaData object for its rowset. The rowset's internal state is also updated, either by the reader or directly by the method RowSet.execute.
    • RowSetWriter
      A disconnected RowSet object that has implemented the RowSetInternal interface can call on its writer (the RowSetWriter object associated with it) to write changes back to the underlying data source. Implementations may vary widely, but generally, a writer will do the following:

      • Make a connection to the data source
      • Check to see whether there is a conflict, that is, whether a value that has been changed in the rowset has also been changed in the data source
      • Write the new values to the data source if there is no conflict
      • Close the connection

RowSet接口可以用许多方式实现,任何人都可以编写一个实现。 鼓励开发人员使用他们的想象力提出使用行集的新方法。

重要说明:使用标记为“1.6以后”的API的代码必须使用实现JDBC 4.0 API的JDBC技术驱动程序运行。 您必须检查您的驱动程序文档以确保它实现了您要使用的特定功能。

Package Specification

Related Documentation

The Java Series book published by Addison-Wesley Longman provides detailed information about the classes and interfaces in the javax.sql package:


CommonDataSource 接口,定义它们之间的共同的方法 DataSourceXADataSourceConnectionPoolDataSource


ConnectionPoolDataSource PooledConnection对象的工厂。


PooledConnection 为连接池管理提供挂钩的对象。
RowSet 该接口为JavaBeans TM组件模型的JDBC API添加了支持。
RowSetInternal RowSet对象实现的接口,以将其自身呈现给 RowSetReaderRowSetWriter对象。
RowSetListener 一个接口,必须由一个组件实现,该组件需要在 RowSet对象的生命期间发生重大事件时收到通知。
RowSetMetaData 包含 RowSet对象中列的 RowSet对象。
RowSetReader 断开连接的对象所调用的工具将 RowSet填充数据行。
RowSetWriter 一个实现 RowSetWriter接口的对象,称为 writer
StatementEventListener 一个对象,用于通知在Statement池中的PreparedStatements上发生的事件。




RowSetEvent 一个 Event当事件发生时,以所生成的对象 RowSet对象。
StatementEvent A StatementEvent被发送给所有在 StatementEventListener注册的 PooledConnection