1   /*
2    *  Copyright (c) 1998-2002, The University of Sheffield.
3    *
4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
5    *  software, licenced under the GNU Library General Public License,
6    *  Version 2, June 1991 (in the distribution as file licence.html,
7    *  and also available at http://gate.ac.uk/gate/licence.html).
8    *
9    *  Atanas Kiryakov, 01/02/2002
10   *
11   *  $Id: ConnectionPool.java,v 1.1 2002/02/04 08:12:46 nasso Exp $
12   */
13  package gate.persist;
14  
15  
16  import java.util.*;
17  import java.sql.*;
18  
19  /**
20     A generic implementation of pool of references to objects of any kind.
21     It is thread-safe, so, allows multiple users to get and release objects
22     "simultaneously". In fact, the standard Java synchronization is used.
23     <BR><BR>
24     The base idea is that, a calling routine will try to get
25     an object from the pool with method Get. On success, it will use the
26     object and return it in the pool with the method Put.
27     <BR><BR>
28     If there ares no available objects in the pool, Get will return <B>null</B>.
29     Then the calling routine should create a new object. Further, scenario goes
30     in the same way - when finished using the object, calling routine shoud Put
31     it in the pool for future use, instead of leaving it to be garbage-collected.
32     <BR><BR>
33     The pool initially is empty. The only way to increase the number of objects
34     managed by the pool, is some external process to Put an object, that was
35     created, instead of previously Get from the pool.
36     <BR><BR>
37     Pool stores only references to currently "free" or available objects. When
38     some external routine Gets an object from the pool, its reference is not
39     locked, it is simply removed from the pool.
40   */
41  public class ConnectionPool {
42     private Vector   connections;
43     private int      size;
44     private String   url;
45     private int      connCount;
46  
47  /**
48     Constructs and object pool with specified size.
49     @param size determines the maximum size of the pool. This is the number
50           free objects that it can manage at the same time
51   */
52     public ConnectionPool(int size, String url) {
53        this.size = size;
54        connections = new Vector(this.size);
55        this.url = url;
56        this.connCount = 0;
57     } // ConnectionPool
58  
59  /**
60     Pulls out an object from the pool. The reference to the object is removed
61     from the pool and their is no longer any kind of relation between this
62     object and the pool. It can be returned back (released) by Put method.
63     @return  an object from the pool, if available.<BR>
64              Otherwise, returns <B>null</B>
65   */
66    public synchronized Connection get()
67        throws SQLException,ClassNotFoundException{
68        int currAvailable = connections.size();
69        if (currAvailable > 0){
70          Connection conn = (Connection) connections.elementAt(currAvailable-1);
71          connections.removeElementAt(currAvailable-1);
72          return conn;
73        }
74        else {
75          if (connCount < size) {
76            Connection newCon = DBHelper.connect(url);
77            connCount++;
78            return newCon;
79          }
80          else {
81            try {
82              wait();
83            }
84            catch (java.lang.InterruptedException ie) {
85              throw new SQLException(" Thread interrupted while waiting "
86                                      +"to get Connection from ConnectionPool !");
87            }
88            return get();
89          }
90        }
91     } // Get
92  
93  /**
94     Puts an object in the pool, those stating that it's "free" or available
95     for use by another processes and routines. An object can be put in the pool,
96     without need to be get from there before.
97     @return  <B>true</B> on success<BR>
98              <B>false</B> when the object was not put in the pool,
99              because the maximal number of references in the pool was riched
100  */
101    public synchronized boolean put(Connection conn){
102       connections.addElement(conn);
103       notify();
104       return true;
105    } // Put
106 
107    public void finalize() {
108      for (int i = 0; i<connections.size(); i++){
109         try {
110           DBHelper.disconnect((Connection) connections.elementAt(i));
111         }
112         catch (Exception e){
113             e.printStackTrace();
114         }
115      }
116    }
117 }