數據源 DataSourcejava
javax.sql 接口 DataSourcesql
從1.4版本開始數據庫
DataSource 對象表示的物理數據源的鏈接。做爲 DriverManager 設施的替代項,DataSource 對象是獲取鏈接的首選方法。實現 DataSource 接口的對象一般在基於 JavaTM Naming and Directory Interface (JNDI) API 的命名服務中註冊。服務器
DataSource 接口由驅動程序供應商實現。共有三種類型的實現:app
基本實現 - 生成標準 Connection 對象分佈式
鏈接池實現 - 生成自動參與鏈接池的 Connection 對象。此實現與中間層鏈接池管理器一塊兒使用。this
分佈式事務實現 - 生成一個 Connection 對象,該對象可用於分佈式事務,而且幾乎始終參與鏈接池。此實現與中間層事務管理器一塊兒使用,而且幾乎始終與鏈接池管理器一塊兒使用。spa
DataSource 對象的屬性在須要時能夠修改。例如,若是將數據源移動到另外一個服務器,則可更改與服務器相關的屬性。其優勢是,由於能夠更改數據源的屬性,因此任何訪問該數據源的代碼都無需更改。日誌
經過 DataSource 對象訪問的驅動程序不會向 DriverManager 註冊。經過查找操做檢索 DataSource 對象,而後使用該對象建立 Connection 對象。使用基本的實現,經過 DataSource 對象獲取的鏈接與經過 DriverManager 設施獲取的鏈接相同。對象
方法摘要
Connection getConnection()
嘗試創建與此 DataSource 對象表示的數據源的鏈接。
Connection getConnection(String username, String password)
嘗試創建與此 DataSource 對象表示的數據源的鏈接。
int getLoginTimeout()
獲取此數據源嘗試鏈接到某一數據庫時能夠等待的最長時間,以秒爲單位。
PrintWriter getLogWriter()
檢索此 DataSource 對象的日誌 writer。
void setLoginTimeout(int seconds)
設置數據源嘗試鏈接到某一數據庫時將等待的最長時間,以秒爲單位。
void setLogWriter(PrintWriter out)
將此 DataSource 對象的日誌 writer 設置爲給定的 java.io.PrintWriter 對象。
從1.6時開始
Method Summary
Connection getConnection()
Attempts to establish a connection with the data source that this DataSource object represents.
Connection getConnection(String username, String password)
Attempts to establish a connection with the data source that this DataSource object represents.
Methods inherited from interface javax.sql.CommonDataSource
getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
Methods inherited from interface java.sql.Wrapper
isWrapperFor, unwrap
1.4 增長包javax.sql
1.5 增長子包avax.sql.rowset
1.6 類改造CommonDataSource等
1.7 類改造RowSetFactory等
1.8 內部代碼修改