Path: blob/master/src/java.sql/share/classes/javax/sql/package-info.java
41152 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* Provides the API for server side data source access and processing from27* the Java programming language.28* This package supplements the {@code java.sql}29* package and, as of the version 1.4 release, is included in the30* Java Platform, Standard Edition (Java SE).31* It remains an essential part of the Java Platform, Enterprise Edition32* (Java EE).33* <p>34* The {@code javax.sql} package provides for the following:35* <OL>36* <LI>The {@code DataSource} interface as an alternative to the37* {@code DriverManager} for establishing a38* connection with a data source39* <LI>Connection pooling and Statement pooling40* <LI>Distributed transactions41* <LI>Rowsets42* </OL>43* <p>44* Applications use the {@code DataSource} and {@code RowSet}45* APIs directly, but the connection pooling and distributed transaction46* APIs are used internally by the middle-tier infrastructure.47*48* <H2>Using a {@code DataSource} Object to Make a Connection</H2>49* <p>50* The {@code javax.sql} package provides the preferred51* way to make a connection with a data source. The {@code DriverManager}52* class, the original mechanism, is still valid, and code using it will53* continue to run. However, the newer {@code DataSource} mechanism54* is preferred because it offers many advantages over the55* {@code DriverManager} mechanism.56* <p>57* These are the main advantages of using a {@code DataSource} object to58* make a connection:59* <UL>60*61* <LI>Changes can be made to a data source's properties, which means62* that it is not necessary to make changes in application code when63* something about the data source or driver changes.64* <LI>Connection and Statement pooling and distributed transactions are available65* through a {@code DataSource} object that is66* implemented to work with the middle-tier infrastructure.67* Connections made through the {@code DriverManager}68* do not have connection and statement pooling or distributed transaction69* capabilities.70* </UL>71* <p>72* Driver vendors provide {@code DataSource} implementations. A73* particular {@code DataSource} object represents a particular74* physical data source, and each connection the {@code DataSource} object75* creates is a connection to that physical data source.76* <p>77* A logical name for the data source is registered with a naming service that78* uses the Java Naming and Directory Interface79* (JNDI) API, usually by a system administrator or someone performing the80* duties of a system administrator. An application can retrieve the81* {@code DataSource} object it wants by doing a lookup on the logical82* name that has been registered for it. The application can then use the83* {@code DataSource} object to create a connection to the physical data84* source it represents.85* <p>86* A {@code DataSource} object can be implemented to work with the87* middle tier infrastructure so that the connections it produces will be88* pooled for reuse. An application that uses such a {@code DataSource}89* implementation will automatically get a connection that participates in90* connection pooling.91* A {@code DataSource} object can also be implemented to work with the92* middle tier infrastructure so that the connections it produces can be93* used for distributed transactions without any special coding.94*95* <H2>Connection Pooling and Statement Pooling</H2>96* <p>97* Connections made via a {@code DataSource}98* object that is implemented to work with a middle tier connection pool manager99* will participate in connection pooling. This can improve performance100* dramatically because creating new connections is very expensive.101* Connection pooling allows a connection to be used and reused,102* thus cutting down substantially on the number of new connections103* that need to be created.104* <p>105* Connection pooling is totally transparent. It is done automatically106* in the middle tier of a Java EE configuration, so from an application's107* viewpoint, no change in code is required. An application simply uses108* the {@code DataSource.getConnection} method to get the pooled109* connection and uses it the same way it uses any {@code Connection}110* object.111* <p>112* The classes and interfaces used for connection pooling are:113* <UL>114* <LI>{@code ConnectionPoolDataSource}115* <LI>{@code PooledConnection}116* <LI>{@code ConnectionEvent}117* <LI>{@code ConnectionEventListener}118* <LI>{@code StatementEvent}119* <LI>{@code StatementEventListener}120* </UL>121* The connection pool manager, a facility in the middle tier of122* a three-tier architecture, uses these classes and interfaces123* behind the scenes. When a {@code ConnectionPoolDataSource} object124* is called on to create a {@code PooledConnection} object, the125* connection pool manager will register as a {@code ConnectionEventListener}126* object with the new {@code PooledConnection} object. When the connection127* is closed or there is an error, the connection pool manager (being a listener)128* gets a notification that includes a {@code ConnectionEvent} object.129* <p>130* If the connection pool manager supports {@code Statement} pooling, for131* {@code PreparedStatements}, which can be determined by invoking the method132* {@code DatabaseMetaData.supportsStatementPooling}, the133* connection pool manager will register as a {@code StatementEventListener}134* object with the new {@code PooledConnection} object. When the135* {@code PreparedStatement} is closed or there is an error, the connection136* pool manager (being a listener)137* gets a notification that includes a {@code StatementEvent} object.138*139* <H2>Distributed Transactions</H2>140* <p>141* As with pooled connections, connections made via a {@code DataSource}142* object that is implemented to work with the middle tier infrastructure143* may participate in distributed transactions. This gives an application144* the ability to involve data sources on multiple servers in a single145* transaction.146* <p>147* The classes and interfaces used for distributed transactions are:148* <UL>149* <LI>{@code XADataSource}150* <LI>{@code XAConnection}151* </UL>152* These interfaces are used by the transaction manager; an application does153* not use them directly.154* <p>155* The {@code XAConnection} interface is derived from the156* {@code PooledConnection} interface, so what applies to a pooled connection157* also applies to a connection that is part of a distributed transaction.158* A transaction manager in the middle tier handles everything transparently.159* The only change in application code is that an application cannot do anything160* that would interfere with the transaction manager's handling of the transaction.161* Specifically, an application cannot call the methods {@code Connection.commit}162* or {@code Connection.rollback}, and it cannot set the connection to be in163* auto-commit mode (that is, it cannot call164* {@code Connection.setAutoCommit(true)}).165* <p>166* An application does not need to do anything special to participate in a167* distributed transaction.168* It simply creates connections to the data sources it wants to use via169* the {@code DataSource.getConnection} method, just as it normally does.170* The transaction manager manages the transaction behind the scenes. The171* {@code XADataSource} interface creates {@code XAConnection} objects, and172* each {@code XAConnection} object creates an {@code XAResource} object173* that the transaction manager uses to manage the connection.174*175*176* <H2>Rowsets</H2>177* The {@code RowSet} interface works with various other classes and178* interfaces behind the scenes. These can be grouped into three categories.179* <OL>180* <LI>Event Notification181* <UL>182* <LI>{@code RowSetListener}<br>183* A {@code RowSet} object is a JavaBeans184* component because it has properties and participates in the JavaBeans185* event notification mechanism. The {@code RowSetListener} interface186* is implemented by a component that wants to be notified about events that187* occur to a particular {@code RowSet} object. Such a component registers188* itself as a listener with a rowset via the {@code RowSet.addRowSetListener}189* method.190* <p>191* When the {@code RowSet} object changes one of its rows, changes all of192* it rows, or moves its cursor, it also notifies each listener that is registered193* with it. The listener reacts by carrying out its implementation of the194* notification method called on it.195* <LI>{@code RowSetEvent}<br>196* As part of its internal notification process, a {@code RowSet} object197* creates an instance of {@code RowSetEvent} and passes it to the listener.198* The listener can use this {@code RowSetEvent} object to find out which rowset199* had the event.200* </UL>201* <LI>Metadata202* <UL>203* <LI>{@code RowSetMetaData}<br>204* This interface, derived from the205* {@code ResultSetMetaData} interface, provides information about206* the columns in a {@code RowSet} object. An application can use207* {@code RowSetMetaData} methods to find out how many columns the208* rowset contains and what kind of data each column can contain.209* <p>210* The {@code RowSetMetaData} interface provides methods for211* setting the information about columns, but an application would not212* normally use these methods. When an application calls the {@code RowSet}213* method {@code execute}, the {@code RowSet} object will contain214* a new set of rows, and its {@code RowSetMetaData} object will have been215* internally updated to contain information about the new columns.216* </UL>217* <LI>The Reader/Writer Facility<br>218* A {@code RowSet} object that implements the {@code RowSetInternal}219* interface can call on the {@code RowSetReader} object associated with it220* to populate itself with data. It can also call on the {@code RowSetWriter}221* object associated with it to write any changes to its rows back to the222* data source from which it originally got the rows.223* A rowset that remains connected to its data source does not need to use a224* reader and writer because it can simply operate on the data source directly.225*226* <UL>227* <LI>{@code RowSetInternal}<br>228* By implementing the {@code RowSetInternal} interface, a229* {@code RowSet} object gets access to230* its internal state and is able to call on its reader and writer. A rowset231* keeps track of the values in its current rows and of the values that immediately232* preceded the current ones, referred to as the <i>original</i> values. A rowset233* also keeps track of (1) the parameters that have been set for its command and234* (2) the connection that was passed to it, if any. A rowset uses the235* {@code RowSetInternal} methods behind the scenes to get access to236* this information. An application does not normally invoke these methods directly.237*238* <LI>{@code RowSetReader}<br>239* A disconnected {@code RowSet} object that has implemented the240* {@code RowSetInternal} interface can call on its reader (the241* {@code RowSetReader} object associated with it) to populate it with242* data. When an application calls the {@code RowSet.execute} method,243* that method calls on the rowset's reader to do much of the work. Implementations244* can vary widely, but generally a reader makes a connection to the data source,245* reads data from the data source and populates the rowset with it, and closes246* the connection. A reader may also update the {@code RowSetMetaData} object247* for its rowset. The rowset's internal state is also updated, either by the248* reader or directly by the method {@code RowSet.execute}.249*250*251* <LI>{@code RowSetWriter}<br>252* A disconnected {@code RowSet} object that has implemented the253* {@code RowSetInternal} interface can call on its writer (the254* {@code RowSetWriter} object associated with it) to write changes255* back to the underlying data source. Implementations may vary widely, but256* generally, a writer will do the following:257*258* <UL>259* <LI>Make a connection to the data source260* <LI>Check to see whether there is a conflict, that is, whether261* a value that has been changed in the rowset has also been changed262* in the data source263* <LI>Write the new values to the data source if there is no conflict264* <LI>Close the connection265* </UL>266*267*268* </UL>269* </OL>270* <p>271* The {@code RowSet} interface may be implemented in any number of272* ways, and anyone may write an implementation. Developers are encouraged273* to use their imaginations in coming up with new ways to use rowsets.274*275*276* <h2>Package Specification</h2>277*278* <ul>279* <li><a href="https://jcp.org/en/jsr/detail?id=221">JDBC 4.3 Specification</a>280* </ul>281*282* <h2>Related Documentation</h2>283* <p>284* The Java Series book published by Addison-Wesley Longman provides detailed285* information about the classes and interfaces in the {@code javax.sql}286* package:287*288* <ul>289* <li>“<i>JDBC™API Tutorial and Reference, Third Edition</i>”290* </ul>291*/292package javax.sql;293294295