summaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/org/postgresql/ObjectPoolFactory.java
blob: 404c366daa6cc04cb5e7cee3f4a9fc2a84af2563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.postgresql;

import java.util.Hashtable;

/**
 * Just a factory class for creating and reusing 
 * ObjectPool arrays of different sizes.
 */
public class ObjectPoolFactory {
    private static Hashtable instances = new Hashtable();

    ObjectPool pool = new ObjectPool();
    int maxsize;

    public static ObjectPoolFactory getInstance(int size){
	Integer s = new Integer(size);
	ObjectPoolFactory poolFactory = (ObjectPoolFactory) instances.get(s);
	if(poolFactory == null){
	    synchronized(instances) {
		poolFactory = (ObjectPoolFactory) instances.get(s);
		if(poolFactory == null){
		    poolFactory = new ObjectPoolFactory(size);
		    instances.put(s, poolFactory);
		}
	    }
	}
	return poolFactory;
    }

    private ObjectPoolFactory(int maxsize){
	this.maxsize = maxsize;
    }
    
    public ObjectPool[] getObjectPoolArr(){
	ObjectPool p[] = null;
	synchronized(pool){
	    if(pool.size() > 0)
		p = (ObjectPool []) pool.remove();
	}
	if(p == null) {
	    p = new ObjectPool[maxsize];
	    for(int i = 0; i < maxsize; i++){
		p[i] = new ObjectPool();
	    }
	}
	return p;
    }

    public void releaseObjectPoolArr(ObjectPool p[]){
	synchronized(pool){
	    pool.add(p);
	    for(int i = 0; i < maxsize; i++){
		p[i].clear();
	    }
	}
    }
}