DataSource
引言
用Spring进行Web应用开发时,我们经常会做datasource的配置。而且datasource的配法风格各异。那么他们到底有哪些异同点呢?
DataSource作用
DataSource是javax.sql包中的类,是Java原生rt.jar包中的类。
public interface DataSource extends CommonDataSource, Wrapper {
Connection getConnection() throws SQLException;
Connection getConnection(String username, String password)
throws SQLException;
}
javax.sql.DataSource定义的是抽象方法,通过Java JNDI的方式将具体实现开放给各个厂商、组织自己、个人自己实现。
在Spring框架中,通过DataSource + 配置的方式,来定义具体的数据库源。并向Spring框架提供数据源的Connection服务。
在Spring中若想实现多数据源,那么就需要在DataSource下手。
javax.sql.DataSource源码注释说明
/**
* <p>A factory for connections to the physical data source that this
* {@code DataSource} object represents. An alternative to the
* {@code DriverManager} facility, a {@code DataSource} object
* is the preferred means of getting a connection. An object that implements
* the {@code DataSource} interface will typically be
* registered with a naming service based on the
* Java™ Naming and Directory (JNDI) API.
* <P>
* The {@code DataSource} interface is implemented by a driver vendor.
* There are three types of implementations:
* <OL>
* <LI>Basic implementation -- produces a standard {@code Connection}
* object
* <LI>Connection pooling implementation -- produces a {@code Connection}
* object that will automatically participate in connection pooling. This
* implementation works with a middle-tier connection pooling manager.
* <LI>Distributed transaction implementation -- produces a
* {@code Connection} object that may be used for distributed
* transactions and almost always participates in connection pooling.
* This implementation works with a middle-tier
* transaction manager and almost always with a connection
* pooling manager.
* </OL>
* <P>
* A {@code DataSource} object has properties that can be modified
* when necessary. For example, if the data source is moved to a different
* server, the property for the server can be changed. The benefit is that
* because the data source's properties can be changed, any code accessing
* that data source does not need to be changed.
* <P>
* A driver that is accessed via a {@code DataSource} object does not
* regist