Revision 320
Added by bojilova over 24 years ago
src/edu/ucsb/nceas/metacat/MetaCatUtil.java | ||
---|---|---|
100 | 100 |
|
101 | 101 |
/* Utility method to get a unused Connection object from the pool */ |
102 | 102 |
public Connection getConnection() |
103 |
throws SQLException, ClassNotFoundException { |
|
103 |
throws SQLException, ClassNotFoundException, Exception {
|
|
104 | 104 |
|
105 | 105 |
Connection conn = null; |
106 | 106 |
Enumeration connections = connectionPool.keys(); |
107 | 107 |
|
108 | 108 |
synchronized (connectionPool) { |
109 | 109 |
while (connections.hasMoreElements()) { |
110 |
|
|
110 | 111 |
conn = (Connection)connections.nextElement(); |
111 | 112 |
Boolean b = (Boolean)connectionPool.get(conn); |
112 |
|
|
113 |
if (b == Boolean.FALSE) { |
|
113 |
|
|
114 |
if (conn == null) { |
|
115 |
// closed connection; replace it |
|
116 |
conn = openDBConnection(); |
|
117 |
// update the pool to show this one taken |
|
118 |
connectionPool.put(conn, Boolean.TRUE); |
|
119 |
|
|
120 |
return conn; |
|
121 |
} else if (b == Boolean.FALSE) { |
|
114 | 122 |
// unused connection; test if it works and get it |
115 | 123 |
try { |
116 | 124 |
conn.setAutoCommit(true); |
... | ... | |
120 | 128 |
} |
121 | 129 |
// update the pool to show this one taken |
122 | 130 |
connectionPool.put(conn, Boolean.TRUE); |
131 |
|
|
123 | 132 |
return conn; |
124 | 133 |
} |
125 | 134 |
} |
126 |
}
|
|
135 |
} |
|
127 | 136 |
|
128 | 137 |
// If we get here, there no free connections; |
129 | 138 |
// we need to make more |
130 | 139 |
int incConn = (new Integer(getOption("incrementConnections"))).intValue(); |
140 |
int maxConn = (new Integer(getOption("maximumConnections"))).intValue(); |
|
141 |
int k = connectionPool.size(); |
|
142 |
if ( k >= maxConn ) { |
|
143 |
throw new Exception("The maximum of " + maxConn + |
|
144 |
" open db connections is reached." + |
|
145 |
" New db connection to MetaCat" + |
|
146 |
" cannot be established."); |
|
147 |
} |
|
148 |
|
|
149 |
// decide how many new connections to open; no more than the maximum |
|
150 |
if ( incConn > maxConn - k ) { incConn = maxConn - k; } |
|
151 |
|
|
152 |
// get them |
|
131 | 153 |
for ( int i = 0; i < incConn; i++ ) { |
132 | 154 |
connectionPool.put( openDBConnection(), Boolean.FALSE ); |
133 | 155 |
} |
134 |
|
|
156 |
|
|
135 | 157 |
// Recurse to get one of the new connections |
136 | 158 |
return getConnection(); |
137 | 159 |
} |
... | ... | |
142 | 164 |
Connection conn; |
143 | 165 |
Enumeration connections = connectionPool.keys(); |
144 | 166 |
|
145 |
while (connections.hasMoreElements()) { |
|
167 |
if (returned != null) { |
|
168 |
while (connections.hasMoreElements()) { |
|
146 | 169 |
conn = (Connection)connections.nextElement(); |
147 | 170 |
if ( conn == returned ) { |
148 | 171 |
connectionPool.put( conn, Boolean.FALSE ); |
149 | 172 |
break; |
150 | 173 |
} |
151 |
} |
|
174 |
} |
|
175 |
} |
|
152 | 176 |
} |
177 |
|
|
178 |
/* Utility method to close all the connection from the pool */ |
|
179 |
public void closeConnections() { |
|
180 |
|
|
181 |
Connection conn; |
|
182 |
Enumeration connections = connectionPool.keys(); |
|
183 |
|
|
184 |
while (connections.hasMoreElements()) { |
|
185 |
conn = (Connection)connections.nextElement(); |
|
186 |
if ( conn != null ) { |
|
187 |
try { |
|
188 |
conn.close(); |
|
189 |
} catch (SQLException e) {} |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
153 | 193 |
|
154 | 194 |
|
155 | 195 |
/** Utility method to convert a file handle into a URL */ |
... | ... | |
196 | 236 |
|
197 | 237 |
/** |
198 | 238 |
* '$Log$ |
239 |
* 'Revision 1.11 2000/08/01 18:26:50 bojilova |
|
240 |
* 'added Pool of Connections |
|
241 |
* 'DBQuery, DBReader, DBTransform, DBUtil are created on every request and use the connections from the Pool |
|
242 |
* 'same with DBWriter and DBValidate |
|
243 |
* ' |
|
199 | 244 |
* 'Revision 1.10 2000/06/26 10:35:05 jones |
200 | 245 |
* 'Merged in substantial changes to DBWriter and associated classes and to |
201 | 246 |
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE |
src/edu/ucsb/nceas/metacat/MetaCatServlet.java | ||
---|---|---|
124 | 124 |
} |
125 | 125 |
} |
126 | 126 |
|
127 |
/** |
|
128 |
* Close all db connections from the pool |
|
129 |
*/ |
|
130 |
public void destroy() { |
|
131 |
|
|
132 |
if (util != null) { |
|
133 |
util.closeConnections(); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
127 | 137 |
/** Handle "GET" method requests from HTTP clients */ |
128 | 138 |
public void doGet (HttpServletRequest request, HttpServletResponse response) |
129 | 139 |
throws ServletException, IOException { |
... | ... | |
395 | 405 |
} |
396 | 406 |
|
397 | 407 |
try { |
408 |
|
|
398 | 409 |
conn = util.getConnection(); |
399 | 410 |
DBQuery queryobj = new DBQuery(conn, saxparser); |
400 | 411 |
doclist = queryobj.findDocuments(xmlquery); |
412 |
util.returnConnection(conn); |
|
413 |
|
|
401 | 414 |
} catch (Exception e) { |
415 |
if ( conn != null ) { util.returnConnection(conn); } |
|
402 | 416 |
response.setContentType("text/html"); |
403 | 417 |
out.println(e.getMessage()); |
404 | 418 |
return; |
405 |
} finally { |
|
406 |
if ( conn != null ) { util.returnConnection(conn); } |
|
407 | 419 |
} |
408 |
/* |
|
409 |
if (queryobj != null) { |
|
410 |
doclist = queryobj.findDocuments(xmlquery); |
|
411 |
} else { |
|
412 |
out.println("Query Object Init failed."); |
|
413 |
return; |
|
414 |
} |
|
415 |
*/ |
|
420 |
|
|
416 | 421 |
// Create a buffer to hold the xml result |
417 | 422 |
StringBuffer resultset = new StringBuffer(); |
418 | 423 |
|
... | ... | |
503 | 508 |
response.setContentType("text/html"); |
504 | 509 |
out.println(e.getMessage()); |
505 | 510 |
} finally { |
506 |
if ( conn != null ) { util.returnConnection(conn); }
|
|
511 |
util.returnConnection(conn);
|
|
507 | 512 |
} |
508 | 513 |
|
509 | 514 |
} |
... | ... | |
554 | 559 |
response.setContentType("text/html"); |
555 | 560 |
out.println(e.getMessage()); |
556 | 561 |
} finally { |
557 |
if ( conn != null ) { util.returnConnection(conn); }
|
|
562 |
util.returnConnection(conn);
|
|
558 | 563 |
} |
559 | 564 |
|
560 | 565 |
// set content type and other response header fields first |
... | ... | |
627 | 632 |
out.println(e.getMessage()); |
628 | 633 |
out.println("</error>"); |
629 | 634 |
} finally { |
630 |
if ( conn != null ) { util.returnConnection(conn); }
|
|
635 |
util.returnConnection(conn);
|
|
631 | 636 |
} |
632 | 637 |
} |
633 | 638 |
|
... | ... | |
659 | 664 |
} catch (NullPointerException npe) { |
660 | 665 |
response.setContentType("text/xml"); |
661 | 666 |
out.println("<error>Error getting document ID: " + docid + "</error>"); |
667 |
if ( conn != null ) { util.returnConnection(conn); } |
|
662 | 668 |
return; // Jivka added |
663 | 669 |
} catch (Exception e) { |
664 | 670 |
response.setContentType("text/html"); |
665 | 671 |
out.println(e.getMessage()); |
666 | 672 |
} finally { |
667 |
if ( conn != null ) { util.returnConnection(conn); }
|
|
673 |
util.returnConnection(conn);
|
|
668 | 674 |
} |
669 | 675 |
} |
670 | 676 |
|
... | ... | |
687 | 693 |
response.setContentType("text/html"); |
688 | 694 |
out.println(e.getMessage()); |
689 | 695 |
} finally { |
690 |
if ( conn != null ) { util.returnConnection(conn); }
|
|
696 |
util.returnConnection(conn);
|
|
691 | 697 |
} |
692 | 698 |
} |
693 | 699 |
|
... | ... | |
770 | 776 |
out.println(e.getMessage()); |
771 | 777 |
out.println("</error>"); |
772 | 778 |
} finally { |
773 |
if ( conn != null ) { util.returnConnection(conn); }
|
|
779 |
util.returnConnection(conn);
|
|
774 | 780 |
} |
775 | 781 |
|
776 | 782 |
} |
... | ... | |
807 | 813 |
out.println(e.getMessage()); |
808 | 814 |
out.println("</error>"); |
809 | 815 |
} finally { |
810 |
if ( conn != null ) { util.returnConnection(conn); }
|
|
816 |
util.returnConnection(conn);
|
|
811 | 817 |
} |
812 | 818 |
|
813 | 819 |
} |
... | ... | |
816 | 822 |
|
817 | 823 |
/** |
818 | 824 |
* '$Log$ |
825 |
* 'Revision 1.57 2000/08/03 23:20:31 bojilova |
|
826 |
* 'Changes related to "getdataguide" action |
|
827 |
* ' |
|
819 | 828 |
* 'Revision 1.55 2000/08/01 18:26:50 bojilova |
820 | 829 |
* 'added Pool of Connections |
821 | 830 |
* 'DBQuery, DBReader, DBTransform, DBUtil are created on every request and use the connections from the Pool |
Also available in: Unified diff
more precise handling of the Connection Pool