使用初始连接和最大连接递增池大小

时间:2017-09-04 21:48:23

标签: java postgresql connection pool

我正在做一个池应用程序,其中有3个变量,A作为内部连接,它是20,b作为递增大小意味着当池达到20时,它将增加5乘以池大小,y作为最大值连接是50,我在以条件方式实现B变量时遇到问题,这就是代码

public class ThreadHolder extends Thread {
ArrayList<Client> pool;
public static int a = 20;
public static int b = 5;
public static int y = 50;
public static long ftime, stime, time;
public Client c = null;

public ThreadHolder(){
    pool = new ArrayList<Client>();
}

public void run(){
    stime = System.currentTimeMillis();
    try {
        while(pool.size()<= y){     
            if(pool.size() < a){    
                poolHelper();
                Thread.sleep(600);
            }

            else if(pool.size()> a){
                a = a+b;
                poolHelper();
                Thread.sleep(600);
            }   

            else {
                System.out.println("----------------------------------");
                System.out.println("Database is busy, please try again in a second.");  
                deleteClient(c);
                System.out.println("Pool -> "+pool.size() + ".");
                Thread.sleep(300);
        }   

        ftime = System.currentTimeMillis();
        time = (ftime - stime)/1000 ;
        System.out.println("Time established -> " + time + " sec");
        }   
    }catch(Exception e) {e.printStackTrace();}
}

public void poolHelper(){
    obtainClient(c);
    addClient(c);
    System.out.println("Pool -> "+pool.size() + ".");
}

public synchronized void obtainClient(Client c){
    Client a = new Client(this);
    a.start();
}

public synchronized void addClient(Client c){
    pool.add(c);
}

public synchronized void deleteClient(Client c){
    pool.remove(c);
}

}

如何实现B的使用,以便在我的poolize达到20个以上的连接时继续将我的池添加到我的池中

0 个答案:

没有答案