现在的位置: 首页 > 综合 > 正文

简单的池实现

2012年06月30日 ⁄ 综合 ⁄ 共 3278字 ⁄ 字号 评论关闭

这个组件的功能主要是处理小量的实例,实现池化功能。

在这组件的基础上又增加了一个preparedstatement的池。

使用此池的类只要象实现类一样,把两个方法写好即可

============实现类=================

package org.koron.db.exchange;

import java.sql.*;

import org.koron.db.DBConnection;

public class SwanStatementPool extends SwanPool<PreparedStatement> {

private String alias;
private String sql;
public SwanStatementPool(String alias, String sql) {
super();
this.alias = alias;
this.sql = sql;
}

public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
}

public String getSql() {
return sql;
}

public void setSql(String sql) {
this.sql = sql;
}

@Override
public PreparedStatement add() {
try {
return DBConnection.getConnection(alias).prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

@Override
public boolean remove(PreparedStatement t) {
try {
if (t != null) {
Connection connection = t.getConnection();
if(!t.isClosed())
t.close();
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}

=====================================抽象类================

package org.koron.db.exchange;

import java.util.ArrayList;

/**
* 实现池功能
* 此类的继承类只需要实现add和remove两个方法即可。
* add方法是生成一个池中的实例,remove是把某个池中的实例给释放 。
* @author FANGZW
*
*/
public abstract class SwanPool<T> {
/**
* 池的最大值
*/
private int maxSize =5;
/**
* 最小数
*/
private int minSize = 1;
/**
* 最大空闲数
*/
private int idleSize =1;
/**
* 最大等待时间,如果超过这个时间,直接强制回收,调用remove方法
*/
private int maxPeriod;
/**
* 当无法获得资源,后续请求会进入wait队列,如果队列超出长度,则直接返回null
* -1表示无限制
* 暂时未使用此变量
*/
private int buffers = -1;
/**
* 申请资源线程的最长等待时间,单位:毫秒
*/
private int timeout = 5000;
/**
* 资源池
*/
private ArrayList<PoolDataUnit<T>> al = new ArrayList<PoolDataUnit<T>>();

class PoolDataUnit<T>
{
/**
* 借出的资源
*/
private T t;
/**
* 借出时间
*/
private long borrowTime;
/**
* 是否已借出
*/
private boolean borrowed;
public PoolDataUnit(T t) {
super();
this.t = t;
this.borrowTime = -1;
this.borrowed = false;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public long getBorrowTime() {
return borrowTime;
}
public void setBorrowTime(long borrowTime) {
this.borrowTime = borrowTime;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}

}
/**
* 生成资源在{@link #borrow}中使用。当所有资源均已被使用,则调用此方法,并把此对象加入池中。
* @return
*/
public abstract T add();
/**
* 回收资源
* @param t
* @return
*/
public abstract boolean remove(T t);

/**
* 调用 {@link #nativeBorrow()}获取资源。
* 如果池中没资源,则把些对象放入等待队列中,等待通知
* @return
*/
public synchronized T borrow()
{
long enterStamp = System.currentTimeMillis();
T t = nativeBorrow();
try {
while(t==null && System.currentTimeMillis()-enterStamp < timeout)//如果还没获得得资源,并且超时时间还没到则继续等待
{
wait();
t = nativeBorrow();
}
return t;
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
/**
* 内部返回资源,当有资源未被使用则直接返回。
* 当池中已创建的资源还有剩余则直接返回,并设置状态。
* 如果没空闲资源,并且资源数小于最大资源数,则调用{@link #add()} 生成资源,并设置状态返回此资源
* @return
*/
private synchronized T nativeBorrow()
{
for (PoolDataUnit<T> unit : al) {
if(System.currentTimeMillis()-unit.getBorrowTime() > maxPeriod)//如果发现资源已过期,直接调remove方法进行移除资源
remove(unit.getT());
if(!unit.isBorrowed())//如果此资源未使用或者此资源被使用的时间超过了时限
{
unit.setBorrowed(true);
unit.setBorrowTime(System.currentTimeMillis());
return unit.getT();
}
}
if(al.size() < maxSize)
{
T t = add();
PoolDataUnit<T> unit = new PoolDataUnit<T>(t);
al.add(unit);
unit.setBorrowed(true);
unit.setBorrowTime(System.currentTimeMillis());
return t;
}
return null;
}

/**
* 还回对象,把对象放回池中,并通知所有等待获取资源的线程
* @param t
*/
public synchronized void lent(T t)
{
for (PoolDataUnit<T> unit : al) {
if(unit.getT().equals(t))
{
unit.setBorrowed(false);
unit.setBorrowTime(System.currentTimeMillis());
notifyAll();
return;
}
}
remove(t);
}
}

抱歉!评论已关闭.