📄 jdbcpool.java
字号:
ResultSet rs=null;
try {
rs =cnn.executeQuery(sql);
this.releaseConnection(cnn);
} catch (JDBCException e) {
if(cnn!=null) this.releaseConnection(cnn);
e.printStackTrace();
throw e;
}
return rs;
}
public String[][] query(String sqlstr) {
//执行查询SQL语句
int rownum = 0; //二维数组的记录数
int colnum = 0; //二维数组的列数
try {
//System.out.println(rownum+":"+colnum);
ResultSet rs = executeQuery(sqlstr,ResultSet.TYPE_SCROLL_INSENSITIVE);
rownum = 0;
while (rs.next()) {
rownum++;
}
rs.beforeFirst();
colnum = rs.getMetaData().getColumnCount();
String[][] rsar = new String[rownum][colnum];
int i = 0;
int j = 0;
while (rs.next()) {
for (j = 0; j < colnum; j++) {
rsar[i][j] = rs.getString(j + 1);
}
i++;
}
rs.close();
return rsar;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("数据库返回结果集有误!");
return null;
}
}
public PreparedStatement prepareStatement(String sql) throws JDBCException{
JDBCCnn cnn=this.getFreeConnection();
PreparedStatement pstmt=null;
try {
pstmt =cnn.prepareStatement(sql);
this.releaseConnection(cnn);
} catch (JDBCException e) {
if(cnn!=null) this.releaseConnection(cnn);
e.printStackTrace();
throw e;
}
return pstmt;
}
public CallableStatement prepareCall(String sql)throws JDBCException{
JDBCCnn cnn=this.getFreeConnection();
CallableStatement cstmt=null;
try {
cstmt =cnn.prepareCall(sql);
this.releaseConnection(cnn);
} catch (JDBCException e) {
if(cnn!=null) this.releaseConnection(cnn);
e.printStackTrace();
throw e;
}
return cstmt;
}
/**
* 从连接池中获取一个连接,如果没有空闲连接,则等待。
* @return 获取的空闲连接
* @exception 增大连接池时出错
*/
protected JDBCCnn getFreeConnection()
throws JDBCException
{
JDBCCnn cnn;
boolean isf=isFull();
boolean incre;
int poolsize;
if(isShutting){
throw new JDBCException(JDBCCnnMgrConstants.EXCEP_SHUTTING);
}
synchronized(connPool){
poolsize=this.size();
//连接池已经满而且连接数已经达到连接池的上限
if(isf&&poolsize>=poolMaxSize){
//写日志
Logger.log(Logger.WARN,JDBCCnnMgrConstants.REACH_UPPER_LIMIT);
try {
connPool.wait();
} catch (InterruptedException e) {
throw new JDBCException(e.getMessage());
}
cnn= getFree();
if(cnn==null) throw new JDBCException(JDBCCnnMgrConstants.NO_FREE_CONN);
//cnn.status=JDBCCnn.TAKING;
//isUsing[pos] =true;
return cnn;
}
//连接池已经满但是连接数还未达到连接池的上限
if(isf){
incre=poolIncrease();
//增大连接池失败
if(!incre){
throw new JDBCException(JDBCCnnMgrConstants.EXCEP_POOL_INCREASE);
}
}
cnn=getFree();
if(cnn==null) throw new JDBCException(JDBCCnnMgrConstants.NO_FREE_CONN);
//System.out.println("Get free connection in getFreeConnection():"+cnn.toString());
return cnn;
}
}
/**
* 从连接池中获取一个连接,如果没有空闲连接,则等待timeout时间。
* 超时返回null
* @param timeout 设置连接超时
* @return 获取的空闲连接
* @exception 等待超时或者增大连接池出错
*/
protected JDBCCnn getFreeConnection(long timeout)
throws JDBCException
{
JDBCCnn cnn;
boolean isf=isFull();
boolean incre;
if(isShutting){
throw new JDBCException(JDBCCnnMgrConstants.EXCEP_SHUTTING);
}
synchronized(connPool){
int poolsize=this.size();
long startTime =java.lang.System.currentTimeMillis();
//System.out.println("is full:"+isf+"\t pool size:"+poolsize);
//连接池已经满而且连接数已经达到连接池的上限
if(isf&&poolsize>=poolMaxSize){
//写日志
//System.out.println(JDBCCnnMgrConstants.REACH_UPPER_LIMIT);
Logger.log(Logger.WARN,JDBCCnnMgrConstants.REACH_UPPER_LIMIT);
try {
connPool.wait(timeout);
} catch (InterruptedException e) {
throw new JDBCException(e.getMessage());
}
//System.out.println("wait:"+java.lang.System.currentTimeMillis());
//如果超时,返回null
long waittime=java.lang.System.currentTimeMillis()- startTime+1;
if (waittime>= timeout) {
// wait()返回的原因是超时
//System.out.println("timeout.");
throw new JDBCException(JDBCCnnMgrConstants.TIMEOUT_GET_CONN);
}
cnn=getFree();
if(cnn==null) throw new JDBCException(JDBCCnnMgrConstants.NO_FREE_CONN);
//cnn.status=JDBCCnn.TAKING;
//isUsing[pos] =true;
return cnn;
}
//连接池已经满但是连接数还未达到连接池的上限
if(isf){
incre=poolIncrease();
//增大连接池失败
if(!incre){
//写出错日志
throw new JDBCException(JDBCCnnMgrConstants.EXCEP_POOL_INCREASE);
}
}
cnn=getFree();
if(cnn==null) throw new JDBCException(JDBCCnnMgrConstants.NO_FREE_CONN);
//System.out.println("Get free connection in getFreeConnection(long timeout):"+cnn.toString());
return cnn;
}
}
/**
* 连接池动态增大
* @return false:连接池已经达到最大或增加时出错 true:连接池正常增加
* */
private boolean poolIncrease(){
Connection conn;
int poolsize=this.size();
//连接池已经达到最大,不能再增加连接。
if(poolsize>=poolMaxSize) {
Logger.log(Logger.WARN,JDBCCnnMgrConstants.REACH_UPPER_LIMIT);
return false;
}
//连接池还没有达到最大,可以继续增加。
try{
for(int i=0;i<increaseStep&&i<(poolMaxSize-poolsize);i++){
conn = DriverManager.getConnection(url,dbUser,dbPassword);
String poolID=driver.trim()+url.trim()+"."+this.dbUser.trim()+"."+this.dbPassword.trim();
connPool.add(new JDBCCnn(poolID,conn,url,dbUser,dbPassword,backDir));
//System.out.println("pool increase:"+(poolsize+i));
}
}catch(Exception e){
String errMsg=ExceptionHandle.getMessage(e);
Logger.log(Logger.ERROR,JDBCCnnMgrConstants.EXCEP_POOL_INCREASE);
Logger.log(Logger.ERROR,e.getMessage());
//e.printStackTrace();
return false;
}
return true;
}
/**
* 连接池动态缩小
* */
private void poolDecrease(){
int poolsize=this.size();
//如果连接池数目达到下限,不做扫描
//if(poolsize<=this.poolMinSize) return;
if(poolsize<=0) return;
try {
if(JDBCPoolManager.getInstance().isDebug()) this.printStatus();
} catch (JDBCException e) {
return;
}
scanPos++;
synchronized(connPool){
JDBCCnn cnn=(JDBCCnn)connPool.get(poolsize-1);
//如果最后一个连接在使用,则重置计数器
if(cnn.status==JDBCCnn.TAKING||cnn.status==JDBCCnn.EXECUTING){
scanPos=0;
return;
}
//最后一个连接没有被使用,而且达到空闲扫描次数
if(scanPos==(scanCount-1)){
//释放最后一个连接
//System.out.println("pool decrease:"+cnn.toString());
connPool.remove(cnn);
try{
cnn.close();
}catch(Exception e){
Logger.log(Logger.ERROR,"Exception occurs when pool decreasing.");
Logger.log(Logger.ERROR,e.getMessage());
}
scanPos=0;
}
return;
}
}
/**
* 关闭一个连接对象
* @param cnn 要关闭的连接对象
* */
protected void closeConnectin(JDBCCnn cnn)
throws JDBCException{
synchronized(connPool){
//System.out.println("before close:"+this.size());
connPool.remove(cnn);
cnn.close();
//System.out.println("after close:"+this.size());
}
}
/**
* 释放连接,释放完成后,通知所有等待
* @param cnn - 将被释放的连接
*/
protected void releaseConnection(JDBCCnn cnn)
{
//System.out.println("Release connection "+cnn.toString());
synchronized(connPool){
cnn.status =JDBCCnn.FREE;
try {
cnn.setAutoCommit(true);
} catch (Exception e) {
}
cnn=null;
connPool.notifyAll();
}
}
/**
* 显示连接池中所有连接的状态
* @exception 连接池正在关闭
* */
public void printStatus()
throws JDBCException
{
Iterator it=connPool.iterator();
if(isShutting){
throw new JDBCException(JDBCCnnMgrConstants.EXCEP_SHUTTING);
}
while(it.hasNext()){
JDBCCnn cnn=(JDBCCnn)it.next();
System.out.print(cnn.toString()+"\t");
switch(cnn.status){
case JDBCCnn.FREE:
System.out.print("FREE"+"\t"+cnn.getSqlString());
break;
case JDBCCnn.TAKING:
System.out.print("TAKING"+"\t"+cnn.getSqlString());
break;
case JDBCCnn.EXECUTING:
System.out.print("EXECUTING"+"\t"+cnn.getSqlString());
break;
}
System.out.println("\n");
}
System.out.println("\n");
}
public static void main(String[] args) {
}
/*class scanTask extends TimerTask{
public void run(){
}
} */
/**
* Returns the dELAY.
* @return int
*/
public static int getDELAY() {
return DELAY;
}
/**
* Returns the driver.
* @return String
*/
public String getDriver() {
return driver;
}
/**
* Returns the increaseStep.
* @return int
*/
public int getIncreaseStep() {
return increaseStep;
}
/**
* Returns the poolMaxSize.
* @return int
*/
public int getPoolMaxSize() {
return poolMaxSize;
}
/**
* Returns the poolMinSize.
* @return int
*/
public int getPoolMinSize() {
return poolMinSize;
}
/**
* Returns the poolName.
* @return String
*/
public String getPoolName() {
return poolName;
}
/**
* Returns the scanCount.
* @return int
*/
public int getScanCount() {
return scanCount;
}
/**
* Returns the scanInterval.
* @return int
*/
public int getScanInterval() {
return scanInterval;
}
/**
* Returns the url.
* @return String
*/
public String getUrl() {
return url;
}
/**
* Sets the dELAY.
* @param dELAY The dELAY to set
*/
public static void setDELAY(int dELAY) {
DELAY = dELAY;
}
/**
* Sets the increaseStep.
* @param increaseStep The increaseStep to set
*/
public void setIncreaseStep(int increaseStep) {
this.increaseStep = increaseStep;
}
/**
* Sets the scanCount.
* @param scanCount The scanCount to set
*/
public void setScanCount(int scanCount) {
this.scanCount = scanCount;
}
/**
* Sets the scanInterval.
* @param scanInterval The scanInterval to set
*/
public void setScanInterval(int scanInterval) {
this.scanInterval = scanInterval;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -