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).
javax.sql
包提供以下内容:
DataSource
interface as an alternative to the DriverManager
for establishing a connection with a data source 应用程序直接使用 DataSource
和 RowSet
API,但连接池和分布式事务API由中间层基础架构在内部使用。
DataSource
Object to Make a Connectionjavax.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
object that is implemented to work with the middle-tier infrastructure. Connections made through the DriverManager
do not have connection and statement pooling or distributed transaction capabilities. 驱动程序供应商提供了DataSource
实现。 一个特别DataSource
对象表示特定的物理数据源,并且每个连接DataSource
对象创建是该物理数据源的连接。
数据源的逻辑名称使用Java命名和目录接口TM (JNDI)API的命名服务进行注册,通常由系统管理员或执行系统管理员职责的人员进行注册。 应用程序可以通过查找已注册的逻辑名称来检索它想要的DataSource
对象。 然后,应用程序可以使用DataSource
对象创建到它所表示的物理数据源的连接。
一个DataSource
对象可以实现与中间层基础架构一起工作,以便它产生的连接将被汇集起来以供重用。 使用这种DataSource
实现的应用程序将自动获得参与连接池的连接。 一个DataSource
对象也可以实现与中间层基础架构一起工作,以便它产生的连接可以用于分布式事务,而不需要任何特殊的编码。
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
对象相同的方式使用它。
用于连接池的类和接口是:
ConnectionPoolDataSource
PooledConnection
ConnectionEvent
ConnectionEventListener
StatementEvent
StatementEventListener
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
对象的通知。
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.
用于分布式事务的类和接口是:
XADataSource
XAConnection
XAConnection
接口来自PooledConnection
接口,因此适用于池式连接的内容也适用于作为分布式事务一部分的连接。 中间层的事务管理器透明地处理所有事情。 应用程序代码的唯一变化是应用程序不能做任何干扰事务管理器处理事务的事情。 具体而言,应用程序无法调用方法Connection.commit
或Connection.rollback
,并且无法将连接设置为自动提交模式(即无法调用Connection.setAutoCommit(true)
)。
应用程序不需要做任何特殊的事情来参与分布式事务。 它只是通过DataSource.getConnection
方法创建与想要使用的数据源的连接,就像通常那样。 事务管理器在幕后管理事务。 XADataSource
接口创建XAConnection
对象,并且每个XAConnection
对象都会创建一个事务管理器用来管理连接的XAResource
对象。
RowSet
interface works with various other classes and interfaces behind the scenes. These can be grouped into three categories.
RowSetListener
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
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. RowSetMetaData
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
方法execute
, RowSet
对象将包含一组新的行,并且其RowSetMetaData
对象将在内部更新以包含有关新列的信息。
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
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
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
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:
RowSet
接口可以用许多方式实现,任何人都可以编写一个实现。 鼓励开发人员使用他们的想象力提出使用行集的新方法。
重要说明:使用标记为“1.6以后”的API的代码必须使用实现JDBC 4.0 API的JDBC技术驱动程序运行。 您必须检查您的驱动程序文档以确保它实现了您要使用的特定功能。
javax.sql
package:
CommonDataSource | 接口,定义它们之间的共同的方法 DataSource , XADataSource 和 ConnectionPoolDataSource 。 |
ConnectionEventListener | 一个对象,用于注册以通知由 |
ConnectionPoolDataSource | PooledConnection 对象的工厂。 |
DataSource | 连接 |
PooledConnection | 为连接池管理提供挂钩的对象。 |
RowSet | 该接口为JavaBeans TM组件模型的JDBC API添加了支持。 |
RowSetInternal | RowSet 对象实现的接口,以将其自身呈现给 RowSetReader 或 RowSetWriter 对象。 |
RowSetListener | 一个接口,必须由一个组件实现,该组件需要在 RowSet 对象的生命期间发生重大事件时收到通知。 |
RowSetMetaData | 包含 RowSet 对象中列的 RowSet 对象。 |
RowSetReader | 断开连接的对象所调用的工具将 RowSet 填充数据行。 |
RowSetWriter | 一个实现 RowSetWriter 接口的对象,称为 writer 。 |
StatementEventListener | 一个对象,用于通知在Statement池中的PreparedStatements上发生的事件。 |
ConnectionEvent | 提供有关连接相关事件源的信息的 |
RowSetEvent | 一个 Event 当事件发生时,以所生成的对象 RowSet 对象。 |
StatementEvent | A StatementEvent 被发送给所有在 StatementEventListener 注册的 PooledConnection 。 |