DataSource接口

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

DataSource接⼝
介绍
JDBC2.0 提供了javax.sql.DataSource接⼝,它负责建⽴与数据库的连接,当在应⽤程序中访问数据库时
不必编写连接数据库的代码,直接引⽤DataSource获取数据库的连接对象即可。

⽤于获取操作数据Connection对象。

在jdbc中我们获取数据库连接对象,需要先在DriverManager中注册,再通过DriverManager获取Connection对象。

//1、注册mysql驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/zg_test?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai"; String username = "root";
String password = "123456";
//通过DriverManager获取Connection
Connection connection = DriverManager.getConnection(url, username, password);
有了DataSource接⼝后,可以快速获得Connection对象,也⽅便实现数据库连接池。

public interface DataSource extends CommonDataSource, Wrapper {
/**
* <p>Attempts to establish a connection with the data source that
* this {@code DataSource} object represents.
*
* @return a connection to the data source
* @exception SQLException if a database access error occurs
* @throws java.sql.SQLTimeoutException when the driver has determined that the
* timeout value specified by the {@code setLoginTimeout} method
* has been exceeded and has at least tried to cancel the
* current database connection attempt
*/
Connection getConnection() throws SQLException;
/**
* <p>Attempts to establish a connection with the data source that
* this {@code DataSource} object represents.
*
* @param username the database user on whose behalf the connection is
* being made
* @param password the user's password
* @return a connection to the data source
* @exception SQLException if a database access error occurs
* @throws java.sql.SQLTimeoutException when the driver has determined that the
* timeout value specified by the {@code setLoginTimeout} method
* has been exceeded and has at least tried to cancel the
* current database connection attempt
* @since 1.4
*/
Connection getConnection(String username, String password)
throws SQLException;
}
UnpooledDataSource
UnpooledDataSource是由mybatis提供的⼀种最简单的实现,没有使⽤到数据库连接池。

其实现和jdbc⼀摸⼀样。

实现了DataSource的getConnect⽅法,实际去执⾏doGetConnection⽅法。

@Override
public Connection getConnection() throws SQLException {
return doGetConnection(username, password);
}
doGetConnection
private Connection doGetConnection(Properties properties) throws SQLException {
initializeDriver();
Connection connection = DriverManager.getConnection(url, properties);
configureConnection(connection);
return connection;
}
doGetConnection也是注册驱动,然后通过DriverManager获取。

private synchronized void initializeDriver() throws SQLException {
if (!registeredDrivers.containsKey(driver)) {
Class<?> driverType;
try {
if (driverClassLoader != null) {
//和我们⾃⼰使⽤jdbc⼀样
driverType = Class.forName(driver, true, driverClassLoader);
} else {
driverType = Resources.classForName(driver);
}
// DriverManager requires the driver to be loaded via the system ClassLoader.
// /~nsayer/Java/dyn-jdbc.html
Driver driverInstance = (Driver)driverType.newInstance();
DriverManager.registerDriver(new DriverProxy(driverInstance));
registeredDrivers.put(driver, driverInstance);
} catch (Exception e) {
throw new SQLException("Error setting driver on UnpooledDataSource. Cause: " + e); }
}
}。

相关文档
最新文档