#享元设计模式 适用于连接池技术,可以提高程序的运行效率
public class PoolConnection{
private static final String USER="root";
private static final String PASSROWD="XXXX";
private static final String URL="XXX";
private static final String DRIVER="org.cn.mysql.xxx";
private int poolSize=100;
List<Connection> poolList;
Connection connection;
public PoolConnection(){
poolList=new ArrayList<>();
for(int i=0;i<poolSize;i++){
try {
Class.forName(DRIVER);
connection=DriverManager.getConnection(URL, USER,PASSROWD);
poolList.add(connection);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
public synchronized void realease(Connection connection){
poolList.add(connection);
}
public synchronized Connection getConnection(){
if(poolList.size()>0){
Connection connection=poolList.get(0);
poolList.remove(connection);
return connection;
}
return null;
}
}
享元设计模式
最新推荐文章于 2024-09-20 20:28:38 发布