//
// mybatis的数据源
//
package org.apache.ibatis.datasource.pooled;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
public class PooledDataSource implements DataSource {
private static final Log log = LogFactory.getLog(PooledDataSource.class);
private final PoolState state = new PoolState(this);
private final UnpooledDataSource dataSource;
//连接池最大连接数
protected int poolMaximumActiveConnections = 10;
//连接池最大闲置连接数
protected int poolMaximumIdleConnections = 5;
//连接池最大检测时间
protected int poolMaximumCheckoutTime = 20000;
//连接池等待时间
protected int poolTimeToWait = 20000;
protected int poolMaximumLocalBadConnectionTolerance = 3;
protected String poolPingQuery = "NO PING QUERY SET";
protected boolean poolPingEnabled;
protected int poolPingConnectionsNotUsedFor;
private int expectedConnectionTypeCode;
//构造方法
public PooledDataSource() {
this.dataSource = new UnpooledDataSource();
}
public PooledDataSource(UnpooledDataSource dataSource) {
this.dataSource = dataSource;
}
public PooledDataSource(String driver, String url, String username, String password) {
this.dataSource = new UnpooledDataSource(driver, url, username, password);
this.expectedConnectionTypeCode = this.assembleConnectionTypeCode(this.dataSource.getUrl(), this.dataSource.getUsername(), this.dataSource.getPassword());
}
public PooledDataSource(String driver, String url, Properties driverProperties) {
this.dataSource = new UnpooledDataSource(driver, url, driverProperties);
this.expectedConnectionTypeCode = this.assembleConnectionTypeCode(this.dataSource.getUrl(), this.dataSource.getUsername(), this.dataSource.getPassword());
}
public PooledDataSource(ClassLoader driverClassLoader, String driver, String url, String username, String password) {
this.dataSource = new UnpooledDataSource(driverClassLoader, driver, url, username, password);
this.expectedConnectionTypeCode = this.assembleConnectionTypeCode(this.dataSource.getUrl(), this.dataSource.getUsername(), this.dataSource.getPassword());
}
public PooledDataSource(ClassLoader driverClassLoader, String driver, String url, Properties driverProperties) {
this.dataSource = new UnpooledDataSource(driverClassLoader, driver, url, driverProperties);
this.expectedConnectionTypeCode = this.assembleConnectionTypeCode(this.dataSource.getUrl(), this.dataSource.getUsername(), this.dataSource.getPassword());
}
//获取连接
public Connection getConnection() throws SQLException {
return this.popConnection(this.dataSource.getUsername(), this.dataSource.getPassword()).getProxyConnection();
}
public Connection getConnection(String username, String password) throws SQLException {
return this.popConnection(username, password).getProxyConnection();
}
public void setLoginTimeout(int loginTimeout) {
DriverManager.setLoginTimeout(loginTimeout);
}
public int getLoginTimeout() {
return DriverManager.getLoginTimeout();
}
public void setLogWriter(PrintWriter logWriter) {
DriverManager.setLogWriter(logWriter);
}
public PrintWriter getLogWriter() {
return DriverManager.getLogWriter();
}
public void setDriver(String driver) {
this.dataSource.setDriver(driver);
this.forceCloseAll();
}
public void setUrl(String url) {
this.dataSource.setUrl(url);
this.forceCloseAll();
}
public void setUsername(String username) {
this.dataSource.setUsername(username);
this.forceCloseAll();
}
public void setPassword(String password) {
this.dataSource.setPassword(password);
this.forceCloseAll();
}
public void setDefaultAutoCommit(
mybatis的默认数据源
最新推荐文章于 2023-12-12 09:33:51 发布