Path: blob/master/src/java.sql/share/classes/javax/sql/DataSource.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*/2425package javax.sql;2627import java.sql.Connection;28import java.sql.ConnectionBuilder;29import java.sql.SQLException;30import java.sql.SQLFeatureNotSupportedException;31import java.sql.Wrapper;3233/**34* <p>A factory for connections to the physical data source that this35* {@code DataSource} object represents. An alternative to the36* {@code DriverManager} facility, a {@code DataSource} object37* is the preferred means of getting a connection. An object that implements38* the {@code DataSource} interface will typically be39* registered with a naming service based on the40* Java Naming and Directory (JNDI) API.41* <P>42* The {@code DataSource} interface is implemented by a driver vendor.43* There are three types of implementations:44* <OL>45* <LI>Basic implementation -- produces a standard {@code Connection}46* object47* <LI>Connection pooling implementation -- produces a {@code Connection}48* object that will automatically participate in connection pooling. This49* implementation works with a middle-tier connection pooling manager.50* <LI>Distributed transaction implementation -- produces a51* {@code Connection} object that may be used for distributed52* transactions and almost always participates in connection pooling.53* This implementation works with a middle-tier54* transaction manager and almost always with a connection55* pooling manager.56* </OL>57* <P>58* A {@code DataSource} object has properties that can be modified59* when necessary. For example, if the data source is moved to a different60* server, the property for the server can be changed. The benefit is that61* because the data source's properties can be changed, any code accessing62* that data source does not need to be changed.63* <P>64* A driver that is accessed via a {@code DataSource} object does not65* register itself with the {@code DriverManager}. Rather, a66* {@code DataSource} object is retrieved through a lookup operation67* and then used to create a {@code Connection} object. With a basic68* implementation, the connection obtained through a {@code DataSource}69* object is identical to a connection obtained through the70* {@code DriverManager} facility.71* <p>72* An implementation of {@code DataSource} must include a public no-arg73* constructor.74*75* @since 1.476*/7778public interface DataSource extends CommonDataSource, Wrapper {7980/**81* <p>Attempts to establish a connection with the data source that82* this {@code DataSource} object represents.83*84* @return a connection to the data source85* @throws SQLException if a database access error occurs86* @throws java.sql.SQLTimeoutException when the driver has determined that the87* timeout value specified by the {@code setLoginTimeout} method88* has been exceeded and has at least tried to cancel the89* current database connection attempt90*/91Connection getConnection() throws SQLException;9293/**94* <p>Attempts to establish a connection with the data source that95* this {@code DataSource} object represents.96*97* @param username the database user on whose behalf the connection is98* being made99* @param password the user's password100* @return a connection to the data source101* @throws SQLException if a database access error occurs102* @throws java.sql.SQLTimeoutException when the driver has determined that the103* timeout value specified by the {@code setLoginTimeout} method104* has been exceeded and has at least tried to cancel the105* current database connection attempt106* @since 1.4107*/108Connection getConnection(String username, String password)109throws SQLException;110111/**112* {@inheritDoc}113* @since 1.4114*/115@Override116java.io.PrintWriter getLogWriter() throws SQLException;117118/**119* {@inheritDoc}120* @since 1.4121*/122@Override123void setLogWriter(java.io.PrintWriter out) throws SQLException;124125/**126* {@inheritDoc}127* @since 1.4128*/129@Override130void setLoginTimeout(int seconds) throws SQLException;131132/**133* {@inheritDoc}134* @since 1.4135*/136@Override137int getLoginTimeout() throws SQLException;138139// JDBC 4.3140141/**142* Create a new {@code ConnectionBuilder} instance143* @implSpec144* The default implementation will throw a {@code SQLFeatureNotSupportedException}145* @return The ConnectionBuilder instance that was created146* @throws SQLException if an error occurs creating the builder147* @throws SQLFeatureNotSupportedException if the driver does not support sharding148* @since 9149* @see ConnectionBuilder150*/151default ConnectionBuilder createConnectionBuilder() throws SQLException {152throw new SQLFeatureNotSupportedException("createConnectionBuilder not implemented");153};154155}156157158