接口 | 描述 |
---|---|
CommonDataSource |
接口,定义它们之间的共同的方法
DataSource ,
XADataSource 和
ConnectionPoolDataSource 。
|
ConnectionEventListener |
注册通知由
PooledConnection 对象生成的事件的对象。
|
ConnectionPoolDataSource |
一个工厂为
PooledConnection 对象。
|
DataSource |
一个连接到这个
DataSource 对象所代表的物理数据源的工厂。
|
PooledConnection |
提供连接池管理钩子的对象。
|
RowSet |
该接口为JavaBeans组件模型的JDBC API添加了支持。
|
RowSetInternal |
一个
RowSet 对象实现的接口,以便向
RowSetReader 或
RowSetWriter 对象呈现自身。
|
RowSetListener |
一个接口,必须由一个组件实现,该组件希望在
RowSet 对象的生命中发生重大事件时被通知。
|
RowSetMetaData |
一个对象,其中包含有关
RowSet 对象中的列的
RowSet 。
|
RowSetReader |
断开连接的
RowSet 对象调用的工具将自己填充数据行。
|
RowSetWriter |
一个实现
RowSetWriter 接口的对象,称为
写入器 。
|
StatementEventListener |
注册在“声明”池中的PreparedStatements上发生的事件的通知对象。
|
XAConnection |
为分布式事务提供支持的对象。
|
XADataSource |
一个用于
XAConnection 使用的XAConnection对象的工厂。
|
类 | 描述 |
---|---|
ConnectionEvent |
一个
Event 对象,提供有关连接相关事件源的信息。
|
RowSetEvent |
Event 对象在
Event 对象发生事件时
RowSet 对象。
|
StatementEvent |
A
StatementEvent 发送到所有
StatementEventListener 的用户注册了
PooledConnection 。
|
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).
The javax.sql
package provides for the following:
DataSource
interface as an alternative to the DriverManager
for establishing a connection with a data source Applications use the DataSource
and RowSet
APIs directly, but the connection pooling and distributed transaction APIs are used internally by the middle-tier infrastructure.
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.
These are the main advantages of using a DataSource
object to make a connection:
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. Driver vendors provide DataSource
implementations. A particular DataSource
object represents a particular physical data source, and each connection the DataSource
object creates is a connection to that physical data source.
A logical name for the data source is registered with a naming service that uses the Java Naming and Directory InterfaceTM (JNDI) API, usually by a system administrator or someone performing the duties of a system administrator. An application can retrieve the DataSource
object it wants by doing a lookup on the logical name that has been registered for it. The application can then use the DataSource
object to create a connection to the physical data source it represents.
A DataSource
object can be implemented to work with the middle tier infrastructure so that the connections it produces will be pooled for reuse. An application that uses such a DataSource
implementation will automatically get a connection that participates in connection pooling. A DataSource
object can also be implemented to work with the middle tier infrastructure so that the connections it produces can be used for distributed transactions without any special coding.
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.
Connection pooling is totally transparent. It is done automatically in the middle tier of a Java EE configuration, so from an application's viewpoint, no change in code is required. An application simply uses the DataSource.getConnection
method to get the pooled connection and uses it the same way it uses any Connection
object.
The classes and interfaces used for connection pooling are:
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.
If the connection pool manager supports Statement
pooling, for PreparedStatements
, which can be determined by invoking the method DatabaseMetaData.supportsStatementPooling
, the connection pool manager will register as a StatementEventListener
object with the new PooledConnection
object. When the PreparedStatement
is closed or there is an error, the connection pool manager (being a listener) gets a notification that includes a StatementEvent
object.
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.
The classes and interfaces used for distributed transactions are:
XADataSource
XAConnection
The XAConnection
interface is derived from the PooledConnection
interface, so what applies to a pooled connection also applies to a connection that is part of a distributed transaction. A transaction manager in the middle tier handles everything transparently. The only change in application code is that an application cannot do anything that would interfere with the transaction manager's handling of the transaction. Specifically, an application cannot call the methods Connection.commit
or Connection.rollback
, and it cannot set the connection to be in auto-commit mode (that is, it cannot call Connection.setAutoCommit(true)
).
An application does not need to do anything special to participate in a distributed transaction. It simply creates connections to the data sources it wants to use via the DataSource.getConnection
method, just as it normally does. The transaction manager manages the transaction behind the scenes. The XADataSource
interface creates XAConnection
objects, and each XAConnection
object creates an XAResource
object that the transaction manager uses to manage the connection.
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. When the RowSet
object changes one of its rows, changes all of it rows, or moves its cursor, it also notifies each listener that is registered with it. The listener reacts by carrying out its implementation of the notification method called on it.
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. The RowSetMetaData
interface provides methods for setting the information about columns, but an application would not normally use these methods. When an application calls the RowSet
method execute
, the RowSet
object will contain a new set of rows, and its RowSetMetaData
object will have been internally updated to contain information about the new columns.
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:
The RowSet
interface may be implemented in any number of ways, and anyone may write an implementation. Developers are encouraged to use their imaginations in coming up with new ways to use rowsets.
IMPORTANT NOTE: Code that uses API marked "Since 1.6" must be run using a JDBC technology driver that implements the JDBC 4.0 API. You must check your driver documentation to be sure that it implements the particular features you want to use.
javax.sql
package: