Annotation of java/classes/org/w3c/tools/jdbc/ConnectionManager.java, revision 1.2
1.1 bmahe 1: // ConnectionManager.java
1.2 ! bmahe 2: // $Id: ConnectionManager.java,v 1.1 2000/06/08 13:52:31 bmahe Exp $
1.1 bmahe 3: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5: package org.w3c.tools.jdbc;
6:
7: import java.sql.SQLException;
8: import java.util.Hashtable;
9:
10: import org.w3c.util.LRUList;
11: import org.w3c.util.SyncLRUList;
12:
13: /**
1.2 ! bmahe 14: * @version $Revision: 1.1 $
1.1 bmahe 15: * @author Beno�t Mah� (bmahe@w3.org)
16: */
17: public class ConnectionManager {
18:
19: public final static boolean debug = true;
20:
21: protected int conn_count = 0;
22: protected int conn_max = 5;
23:
24: /**
25: * The LRU list of connections.
26: */
27: protected LRUList connectionsLru = null;
28:
29: /**
30: * The cached servers, index is the jdbc URL
31: */
32: protected Hashtable servers = null;
33:
34: /**
35: * Get a connection to the given server.
36: * @param server the jdbc server.
37: * @return a JdbcConnection or null if there is no connection available.
38: */
39: protected JdbcConnection getConnection(JdbcServer server) {
40: JdbcServerState ss = server.getState();
41: while ( true ) {
42: JdbcConnection conn = null;
43: while ( true ) {
44: conn = ss.getConnection();
45: if ( conn == null )
46: break;
47: if (conn.markUsed())
48: return conn;
49: }
1.2 ! bmahe 50: if (negotiateConnection(server)) {
! 51: conn = allocateConnection(server);
1.1 bmahe 52: if ( conn.markUsed() ) {
53: return conn;
54: } else {
55: // Failed to establish a fresh connection !
56: return null;
57: }
58: }
59: // Wait for a connection to become available:
60: try {
1.2 ! bmahe 61: waitForConnection(server);
1.1 bmahe 62: } catch (InterruptedException ex) {
63: }
64: }
65: }
66:
67: /**
68: * Connections management - Allocate a new connection for this server.
69: * @param server the JdbcServer
70: * @return a newly created connection or null
71: */
72: protected JdbcConnection allocateConnection(JdbcServer server) {
1.2 ! bmahe 73: JdbcConnection conn = new JdbcConnection(server);
1.1 bmahe 74: notifyConnection(conn);
75: return conn;
76: }
77:
78: protected boolean negotiateConnection(JdbcServer server) {
79: return conn_count <= conn_max;
80: }
81:
82: protected void deleteConnection(JdbcConnection conn) {
83: JdbcServerState ss = conn.getServer().getState();
84: ss.deleteConnection(conn);
85: synchronized(this) {
86: --conn_count;
87: if (debug)
88: System.out.println("+++ delete conn_count: " + conn_count);
89: notify();
90: }
91: }
92:
93: /**
94: * A new connection has just been created.
95: * @param conn the new connection
96: */
97: protected synchronized void notifyConnection(JdbcConnection conn) {
98: ++conn_count;
99: }
100:
101: /**
102: * The given connection is about to be used.
103: * Update our list of available servers.
104: * @param conn The idle connection.
105: */
106: public void notifyUse(JdbcConnection conn) {
107: if (debug)
108: System.out.println("+++ JdbcConnection used");
109: connectionsLru.remove(conn);
110: }
111:
112: /**
113: * The given connection can be reused, but is now idle.
114: * @param conn The connection that is now idle.
115: */
116: public synchronized void notifyIdle(JdbcConnection conn) {
117: if (debug)
118: System.out.println("+++ JdbcConnection idle");
119: connectionsLru.toHead(conn);
120: JdbcServerState ss = conn.getServer().getState();
121: ss.registerConnection(conn);
122: notify();
123: }
124:
125: /**
126: * Wait for a connection to come up.
127: * @param server, the target server.
128: * @exception InterruptedException If interrupted..
129: */
130:
131: protected synchronized void waitForConnection(JdbcServer server)
132: throws InterruptedException
133: {
134: wait();
135: }
136:
137: protected static ConnectionManager getManager() {
138: return new ConnectionManager(); //FIXME
139: }
140:
141: private ConnectionManager() {
142:
143: }
144:
145: }
Webmaster