Revision 1217
Added by Jing Tao over 22 years ago
src/edu/ucsb/nceas/metacat/DBValidate.java | ||
---|---|---|
96 | 96 |
} |
97 | 97 |
|
98 | 98 |
/** Construct a new validation object using a database entity resolver */ |
99 |
public DBValidate(String parserName, Connection conn) { |
|
99 |
public DBValidate(String parserName, DBConnection conn) {
|
|
100 | 100 |
this(parserName); |
101 | 101 |
|
102 | 102 |
DBEntityResolver dbresolver = new DBEntityResolver(conn); |
... | ... | |
208 | 208 |
String doc = args[0]; |
209 | 209 |
|
210 | 210 |
MetaCatUtil util = new MetaCatUtil(); |
211 |
DBConnection conn = null; |
|
212 |
int serailNumber = -1; |
|
211 | 213 |
try { |
212 |
Connection conn = util.openDBConnection(); |
|
214 |
conn = DBConnectionPool.getDBConnection("DBValidate.main"); |
|
215 |
serailNumber = conn.getCheckOutSerialNumber(); |
|
213 | 216 |
|
214 | 217 |
DBValidate gxv = new DBValidate(util.getOption("saxparser"), conn); |
215 | 218 |
if (gxv.validate(doc)) { |
... | ... | |
219 | 222 |
} |
220 | 223 |
} catch (SQLException e) { |
221 | 224 |
System.out.println("<error>Couldn't open database connection.</error>"); |
222 |
} catch (ClassNotFoundException e) { |
|
223 |
System.out.println("<error>Couldn't open database connection.</error>"); |
|
224 |
} |
|
225 |
} |
|
226 |
finally |
|
227 |
{ |
|
228 |
DBConnectionPool.returnDBConnection(conn, serailNumber); |
|
229 |
}//finally |
|
225 | 230 |
} |
226 | 231 |
|
227 | 232 |
|
src/edu/ucsb/nceas/metacat/DBConnectionPool.java | ||
---|---|---|
78 | 78 |
final static long CYCLETIMEOFDBCONNECTION = |
79 | 79 |
Long.parseLong(MetaCatUtil.getOption("cycleTimeOfDBConnection")); |
80 | 80 |
|
81 |
//the number for trying to check out a connection in the pool in |
|
82 |
//getDBConnection method |
|
83 |
final static int LIMIT = 2; |
|
81 | 84 |
|
82 | 85 |
final static int FREE = 0; //status of a connection |
83 | 86 |
final static int BUSY = 1; //statis of a connection |
... | ... | |
185 | 188 |
throws SQLException |
186 | 189 |
{ |
187 | 190 |
DBConnection db = null; |
188 |
MetaCatUtil.debugMessage("Try to checking out connection...", 10); |
|
189 |
//try every DBConnection in the pool |
|
190 |
|
|
191 |
for (int i=0; i<connectionPool.size(); i++) |
|
191 |
int random = 0; //random number |
|
192 |
int index = 0; //index |
|
193 |
int size = 0; //size of connection pool |
|
194 |
MetaCatUtil.debugMessage("Try to checking out connection...", 45); |
|
195 |
size = connectionPool.size(); |
|
196 |
MetaCatUtil.debugMessage("size of connection pool: "+size, 45); |
|
197 |
|
|
198 |
//try every DBConnection in the pool |
|
199 |
//every DBConnection will be try LIMITE times |
|
200 |
for (int j=0 ; j<LIMIT; j++) |
|
201 |
{ |
|
202 |
//create a random number as the started index for connection pool |
|
203 |
//So that the connection ofindex of 0 wouldn't be a the heaviest user |
|
204 |
random = (new Double (Math.random()*100)).intValue(); |
|
205 |
for (int i=0; i<size; i++) |
|
192 | 206 |
{ |
193 |
db = (DBConnection) connectionPool.elementAt(i); |
|
207 |
index =(i+random)%size; |
|
208 |
db = (DBConnection) connectionPool.elementAt(index); |
|
209 |
MetaCatUtil.debugMessage("Index: "+index, 45); |
|
210 |
MetaCatUtil.debugMessage("Tag: "+db.getTag(), 45); |
|
211 |
MetaCatUtil.debugMessage("Status: "+db.getStatus(), 45); |
|
194 | 212 |
//check if the connection is free |
195 | 213 |
if (db.getStatus()==FREE) |
196 | 214 |
{ |
... | ... | |
208 | 226 |
db.setCheckOutMethodName(methodName); |
209 | 227 |
//debug message |
210 | 228 |
MetaCatUtil.debugMessage("The connection is checked out: " |
211 |
+db.getTag(), 20); |
|
212 |
MetaCatUtil.debugMessage("The age is "+db.getAge(), 25); |
|
213 |
MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 25); |
|
229 |
+db.getTag(), 35); |
|
230 |
MetaCatUtil.debugMessage("The method for checking is: " |
|
231 |
+db.getCheckOutMethodName(), 35); |
|
232 |
MetaCatUtil.debugMessage("The age is "+db.getAge(), 40); |
|
233 |
MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 40); |
|
214 | 234 |
MetaCatUtil.debugMessage("The conection time it has: " |
215 |
+db.getConnectionTime(), 25);
|
|
235 |
+db.getConnectionTime(), 40);
|
|
216 | 236 |
//set check out time |
217 | 237 |
db.setCheckOutTime(System.currentTimeMillis()); |
218 | 238 |
//check it out |
... | ... | |
223 | 243 |
//close this DBConnection |
224 | 244 |
db.close(); |
225 | 245 |
//remove it form connection pool |
226 |
connectionPool.remove(i); |
|
246 |
connectionPool.remove(index);
|
|
227 | 247 |
//insert a new DBConnection to same palace |
228 | 248 |
db = new DBConnection(); |
229 |
connectionPool.insertElementAt(db, i); |
|
249 |
connectionPool.insertElementAt(db, index);
|
|
230 | 250 |
}//else |
231 | 251 |
}//if |
232 | 252 |
}//for |
253 |
}//for |
|
233 | 254 |
|
234 |
|
|
235 | 255 |
//if couldn't get a connection, we should increase DBConnection pool |
236 | 256 |
//if the connection pool size is less than maximum connection number |
237 |
int poolSize = connectionPool.size(); |
|
238 |
if ( poolSize < MAXIMUMCONNECTIONNUMBER )
|
|
257 |
|
|
258 |
if ( size < MAXIMUMCONNECTIONNUMBER )
|
|
239 | 259 |
{ |
240 |
if ((poolSize+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
|
|
260 |
if ((size+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER)
|
|
241 | 261 |
{ |
242 | 262 |
//if we can create INCREASECONNECTIONNUMBER of new DBConnection |
243 | 263 |
//add to connection pool |
... | ... | |
251 | 271 |
{ |
252 | 272 |
//There is no enough room to increase INCREASECONNECTIONNUMBER |
253 | 273 |
//we create new DBCoonection to Maximum connection number |
254 |
for (int i= poolSize+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
|
|
274 |
for (int i= size+1; i<= MAXIMUMCONNECTIONNUMBER; i++)
|
|
255 | 275 |
{ |
256 | 276 |
DBConnection dbConn = new DBConnection(); |
257 | 277 |
connectionPool.add(dbConn); |
... | ... | |
261 | 281 |
}//if |
262 | 282 |
else |
263 | 283 |
{ |
264 |
throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + |
|
284 |
/*throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER +
|
|
265 | 285 |
" open db connections is reached." + |
266 | 286 |
" New db connection to MetaCat" + |
267 |
" cannot be established."); |
|
268 |
|
|
269 |
}//else |
|
270 |
|
|
271 |
//recursive to get new connection |
|
272 |
return getDBConnection(); |
|
273 |
}//getDBConnection |
|
274 |
|
|
275 |
/** |
|
276 |
* Method to get a DBConnection in connection pool |
|
277 |
* 1) try to get a DBConnection from DBConnection pool |
|
278 |
* 2) if 1) failed, then check the size of pool. If the size reach the |
|
279 |
* maximum number of connection, throw a exception: couldn't get one |
|
280 |
* 3) If the size is less than the maximum number of connectio, create some |
|
281 |
* new connections and recursive get one |
|
282 |
*/ |
|
283 |
public static synchronized DBConnection getDBConnection() throws SQLException |
|
284 |
{ |
|
285 |
DBConnection db = null; |
|
286 |
MetaCatUtil.debugMessage("Try to checking out connection...", 10); |
|
287 |
//try every DBConnection in the pool |
|
288 |
|
|
289 |
for (int i=0; i<connectionPool.size(); i++) |
|
290 |
{ |
|
291 |
db = (DBConnection) connectionPool.elementAt(i); |
|
292 |
//check if the connection is free |
|
293 |
if (db.getStatus()==FREE) |
|
294 |
{ |
|
295 |
//If this connection is good, return this DBConnection |
|
296 |
if (validateDBConnection(db)) |
|
297 |
{ |
|
298 |
|
|
299 |
//set this DBConnection status |
|
300 |
db.setStatus(BUSY); |
|
301 |
//increase checkout serial number |
|
302 |
db.increaseCheckOutSerialNumber(1); |
|
303 |
//increase one usageCount |
|
304 |
db.increaseUsageCount(1); |
|
305 |
//debug message |
|
306 |
MetaCatUtil.debugMessage("The connection is checked out: " |
|
307 |
+db.getTag(), 25); |
|
308 |
MetaCatUtil.debugMessage("The age is "+db.getAge(), 25); |
|
309 |
MetaCatUtil.debugMessage("The usage is "+db.getUsageCount(), 25); |
|
310 |
MetaCatUtil.debugMessage("The conection time it has: " |
|
311 |
+db.getConnectionTime(), 25); |
|
312 |
//set check out time |
|
313 |
db.setCheckOutTime(System.currentTimeMillis()); |
|
314 |
//check it out |
|
315 |
return db; |
|
316 |
}//if |
|
317 |
else//The DBConnection has some problem |
|
318 |
{ |
|
319 |
//close this DBConnection |
|
320 |
db.close(); |
|
321 |
//remove it form connection pool |
|
322 |
connectionPool.remove(i); |
|
323 |
//insert a new DBConnection to same palace |
|
324 |
db = new DBConnection(); |
|
325 |
connectionPool.insertElementAt(db, i); |
|
326 |
}//else |
|
327 |
}//if |
|
328 |
}//for |
|
329 |
|
|
330 |
|
|
331 |
//if couldn't get a connection, we should increase DBConnection pool |
|
332 |
//if the connection pool size is less than maximum connection number |
|
333 |
int poolSize = connectionPool.size(); |
|
334 |
if ( poolSize < MAXIMUMCONNECTIONNUMBER ) |
|
335 |
{ |
|
336 |
if ((poolSize+INCREASECONNECTIONNUMBER) < MAXIMUMCONNECTIONNUMBER) |
|
337 |
{ |
|
338 |
//if we can create INCREASECONNECTIONNUMBER of new DBConnection |
|
339 |
//add to connection pool |
|
340 |
for ( int i=0; i<INCREASECONNECTIONNUMBER; i++) |
|
341 |
{ |
|
342 |
DBConnection dbConn = new DBConnection(); |
|
343 |
connectionPool.add(dbConn); |
|
344 |
}//for |
|
345 |
}//if |
|
346 |
else |
|
347 |
{ |
|
348 |
//There is no enough room to increase INCREASECONNECTIONNUMBER |
|
349 |
//we create new DBCoonection to Maximum connection number |
|
350 |
for (int i= poolSize+1; i<= MAXIMUMCONNECTIONNUMBER; i++) |
|
351 |
{ |
|
352 |
DBConnection dbConn = new DBConnection(); |
|
353 |
connectionPool.add(dbConn); |
|
354 |
}//for |
|
355 |
}//else |
|
356 |
|
|
357 |
}//if |
|
358 |
else |
|
359 |
{ |
|
360 |
throw new SQLException("The maximum of " +MAXIMUMCONNECTIONNUMBER + |
|
287 |
" cannot be established.");*/ |
|
288 |
MetaCatUtil.debugMessage("The maximum of " +MAXIMUMCONNECTIONNUMBER + |
|
361 | 289 |
" open db connections is reached." + |
362 | 290 |
" New db connection to MetaCat" + |
363 |
" cannot be established."); |
|
291 |
" cannot be established.", 1); |
|
292 |
return null; |
|
293 |
//if couldn't get a connection, sleep 20 seconds and try again. |
|
294 |
/*try |
|
295 |
{ |
|
296 |
System.out.println("sleep"); |
|
297 |
Thread.sleep(2000); |
|
298 |
} |
|
299 |
catch (Exception e) |
|
300 |
{ |
|
301 |
}*/ |
|
302 |
|
|
364 | 303 |
|
365 | 304 |
}//else |
366 | 305 |
|
367 | 306 |
//recursive to get new connection |
368 |
return getDBConnection();
|
|
307 |
return getDBConnection(methodName);
|
|
369 | 308 |
}//getDBConnection |
309 |
|
|
310 |
|
|
370 | 311 |
|
371 | 312 |
|
372 |
|
|
373 | 313 |
/** |
374 | 314 |
* Method to check if a db connection works fine or not |
375 | 315 |
* Check points include: |
... | ... | |
382 | 322 |
*/ |
383 | 323 |
private static boolean validateDBConnection (DBConnection dbConn) |
384 | 324 |
{ |
385 |
Connection conn = null; |
|
386 | 325 |
|
326 |
|
|
387 | 327 |
//Check if the DBConnection usageCount if it is too many |
388 | 328 |
if (dbConn.getUsageCount() >= MAXIMUMUSAGENUMBER ) |
389 | 329 |
{ |
... | ... | |
408 | 348 |
} |
409 | 349 |
|
410 | 350 |
//Try to run a simple query |
411 |
conn = dbConn.getConnections(); |
|
412 | 351 |
try |
413 | 352 |
{ |
414 | 353 |
long startTime=System.currentTimeMillis(); |
415 |
conn.getMetaData();
|
|
354 |
DatabaseMetaData metaData = dbConn.getMetaData();
|
|
416 | 355 |
long stopTime=System.currentTimeMillis(); |
417 | 356 |
//increase one usagecount |
418 | 357 |
dbConn.increaseUsageCount(1); |
... | ... | |
446 | 385 |
{ |
447 | 386 |
MetaCatUtil.debugMessage("Couldn't find a DBConnection in the pool" |
448 | 387 |
+" which have same tag to the returned" |
449 |
+" DBConnetion object"); |
|
388 |
+" DBConnetion object", 30);
|
|
450 | 389 |
return; |
451 | 390 |
|
452 | 391 |
}//if |
... | ... | |
470 | 409 |
|
471 | 410 |
//set check out time to 0 |
472 | 411 |
dbConn.setCheckOutTime(0); |
473 |
|
|
412 |
|
|
474 | 413 |
MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+" checked in.", |
475 |
20); |
|
414 |
35); |
|
415 |
MetaCatUtil.debugMessage("Connection: "+dbConn.getTag()+"'s status: " |
|
416 |
+dbConn.getStatus(), 35); |
|
417 |
|
|
476 | 418 |
}//if |
419 |
else |
|
420 |
{ |
|
421 |
MetaCatUtil.debugMessage("This DBConnection couldn't return", 30); |
|
422 |
}//else |
|
477 | 423 |
}//else |
478 | 424 |
|
479 | 425 |
|
... | ... | |
489 | 435 |
{ |
490 | 436 |
int index = -1; |
491 | 437 |
String info = null; |
438 |
//if conn is null return -1 too |
|
439 |
if (conn==null) |
|
440 |
{ |
|
441 |
return -1; |
|
442 |
} |
|
492 | 443 |
//get tag of this returned DBConnection |
493 | 444 |
info = conn.getTag(); |
494 | 445 |
//if the tag is null or empty, -1 will be returned |
... | ... | |
615 | 566 |
}//while |
616 | 567 |
}//run |
617 | 568 |
|
569 |
/** |
|
570 |
* Method to get the number of free DBConnection in DBConnection pool |
|
571 |
*/ |
|
572 |
public int getFreeDBConnectionNumber() |
|
573 |
{ |
|
574 |
int numberOfFreeDBConnetion = 0; //return number |
|
575 |
DBConnection db = null; //single DBconnection |
|
576 |
int poolSize = 0; //size of connection pool |
|
577 |
//get the size of DBConnection pool |
|
578 |
poolSize = connectionPool.size(); |
|
579 |
//Check every DBConnection in the pool |
|
580 |
for ( int i=0; i<poolSize; i++) |
|
581 |
{ |
|
582 |
|
|
583 |
db = (DBConnection) connectionPool.elementAt(i); |
|
584 |
//check the status of db. If it is free, count it |
|
585 |
if (db.getStatus() == FREE) |
|
586 |
{ |
|
587 |
numberOfFreeDBConnetion++; |
|
588 |
}//if |
|
589 |
}//for |
|
590 |
//return the count result |
|
591 |
return numberOfFreeDBConnetion; |
|
592 |
}//getFreeDBConnectionNumber |
|
593 |
|
|
594 |
/** |
|
595 |
* Method to print out the method name which have busy DBconnection |
|
596 |
*/ |
|
597 |
public void printMethodNameHavingBusyDBConnection() |
|
598 |
{ |
|
599 |
|
|
600 |
DBConnection db = null; //single DBconnection |
|
601 |
int poolSize = 0; //size of connection pool |
|
602 |
//get the size of DBConnection pool |
|
603 |
poolSize = connectionPool.size(); |
|
604 |
//Check every DBConnection in the pool |
|
605 |
for ( int i=0; i<poolSize; i++) |
|
606 |
{ |
|
607 |
|
|
608 |
db = (DBConnection) connectionPool.elementAt(i); |
|
609 |
//check the status of db. If it is free, count it |
|
610 |
if (db.getStatus() == BUSY) |
|
611 |
{ |
|
612 |
MetaCatUtil.debugMessage("This method having a busy DBConnection: " |
|
613 |
+db.getCheckOutMethodName(), 30); |
|
614 |
MetaCatUtil.debugMessage("The busy DBConnection tag is: " |
|
615 |
+db.getTag(), 30); |
|
616 |
}//if |
|
617 |
}//for |
|
618 |
|
|
619 |
}//printMethodNameHavingBusyDBConnection |
|
620 |
|
|
618 | 621 |
}//DBConnectionPool |
src/edu/ucsb/nceas/metacat/DBConnection.java | ||
---|---|---|
379 | 379 |
conn.commit(); |
380 | 380 |
}//commit |
381 | 381 |
|
382 |
/** |
|
383 |
* Method to set commit mode |
|
384 |
* @param autocommit, true of false to auto commit |
|
385 |
*/ |
|
386 |
public void setAutoCommit( boolean autoCommit) throws SQLException |
|
387 |
{ |
|
388 |
conn.setAutoCommit(autoCommit); |
|
389 |
}//setAutoCommit |
|
382 | 390 |
|
391 |
/** |
|
392 |
* Method to roll back |
|
393 |
*/ |
|
394 |
public void rollback() throws SQLException |
|
395 |
{ |
|
396 |
conn.rollback(); |
|
397 |
}//rollback |
|
398 |
|
|
399 |
/** |
|
400 |
* Method to get meta data |
|
401 |
*/ |
|
402 |
public DatabaseMetaData getMetaData() throws SQLException |
|
403 |
{ |
|
404 |
return conn.getMetaData(); |
|
405 |
}//getMetaData |
|
406 |
|
|
383 | 407 |
}//DBConnection class |
src/edu/ucsb/nceas/metacat/MetacatReplication.java | ||
---|---|---|
241 | 241 |
HttpServletResponse response) |
242 | 242 |
{ |
243 | 243 |
String subaction = ((String[])params.get("subaction"))[0]; |
244 |
Connection conn = null; |
|
244 |
DBConnection dbConn = null; |
|
245 |
int serialNumber = -1; |
|
246 |
PreparedStatement pstmt = null; |
|
245 | 247 |
|
246 | 248 |
try { |
247 |
conn = util.openDBConnection(); |
|
248 |
PreparedStatement pstmt = null; |
|
249 |
|
|
249 |
//conn = util.openDBConnection(); |
|
250 |
dbConn=DBConnectionPool. |
|
251 |
getDBConnection("MetacatReplication.handleServerControlRequest"); |
|
252 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
253 |
|
|
250 | 254 |
// add server to server list |
251 | 255 |
if ( subaction.equals("add") ) { |
252 | 256 |
String replicate = ((String[])params.get("replicate"))[0]; |
253 | 257 |
String server = ((String[])params.get("server"))[0]; |
254 |
pstmt = conn.prepareStatement("INSERT INTO xml_replication " +
|
|
258 |
pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
|
|
255 | 259 |
"(server, last_checked, replicate) " + |
256 | 260 |
"VALUES ('" + server + "', to_date(" + |
257 | 261 |
"'01/01/00', 'MM/DD/YY'), '" + |
258 | 262 |
replicate + "')"); |
259 | 263 |
pstmt.execute(); |
260 | 264 |
pstmt.close(); |
261 |
conn.commit();
|
|
265 |
dbConn.commit();
|
|
262 | 266 |
out.println("Server " + server + " added"); |
263 | 267 |
response.setContentType("text/html"); |
264 | 268 |
out.println("<html><body><table border=\"1\">"); |
265 | 269 |
out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>"); |
266 | 270 |
out.println("<b>replicate</b></td></tr>"); |
267 |
pstmt = conn.prepareStatement("SELECT * FROM xml_replication"); |
|
271 |
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication"); |
|
272 |
//increase dbconnection usage |
|
273 |
dbConn.increaseUsageCount(1); |
|
274 |
|
|
268 | 275 |
pstmt.execute(); |
269 | 276 |
ResultSet rs = pstmt.getResultSet(); |
270 | 277 |
boolean tablehasrows = rs.next(); |
... | ... | |
284 | 291 |
// delete server from server list |
285 | 292 |
} else if ( subaction.equals("delete") ) { |
286 | 293 |
String server = ((String[])params.get("server"))[0]; |
287 |
pstmt = conn.prepareStatement("DELETE FROM xml_replication " +
|
|
294 |
pstmt = dbConn.prepareStatement("DELETE FROM xml_replication " +
|
|
288 | 295 |
"WHERE server LIKE '" + server + "'"); |
289 | 296 |
pstmt.execute(); |
290 | 297 |
pstmt.close(); |
291 |
conn.commit();
|
|
298 |
dbConn.commit();
|
|
292 | 299 |
out.println("Server " + server + " deleted"); |
293 | 300 |
response.setContentType("text/html"); |
294 | 301 |
out.println("<html><body><table border=\"1\">"); |
295 | 302 |
out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>"); |
296 | 303 |
out.println("<b>replicate</b></td></tr>"); |
297 |
pstmt = conn.prepareStatement("SELECT * FROM xml_replication"); |
|
304 |
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication"); |
|
305 |
//increase dbconnection usage |
|
306 |
dbConn.increaseUsageCount(1); |
|
298 | 307 |
pstmt.execute(); |
299 | 308 |
ResultSet rs = pstmt.getResultSet(); |
300 | 309 |
boolean tablehasrows = rs.next(); |
... | ... | |
313 | 322 |
out.println("<html><body><table border=\"1\">"); |
314 | 323 |
out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>"); |
315 | 324 |
out.println("<b>replicate</b></td></tr>"); |
316 |
pstmt = conn.prepareStatement("SELECT * FROM xml_replication");
|
|
325 |
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
|
|
317 | 326 |
pstmt.execute(); |
318 | 327 |
ResultSet rs = pstmt.getResultSet(); |
319 | 328 |
boolean tablehasrows = rs.next(); |
... | ... | |
326 | 335 |
out.println("</table></body></html>"); |
327 | 336 |
} |
328 | 337 |
pstmt.close(); |
329 |
conn.close(); |
|
338 |
//conn.close();
|
|
330 | 339 |
|
331 | 340 |
} catch(Exception e) { |
332 | 341 |
System.out.println("Error in " + |
... | ... | |
334 | 343 |
e.getMessage()); |
335 | 344 |
e.printStackTrace(System.out); |
336 | 345 |
} |
346 |
finally |
|
347 |
{ |
|
348 |
try |
|
349 |
{ |
|
350 |
pstmt.close(); |
|
351 |
}//try |
|
352 |
catch (SQLException ee) |
|
353 |
{ |
|
354 |
MetaCatUtil.debugMessage("Error in " + |
|
355 |
"MetacatReplication.handleServerControlRequest to close pstmt " |
|
356 |
+ ee.getMessage()); |
|
357 |
}//catch |
|
358 |
finally |
|
359 |
{ |
|
360 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
361 |
}//finally |
|
362 |
}//finally |
|
363 |
|
|
337 | 364 |
} |
338 | 365 |
|
339 | 366 |
// download certificate with the public key from certURL and |
... | ... | |
408 | 435 |
String dbaction = "UPDATE"; // the default action is UPDATE |
409 | 436 |
boolean override = false; |
410 | 437 |
int serverCode = 1; |
438 |
DBConnection dbConn = null; |
|
439 |
int serialNumber = -1; |
|
411 | 440 |
|
412 | 441 |
try { |
413 | 442 |
//if the url contains a dbaction then the default action is overridden |
... | ... | |
443 | 472 |
String homeServer=(String)docinfoHash.get("home_server"); |
444 | 473 |
util.debugMessage("homeServer: "+homeServer,20); |
445 | 474 |
// write the document here |
446 |
Connection conn = util.openDBConnection(); |
|
447 |
DocumentImpl.writeReplication(conn, new StringReader(xmldoc), null, null, |
|
448 |
dbaction, docid, user, null, homeServer, false); |
|
475 |
//Connection conn = util.openDBConnection(); |
|
476 |
dbConn=DBConnectionPool. |
|
477 |
getDBConnection("MetacatReplication.handleForceReplicateRequest"); |
|
478 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
479 |
DocumentImpl.writeReplication(dbConn, |
|
480 |
new StringReader(xmldoc), null, null, dbaction, docid, user, |
|
481 |
null, homeServer, false); |
|
449 | 482 |
|
450 | 483 |
|
451 |
conn.close(); |
|
484 |
//conn.close();
|
|
452 | 485 |
|
453 | 486 |
MetacatReplication.replLog("document " + docid + " added to DB with " + |
454 | 487 |
"action " + dbaction); |
... | ... | |
458 | 491 |
"Request(): " + e.getMessage()); |
459 | 492 |
e.printStackTrace(); |
460 | 493 |
} |
494 |
finally |
|
495 |
{ |
|
496 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
497 |
}//finally |
|
461 | 498 |
} |
462 | 499 |
|
463 | 500 |
/** |
... | ... | |
489 | 526 |
boolean override = false; |
490 | 527 |
int serverCode = 1; |
491 | 528 |
String dbaction=null; |
492 |
Connection conn=null; |
|
529 |
//Connection conn=null;
|
|
493 | 530 |
|
494 | 531 |
try |
495 | 532 |
{ |
... | ... | |
534 | 571 |
//the xml_documents |
535 | 572 |
if (dbaction.equals("delete")) |
536 | 573 |
{ |
537 |
conn = util.getConnection(); |
|
538 |
DocumentImpl.delete(conn,docid,user,null);
|
|
539 |
util.returnConnection(conn); |
|
574 |
//conn = util.getConnection();
|
|
575 |
DocumentImpl.delete(docid,user,null); |
|
576 |
//util.returnConnection(conn);
|
|
540 | 577 |
} |
541 | 578 |
//To data file insert or update is same |
542 | 579 |
else if (dbaction.equals("insert")||dbaction.equals("update")) |
... | ... | |
560 | 597 |
} catch(Exception e) |
561 | 598 |
{ |
562 | 599 |
|
563 |
util.returnConnection(conn); |
|
600 |
//util.returnConnection(conn);
|
|
564 | 601 |
util.debugMessage("ERROR in MetacatReplication.handleForceReplicate" + |
565 | 602 |
"Request(): " + e.getMessage(), 30); |
566 | 603 |
} |
... | ... | |
575 | 612 |
private void handleGetLockRequest(PrintWriter out, Hashtable params, |
576 | 613 |
HttpServletResponse response) |
577 | 614 |
{ |
578 |
Connection conn = null; |
|
615 |
//Connection conn = null;
|
|
579 | 616 |
try |
580 | 617 |
{ |
581 |
conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
582 |
"handleGetLockRequest"); |
|
618 |
/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
|
|
619 |
"handleGetLockRequest");*/
|
|
583 | 620 |
String docid = ((String[])params.get("docid"))[0]; |
584 | 621 |
String remoteRev = ((String[])params.get("updaterev"))[0]; |
585 |
DocumentImpl requestDoc = new DocumentImpl(conn, docid);
|
|
622 |
DocumentImpl requestDoc = new DocumentImpl(docid); |
|
586 | 623 |
MetacatReplication.replLog("lock request for " + docid); |
587 | 624 |
int localRevInt = requestDoc.getRev(); |
588 | 625 |
int remoteRevInt = Integer.parseInt(remoteRev); |
... | ... | |
612 | 649 |
MetacatReplication.replLog("lock denied for " + docid + |
613 | 650 |
"reason: client has outdated file"); |
614 | 651 |
} |
615 |
conn.close(); |
|
652 |
//conn.close();
|
|
616 | 653 |
} |
617 | 654 |
catch(Exception e) |
618 | 655 |
{ |
... | ... | |
634 | 671 |
{ |
635 | 672 |
String docid = ((String[])(params.get("docid")))[0]; |
636 | 673 |
StringBuffer sb = new StringBuffer(); |
637 |
Connection conn = null; |
|
674 |
//Connection conn = null;
|
|
638 | 675 |
try |
639 | 676 |
{ |
640 |
conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
641 |
"handleGetDocumentInfoRequest");
|
|
642 |
DocumentImpl doc = new DocumentImpl(conn, docid);
|
|
677 |
/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
|
|
678 |
"handleGetDocumentInfoRequest");*/
|
|
679 |
DocumentImpl doc = new DocumentImpl(docid); |
|
643 | 680 |
sb.append("<documentinfo><docid>").append(docid); |
644 | 681 |
sb.append("</docid><docname>").append(doc.getDocname()); |
645 | 682 |
sb.append("</docname><doctype>").append(doc.getDoctype()); |
... | ... | |
655 | 692 |
sb.append("</rev></documentinfo>"); |
656 | 693 |
response.setContentType("text/xml"); |
657 | 694 |
out.println(sb.toString()); |
658 |
conn.close(); |
|
695 |
//conn.close();
|
|
659 | 696 |
} |
660 | 697 |
catch (Exception e) |
661 | 698 |
{ |
... | ... | |
792 | 829 |
private void handleGetDocumentRequest(PrintWriter out, Hashtable params, |
793 | 830 |
HttpServletResponse response) |
794 | 831 |
{ |
795 |
Connection conn = null;
|
|
832 |
//Connection dbConn = null;
|
|
796 | 833 |
try |
797 | 834 |
{ |
798 | 835 |
//try to open a https stream to test if the request server's public key |
... | ... | |
811 | 848 |
} |
812 | 849 |
|
813 | 850 |
String docid = ((String[])(params.get("docid")))[0]; |
814 |
conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
815 |
"handleGetDocumentRequest"); |
|
816 |
DocumentImpl di = new DocumentImpl(conn, docid);
|
|
851 |
/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
|
|
852 |
"handleGetDocumentRequest");*/
|
|
853 |
DocumentImpl di = new DocumentImpl(docid); |
|
817 | 854 |
response.setContentType("text/xml"); |
818 | 855 |
out.print(di.toString()); |
819 |
conn.close(); |
|
856 |
//conn.close();
|
|
820 | 857 |
MetacatReplication.replLog("document " + docid + " sent"); |
821 | 858 |
|
822 | 859 |
} |
... | ... | |
849 | 886 |
private void handleUpdateRequest(PrintWriter out, Hashtable params, |
850 | 887 |
HttpServletResponse response) |
851 | 888 |
{ |
852 |
Connection conn = null; |
|
889 |
DBConnection dbConn = null; |
|
890 |
int serialNumber = -1; |
|
891 |
PreparedStatement pstmt = null; |
|
853 | 892 |
try |
854 | 893 |
{ |
855 | 894 |
//try to open a https stream to test if the request server's public key |
... | ... | |
893 | 932 |
delsql.append("and server_location = 1"); |
894 | 933 |
} |
895 | 934 |
|
896 |
conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
897 |
"handleUpdateRequest"); |
|
898 |
PreparedStatement pstmt = conn.prepareStatement(docsql.toString()); |
|
935 |
/*conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
936 |
"handleUpdateRequest");*/ |
|
937 |
dbConn=DBConnectionPool. |
|
938 |
getDBConnection("MetacatReplication.handleUpdateRequest"); |
|
939 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
940 |
pstmt = dbConn.prepareStatement(docsql.toString()); |
|
899 | 941 |
pstmt.execute(); |
900 | 942 |
ResultSet rs = pstmt.getResultSet(); |
901 | 943 |
boolean tablehasrows = rs.next(); |
... | ... | |
975 | 1017 |
}//while |
976 | 1018 |
}//else |
977 | 1019 |
|
978 |
pstmt = conn.prepareStatement(delsql.toString()); |
|
1020 |
pstmt = dbConn.prepareStatement(delsql.toString()); |
|
1021 |
//usage count should increas 1 |
|
1022 |
dbConn.increaseUsageCount(1); |
|
1023 |
|
|
979 | 1024 |
pstmt.execute(); |
980 | 1025 |
rs = pstmt.getResultSet(); |
981 | 1026 |
tablehasrows = rs.next(); |
... | ... | |
1002 | 1047 |
doclist.append("</updates></replication>"); |
1003 | 1048 |
MetaCatUtil.debugMessage("doclist: " + doclist.toString()); |
1004 | 1049 |
pstmt.close(); |
1005 |
conn.close(); |
|
1050 |
//conn.close();
|
|
1006 | 1051 |
response.setContentType("text/xml"); |
1007 | 1052 |
out.println(doclist.toString()); |
1008 | 1053 |
|
... | ... | |
1015 | 1060 |
response.setContentType("text/xml"); |
1016 | 1061 |
out.println("<error>e.getMessage()</error>"); |
1017 | 1062 |
} |
1063 |
finally |
|
1064 |
{ |
|
1065 |
try |
|
1066 |
{ |
|
1067 |
pstmt.close(); |
|
1068 |
}//try |
|
1069 |
catch (SQLException ee) |
|
1070 |
{ |
|
1071 |
MetaCatUtil.debugMessage("Error in MetacatReplication." + |
|
1072 |
"handleUpdaterequest to close pstmt: "+ee.getMessage(), 30); |
|
1073 |
}//catch |
|
1074 |
finally |
|
1075 |
{ |
|
1076 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
1077 |
}//finally |
|
1078 |
}//finally |
|
1079 |
|
|
1080 |
|
|
1081 |
|
|
1018 | 1082 |
|
1019 | 1083 |
} |
1020 | 1084 |
|
... | ... | |
1041 | 1105 |
HttpServletResponse response, |
1042 | 1106 |
boolean printFlag) |
1043 | 1107 |
{ |
1044 |
Connection conn = null; |
|
1108 |
DBConnection dbConn = null; |
|
1109 |
int serialNumber = -1; |
|
1045 | 1110 |
PreparedStatement pstmt = null; |
1046 | 1111 |
try |
1047 | 1112 |
{ |
1048 |
conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
1049 |
"handleGetCatalogRequest"); |
|
1050 |
pstmt = conn.prepareStatement("select entry_type, " + |
|
1113 |
/*conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
1114 |
"handleGetCatalogRequest");*/ |
|
1115 |
dbConn=DBConnectionPool. |
|
1116 |
getDBConnection("MetacatReplication.handleGetCatalogRequest"); |
|
1117 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
1118 |
pstmt = dbConn.prepareStatement("select entry_type, " + |
|
1051 | 1119 |
"source_doctype, target_doctype, public_id, " + |
1052 | 1120 |
"system_id from xml_catalog"); |
1053 | 1121 |
pstmt.execute(); |
... | ... | |
1067 | 1135 |
tablehasrows = rs.next(); |
1068 | 1136 |
} |
1069 | 1137 |
sb.append("</xml_catalog>"); |
1070 |
conn.close(); |
|
1138 |
//conn.close();
|
|
1071 | 1139 |
if(printFlag) |
1072 | 1140 |
{ |
1073 | 1141 |
response.setContentType("text/xml"); |
... | ... | |
1078 | 1146 |
} |
1079 | 1147 |
catch(Exception e) |
1080 | 1148 |
{ |
1081 |
try |
|
1082 |
{ |
|
1083 |
pstmt.close(); |
|
1084 |
conn.close(); |
|
1085 |
} |
|
1086 |
catch(Exception ee) |
|
1087 |
{} |
|
1149 |
|
|
1088 | 1150 |
System.out.println("error in MetacatReplication.handleGetCatalogRequest:"+ |
1089 | 1151 |
e.getMessage()); |
1090 | 1152 |
e.printStackTrace(System.out); |
1091 | 1153 |
} |
1154 |
finally |
|
1155 |
{ |
|
1156 |
try |
|
1157 |
{ |
|
1158 |
pstmt.close(); |
|
1159 |
}//try |
|
1160 |
catch (SQLException ee) |
|
1161 |
{ |
|
1162 |
MetaCatUtil. |
|
1163 |
debugMessage("Error in MetacatReplication.handleGetCatalogRequest: " |
|
1164 |
+ee.getMessage(), 30); |
|
1165 |
}//catch |
|
1166 |
finally |
|
1167 |
{ |
|
1168 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
1169 |
}//finally |
|
1170 |
}//finally |
|
1171 |
|
|
1092 | 1172 |
return null; |
1093 | 1173 |
} |
1094 | 1174 |
|
... | ... | |
1145 | 1225 |
public static String getServer(int serverCode) |
1146 | 1226 |
{ |
1147 | 1227 |
//System.out.println("serverid: " + serverCode); |
1148 |
Connection conn = null; |
|
1228 |
DBConnection dbConn = null; |
|
1229 |
int serialNumber = -1; |
|
1230 |
PreparedStatement pstmt = null; |
|
1149 | 1231 |
try |
1150 | 1232 |
{ |
1151 |
conn = MetacatReplication.getDBConnection("MetacatReplication." + |
|
1152 |
"getServer"); |
|
1233 |
dbConn=DBConnectionPool. |
|
1234 |
getDBConnection("MetacatReplication.getServer"); |
|
1235 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
1153 | 1236 |
String sql = new String("select server from " + |
1154 | 1237 |
"xml_replication where serverid = " + |
1155 | 1238 |
serverCode); |
1156 |
PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
1239 |
pstmt = dbConn.prepareStatement(sql);
|
|
1157 | 1240 |
//System.out.println("getserver sql: " + sql); |
1158 | 1241 |
pstmt.execute(); |
1159 | 1242 |
ResultSet rs = pstmt.getResultSet(); |
... | ... | |
1163 | 1246 |
//System.out.println("server: " + rs.getString(1)); |
1164 | 1247 |
return rs.getString(1); |
1165 | 1248 |
} |
1166 |
pstmt.close(); |
|
1167 |
conn.close(); |
|
1249 |
|
|
1250 |
//conn.close();
|
|
1168 | 1251 |
} |
1169 | 1252 |
catch(Exception e) |
1170 | 1253 |
{ |
1171 | 1254 |
System.out.println("Error in MetacatReplication.getServer: " + |
1172 | 1255 |
e.getMessage()); |
1173 | 1256 |
} |
1257 |
finally |
|
1258 |
{ |
|
1259 |
try |
|
1260 |
{ |
|
1261 |
pstmt.close(); |
|
1262 |
}//try |
|
1263 |
catch (SQLException ee) |
|
1264 |
{ |
|
1265 |
MetaCatUtil.debugMessage("Error in MetacactReplication.getserver: "+ |
|
1266 |
ee.getMessage(), 30); |
|
1267 |
}//catch |
|
1268 |
finally |
|
1269 |
{ |
|
1270 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
1271 |
}//fianlly |
|
1272 |
}//finally |
|
1273 |
|
|
1274 |
|
|
1275 |
|
|
1174 | 1276 |
return null; |
1175 | 1277 |
//return null if the server does not exist |
1176 | 1278 |
} |
... | ... | |
1183 | 1285 |
*/ |
1184 | 1286 |
public static int getServerCode(String server) throws Exception |
1185 | 1287 |
{ |
1186 |
Connection conn = null; |
|
1288 |
DBConnection dbConn = null; |
|
1289 |
int serialNumber = -1; |
|
1187 | 1290 |
PreparedStatement pstmt = null; |
1188 | 1291 |
int serverCode = 0; |
1189 | 1292 |
|
1190 | 1293 |
try { |
1191 | 1294 |
|
1192 |
conn = util.openDBConnection(); |
|
1193 |
pstmt = conn.prepareStatement("SELECT serverid FROM xml_replication " + |
|
1295 |
//conn = util.openDBConnection(); |
|
1296 |
dbConn=DBConnectionPool. |
|
1297 |
getDBConnection("MetacatReplication.getServerCode"); |
|
1298 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
1299 |
pstmt = dbConn.prepareStatement("SELECT serverid FROM xml_replication " + |
|
1194 | 1300 |
"WHERE server LIKE '" + server + "'"); |
1195 | 1301 |
pstmt.execute(); |
1196 | 1302 |
ResultSet rs = pstmt.getResultSet(); |
... | ... | |
1198 | 1304 |
if ( tablehasrows ) { |
1199 | 1305 |
serverCode = rs.getInt(1); |
1200 | 1306 |
pstmt.close(); |
1201 |
conn.close(); |
|
1307 |
//conn.close();
|
|
1202 | 1308 |
return serverCode; |
1203 | 1309 |
} |
1204 | 1310 |
|
... | ... | |
1206 | 1312 |
throw e; |
1207 | 1313 |
|
1208 | 1314 |
} finally { |
1209 |
try { |
|
1315 |
try |
|
1316 |
{ |
|
1210 | 1317 |
pstmt.close(); |
1211 |
conn.close(); |
|
1212 |
} catch(Exception ee) {} |
|
1213 |
} |
|
1318 |
//conn.close(); |
|
1319 |
}//try |
|
1320 |
catch(Exception ee) |
|
1321 |
{ |
|
1322 |
MetaCatUtil.debugMessage("Error in MetacatReplicatio.getServerCode: " |
|
1323 |
+ee.getMessage(), 30); |
|
1324 |
|
|
1325 |
}//catch |
|
1326 |
finally |
|
1327 |
{ |
|
1328 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
1329 |
}//finally |
|
1330 |
}//finally |
|
1214 | 1331 |
|
1215 | 1332 |
return serverCode; |
1216 | 1333 |
} |
... | ... | |
1270 | 1387 |
*/ |
1271 | 1388 |
public static boolean replToServer(String server) |
1272 | 1389 |
{ |
1273 |
Connection conn = null; |
|
1390 |
DBConnection dbConn = null; |
|
1391 |
int serialNumber = -1; |
|
1274 | 1392 |
PreparedStatement pstmt = null; |
1275 | 1393 |
try |
1276 | 1394 |
{ |
1277 |
conn = MetacatReplication.getDBConnection("MetacatReplication.replToServer"); |
|
1278 |
pstmt = conn.prepareStatement("select replicate from " + |
|
1395 |
dbConn=DBConnectionPool. |
|
1396 |
getDBConnection("MetacatReplication.repltoServer"); |
|
1397 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
1398 |
pstmt = dbConn.prepareStatement("select replicate from " + |
|
1279 | 1399 |
"xml_replication where server like '" + |
1280 | 1400 |
server + "'"); |
1281 | 1401 |
pstmt.execute(); |
... | ... | |
1287 | 1407 |
if(i == 1) |
1288 | 1408 |
{ |
1289 | 1409 |
pstmt.close(); |
1290 |
conn.close(); |
|
1410 |
//conn.close();
|
|
1291 | 1411 |
return true; |
1292 | 1412 |
} |
1293 | 1413 |
else |
1294 | 1414 |
{ |
1295 | 1415 |
pstmt.close(); |
1296 |
conn.close(); |
|
1416 |
//conn.close();
|
|
1297 | 1417 |
return false; |
1298 | 1418 |
} |
1299 | 1419 |
} |
... | ... | |
1308 | 1428 |
try |
1309 | 1429 |
{ |
1310 | 1430 |
pstmt.close(); |
1311 |
conn.close(); |
|
1312 |
} |
|
1431 |
//conn.close();
|
|
1432 |
}//try
|
|
1313 | 1433 |
catch(Exception ee) |
1314 |
{} |
|
1315 |
} |
|
1434 |
{ |
|
1435 |
MetaCatUtil.debugMessage("Error in MetacatReplication.replToServer: " |
|
1436 |
+ee.getMessage(), 30); |
|
1437 |
}//catch |
|
1438 |
finally |
|
1439 |
{ |
|
1440 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
1441 |
}//finally |
|
1442 |
}//finally |
|
1316 | 1443 |
return false; |
1317 | 1444 |
//the default if this server does not exist is to not replicate to it. |
1318 | 1445 |
} |
1319 | 1446 |
|
1320 |
/** |
|
1321 |
* A method for redundantly trying to connect. this method will attempt to |
|
1322 |
* connect to the DB 3 times before failing. |
|
1323 |
* @param methodname the methodname from which this method is called. |
|
1324 |
*/ |
|
1325 |
public static Connection getDBConnection(String methodname) throws Exception |
|
1326 |
{ |
|
1327 |
Connection conn = null; |
|
1328 |
try |
|
1329 |
{ |
|
1330 |
try |
|
1331 |
{ //this connection is prone to error for some reason so we |
|
1332 |
//try to connect it three times before quiting. |
|
1333 |
conn = util.openDBConnection(); |
|
1334 |
return conn; |
|
1335 |
} |
|
1336 |
catch(SQLException sqle) |
|
1337 |
{ |
|
1338 |
try |
|
1339 |
{ |
|
1340 |
conn = util.openDBConnection(); |
|
1341 |
return conn; |
|
1342 |
} |
|
1343 |
catch(SQLException sqlee) |
|
1344 |
{ |
|
1345 |
try |
|
1346 |
{ |
|
1347 |
conn = util.openDBConnection(); |
|
1348 |
return conn; |
|
1349 |
} |
|
1350 |
catch(SQLException sqleee) |
|
1351 |
{ |
|
1352 |
System.out.println("error getting db connection in " + |
|
1353 |
methodname + ": " + |
|
1354 |
sqleee.getMessage()); |
|
1355 |
return conn; |
|
1356 |
} |
|
1357 |
} |
|
1358 |
} |
|
1359 |
} |
|
1360 |
catch(Exception e) |
|
1361 |
{ |
|
1362 |
throw new java.lang.Exception("error in " + methodname + ": " + |
|
1363 |
e.getMessage()); |
|
1364 |
} |
|
1365 |
} |
|
1447 |
|
|
1366 | 1448 |
} |
src/edu/ucsb/nceas/metacat/DBQuery.java | ||
---|---|---|
59 | 59 |
static final int WRITE = 2; |
60 | 60 |
static final int READ = 4; |
61 | 61 |
|
62 |
private Connection conn = null; |
|
62 |
//private Connection conn = null;
|
|
63 | 63 |
private String parserName = null; |
64 | 64 |
private MetaCatUtil util = new MetaCatUtil(); |
65 | 65 |
/** |
... | ... | |
97 | 97 |
|
98 | 98 |
// Open a connection to the database |
99 | 99 |
MetaCatUtil util = new MetaCatUtil(); |
100 |
Connection dbconn = util.openDBConnection(); |
|
100 |
//Connection dbconn = util.openDBConnection();
|
|
101 | 101 |
|
102 | 102 |
double connTime = System.currentTimeMillis(); |
103 | 103 |
|
104 | 104 |
// Execute the query |
105 |
DBQuery queryobj = new DBQuery(dbconn, util.getOption("saxparser"));
|
|
105 |
DBQuery queryobj = new DBQuery(util.getOption("saxparser")); |
|
106 | 106 |
FileReader xml = new FileReader(new File(xmlfile)); |
107 | 107 |
Hashtable nodelist = null; |
108 | 108 |
nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex); |
... | ... | |
170 | 170 |
* @param parserName the fully qualified name of a Java class implementing |
171 | 171 |
* the org.xml.sax.XMLReader interface |
172 | 172 |
*/ |
173 |
public DBQuery( Connection conn, String parserName )
|
|
173 |
public DBQuery(String parserName ) |
|
174 | 174 |
throws IOException, |
175 | 175 |
SQLException, |
176 | 176 |
ClassNotFoundException { |
177 |
this.conn = conn; |
|
177 |
//this.conn = conn;
|
|
178 | 178 |
this.parserName = parserName; |
179 | 179 |
} |
180 | 180 |
|
... | ... | |
211 | 211 |
String fieldname = null; |
212 | 212 |
String fielddata = null; |
213 | 213 |
String relation = null; |
214 |
Connection dbconn = null; |
|
215 |
Connection dbconn2 = null; |
|
214 |
//Connection dbconn = null;
|
|
215 |
//Connection dbconn2 = null;
|
|
216 | 216 |
int rev = 0; |
217 |
StringBuffer document = null; |
|
217 |
StringBuffer document = null; |
|
218 |
DBConnection dbconn = null; |
|
219 |
int serialNumber = -1; |
|
218 | 220 |
|
219 | 221 |
try { |
220 |
if (conn == null || conn.isClosed()) { |
|
222 |
/*if (conn == null || conn.isClosed()) {
|
|
221 | 223 |
dbconn = util.openDBConnection(); |
222 | 224 |
} else { |
223 | 225 |
dbconn = conn; |
224 |
} |
|
226 |
}*/ |
|
227 |
|
|
228 |
dbconn=DBConnectionPool.getDBConnection("DBQuery.findDocuments"); |
|
229 |
serialNumber=dbconn.getCheckOutSerialNumber(); |
|
225 | 230 |
// problem with ODBC driver multi-threading |
226 | 231 |
// dbconn2 = util.openDBConnection(); // for use by AccessControlList |
227 | 232 |
|
... | ... | |
239 | 244 |
while (tableHasRows) |
240 | 245 |
{ |
241 | 246 |
docid = rs.getString(1).trim(); |
242 |
if ( !hasPermission(dbconn, user, groups, docid) ) {
|
|
247 |
if ( !hasPermission(user, groups, docid) ) { |
|
243 | 248 |
// Advance to the next record in the cursor |
244 | 249 |
tableHasRows = rs.next(); |
245 | 250 |
continue; |
... | ... | |
279 | 284 |
|
280 | 285 |
PreparedStatement npstmt = dbconn. |
281 | 286 |
prepareStatement(btBuf.toString()); |
287 |
//should incease usage count |
|
288 |
dbconn.increaseUsageCount(1); |
|
282 | 289 |
npstmt.execute(); |
283 | 290 |
ResultSet btrs = npstmt.getResultSet(); |
284 | 291 |
boolean hasBtRows = btrs.next(); |
... | ... | |
293 | 300 |
// xmldoc = new DocumentImpl(dbconn, packageDocid); |
294 | 301 |
// thus use the following to get the doc info only |
295 | 302 |
// xmldoc = new DocumentImpl(dbconn); |
296 |
xmldoc = new DocumentImpl(dbconn, packageDocid, false);
|
|
303 |
xmldoc = new DocumentImpl(packageDocid, false); |
|
297 | 304 |
if (xmldoc == null) { |
298 | 305 |
util.debugMessage("Document was null for: "+packageDocid, 50); |
299 | 306 |
} |
... | ... | |
388 | 395 |
//pstmt.close(); |
389 | 396 |
pstmt = dbconn.prepareStatement(qspec.printExtendedSQL( |
390 | 397 |
doclist.toString())); |
398 |
//increase dbconnection usage count |
|
399 |
dbconn.increaseUsageCount(1); |
|
391 | 400 |
pstmt.execute(); |
392 | 401 |
rs = pstmt.getResultSet(); |
393 | 402 |
tableHasRows = rs.next(); |
394 | 403 |
while(tableHasRows) |
395 | 404 |
{ |
396 | 405 |
docid = rs.getString(1).trim(); |
397 |
if ( !hasPermission(dbconn, user, groups, docid) ) {
|
|
406 |
if ( !hasPermission(user, groups, docid) ) { |
|
398 | 407 |
// Advance to the next record in the cursor |
399 | 408 |
tableHasRows = rs.next(); |
400 | 409 |
continue; |
... | ... | |
485 | 494 |
ee.getMessage()); |
486 | 495 |
ee.printStackTrace(System.err); |
487 | 496 |
} |
488 |
finally { |
|
497 |
finally |
|
498 |
{ |
|
489 | 499 |
try |
490 | 500 |
{ |
491 |
dbconn.close(); |
|
492 |
// dbconn2.close(); |
|
493 |
} |
|
494 |
catch(SQLException sqle) |
|
501 |
pstmt.close(); |
|
502 |
}//try |
|
503 |
catch (SQLException sqlE) |
|
495 | 504 |
{ |
496 |
System.out.println("error closing conn in DBQuery.findDocuments"); |
|
497 |
} |
|
498 |
} |
|
505 |
MetaCatUtil.debugMessage("Error in DBQuery.findDocuments: " |
|
506 |
+sqlE.getMessage(), 30); |
|
507 |
}//catch |
|
508 |
finally |
|
509 |
{ |
|
510 |
DBConnectionPool.returnDBConnection(dbconn, serialNumber); |
|
511 |
}//finally |
|
512 |
}//finally |
|
499 | 513 |
//System.out.println("docListResult: "); |
500 | 514 |
//System.out.println(docListResult.toString()); |
501 | 515 |
return docListResult; |
... | ... | |
507 | 521 |
* in the order in which they appearred in the document. |
508 | 522 |
* @param nodename the name or path of the particular node. |
509 | 523 |
* @param docid the docid of the document you want the node from. |
510 |
* @param conn a database connection-this allows this method to be static |
|
511 | 524 |
*/ |
512 |
public static Object[] getNodeContent(String nodename, String docid, |
|
513 |
Connection conn) |
|
525 |
public static Object[] getNodeContent(String nodename, String docid) |
|
514 | 526 |
{ |
527 |
DBConnection dbconn = null; |
|
528 |
int serialNumber = -1; |
|
515 | 529 |
StringBuffer query = new StringBuffer(); |
516 | 530 |
Vector result = new Vector(); |
517 | 531 |
PreparedStatement pstmt = null; |
... | ... | |
521 | 535 |
query.append("' and docid like '").append(docid).append("')"); |
522 | 536 |
try |
523 | 537 |
{ |
524 |
pstmt = conn.prepareStatement(query.toString()); |
|
538 |
dbconn=DBConnectionPool.getDBConnection("DBQuery.getNodeContent"); |
|
539 |
serialNumber=dbconn.getCheckOutSerialNumber(); |
|
540 |
pstmt = dbconn.prepareStatement(query.toString()); |
|
525 | 541 |
|
526 | 542 |
// Execute the SQL query using the JDBC connection |
527 | 543 |
pstmt.execute(); |
... | ... | |
542 | 558 |
{ |
543 | 559 |
pstmt.close(); |
544 | 560 |
} |
545 |
catch(SQLException sqle) {} |
|
561 |
catch(SQLException sqle) |
|
562 |
{} |
|
563 |
finally |
|
564 |
{ |
|
565 |
DBConnectionPool.returnDBConnection(dbconn, serialNumber); |
|
566 |
} |
|
567 |
|
|
546 | 568 |
} |
547 | 569 |
return result.toArray(); |
548 | 570 |
} |
... | ... | |
779 | 801 |
* Check for "READ" permission on @docid for @user and/or @group |
780 | 802 |
* from DB connection |
781 | 803 |
*/ |
782 |
private boolean hasPermission ( Connection conn, String user,
|
|
804 |
private boolean hasPermission (String user, |
|
783 | 805 |
String[] groups, String docid ) |
784 | 806 |
throws SQLException, Exception |
785 | 807 |
{ |
786 | 808 |
// Check for READ permission on @docid for @user and/or @groups |
787 |
AccessControlList aclobj = new AccessControlList(conn); |
|
788 |
return aclobj.hasPermission("READ", user, groups, docid); |
|
809 |
//AccessControlList aclobj = new AccessControlList(); |
|
810 |
//return aclobj.hasPermission("READ", user, groups, docid); |
|
811 |
return AccessControlList.hasPermission("READ", user, groups, docid); |
|
789 | 812 |
} |
790 | 813 |
|
791 | 814 |
/** |
... | ... | |
794 | 817 |
*/ |
795 | 818 |
private Vector getCurrentDocidListForDataPackage(String dataPackageDocid) |
796 | 819 |
{ |
820 |
DBConnection dbConn = null; |
|
821 |
int serialNumber = -1; |
|
797 | 822 |
Vector docIdList=new Vector();//return value |
798 |
PreparedStatement pStmt; |
|
823 |
PreparedStatement pStmt = null;
|
|
799 | 824 |
ResultSet rs=null; |
800 | 825 |
String docIdInSubjectField=null; |
801 | 826 |
String docIdInObjectField=null; |
... | ... | |
803 | 828 |
String query="SELECT subject, object from xml_relation where docId = ?"; |
804 | 829 |
try |
805 | 830 |
{ |
806 |
pStmt=conn.prepareStatement(query); |
|
831 |
dbConn=DBConnectionPool. |
|
832 |
getDBConnection("DBQuery.getCurrentDocidListForDataPackage"); |
|
833 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
834 |
pStmt=dbConn.prepareStatement(query); |
|
807 | 835 |
//bind the value to query |
808 | 836 |
pStmt.setString(1, dataPackageDocid); |
809 | 837 |
|
... | ... | |
840 | 868 |
util.debugMessage("Error in getDocidListForDataPackage: " |
841 | 869 |
+e.getMessage(), 30); |
842 | 870 |
}//catch |
871 |
finally |
|
872 |
{ |
|
873 |
try |
|
874 |
{ |
|
875 |
pStmt.close(); |
|
876 |
}//try |
|
877 |
catch (SQLException ee) |
|
878 |
{ |
|
879 |
util.debugMessage("Error in getDocidListForDataPackage: " |
|
880 |
+ee.getMessage(), 30); |
|
881 |
}//catch |
|
882 |
finally |
|
883 |
{ |
|
884 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
885 |
}//fianlly |
|
886 |
}//finally |
|
843 | 887 |
return docIdList; |
844 | 888 |
}//getCurrentDocidListForDataPackadge() |
845 | 889 |
|
... | ... | |
854 | 898 |
Vector docIdList=new Vector();//return value |
855 | 899 |
Vector tripleList=null; |
856 | 900 |
String xml=null; |
857 |
Connection dbConn=null; |
|
901 |
|
|
902 |
//initial a documentImpl object |
|
903 |
DocumentImpl packageDocument = |
|
904 |
new DocumentImpl(dataPackageDocid); |
|
905 |
//transfer to documentImpl object to string |
|
906 |
xml=packageDocument.toString(); |
|
858 | 907 |
|
859 |
if (conn == null || conn.isClosed()) |
|
860 |
{ |
|
861 |
dbConn = util.openDBConnection(); |
|
862 |
} |
|
863 |
else |
|
864 |
{ |
|
865 |
dbConn=conn; |
|
866 |
} |
|
867 |
//initial a documentImpl object |
|
868 |
DocumentImpl packageDocument = new DocumentImpl(dbConn, dataPackageDocid); |
|
869 |
//transfer to documentImpl object to string |
|
870 |
xml=packageDocument.toString(); |
|
871 |
|
|
872 |
//create a tripcollection object |
|
873 |
TripleCollection tripleForPackage = new |
|
908 |
//create a tripcollection object |
|
909 |
TripleCollection tripleForPackage = new |
|
874 | 910 |
TripleCollection(new StringReader(xml)); |
875 |
//get the vetor of triples |
|
876 |
tripleList=tripleForPackage.getCollection(); |
|
911 |
//get the vetor of triples
|
|
912 |
tripleList=tripleForPackage.getCollection();
|
|
877 | 913 |
|
878 |
for (int i= 0; i<tripleList.size(); i++) |
|
879 |
{ |
|
880 |
//put subject docid into docIdlist without duplicate |
|
881 |
if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getSubject())) |
|
914 |
for (int i= 0; i<tripleList.size(); i++) |
|
882 | 915 |
{ |
883 |
//put subject docid into docIdlist |
|
884 |
docIdList.add(((Triple)tripleList.get(i)).getSubject()); |
|
885 |
} |
|
886 |
//put object docid into docIdlist without duplicate |
|
887 |
if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getObject())) |
|
888 |
{ |
|
889 |
docIdList.add(((Triple)(tripleList.get(i))).getObject()); |
|
890 |
} |
|
891 |
}//for |
|
916 |
//put subject docid into docIdlist without duplicate |
|
917 |
if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getSubject())) |
|
918 |
{ |
|
919 |
//put subject docid into docIdlist |
|
920 |
docIdList.add(((Triple)tripleList.get(i)).getSubject()); |
|
921 |
} |
|
922 |
//put object docid into docIdlist without duplicate |
|
923 |
if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getObject())) |
|
924 |
{ |
|
925 |
docIdList.add(((Triple)(tripleList.get(i))).getObject()); |
|
926 |
} |
|
927 |
}//for |
|
928 |
|
|
892 | 929 |
|
930 |
|
|
893 | 931 |
return docIdList; |
894 | 932 |
}//getDocidListForPackageInXMLRevisions() |
895 | 933 |
|
... | ... | |
904 | 942 |
private boolean isDataPackageId(String docId) |
905 | 943 |
{ |
906 | 944 |
boolean result=false; |
907 |
PreparedStatement pStmt; |
|
945 |
PreparedStatement pStmt = null;
|
|
908 | 946 |
ResultSet rs=null; |
909 | 947 |
String query="SELECT docId from xml_relation where docId = ?"; |
948 |
DBConnection dbConn = null; |
|
949 |
int serialNumber = -1; |
|
910 | 950 |
try |
911 | 951 |
{ |
912 |
pStmt=conn.prepareStatement(query); |
|
952 |
dbConn=DBConnectionPool. |
|
953 |
getDBConnection("DBQuery.isDataPackageId"); |
|
954 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
955 |
pStmt=dbConn.prepareStatement(query); |
|
913 | 956 |
//bind the value to query |
914 | 957 |
pStmt.setString(1, docId); |
915 | 958 |
//execute the query |
... | ... | |
924 | 967 |
}//try |
925 | 968 |
catch (SQLException e) |
926 | 969 |
{ |
927 |
util.debugMessage("Error in getDocidListForDataPackadge: "
|
|
970 |
util.debugMessage("Error in isDataPackageId: "
|
|
928 | 971 |
+e.getMessage(), 30); |
929 | 972 |
} |
973 |
finally |
|
974 |
{ |
|
975 |
try |
|
976 |
{ |
|
977 |
pStmt.close(); |
|
978 |
}//try |
|
979 |
catch (SQLException ee) |
|
980 |
{ |
|
981 |
MetaCatUtil.debugMessage("Error in isDataPackageId: " |
|
982 |
+ ee.getMessage(), 30); |
|
983 |
}//catch |
|
984 |
finally |
|
985 |
{ |
|
986 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
987 |
}//finally |
|
988 |
}//finally |
|
930 | 989 |
return result; |
931 | 990 |
}//isDataPackageId() |
932 | 991 |
|
... | ... | |
937 | 996 |
* @param user, the name of user |
938 | 997 |
* @param groups, the user's group |
939 | 998 |
*/ |
940 |
private boolean hasPermissionToExportPackage(Connection conn, String docId,
|
|
999 |
private boolean hasPermissionToExportPackage(String docId, |
|
941 | 1000 |
String user, String[] groups) |
942 | 1001 |
throws Exception |
943 | 1002 |
{ |
944 |
DocumentImpl doc=new DocumentImpl(conn,docId); |
|
945 |
return doc.hasReadPermission(conn, user, groups,docId);
|
|
1003 |
//DocumentImpl doc=new DocumentImpl(conn,docId);
|
|
1004 |
return DocumentImpl.hasReadPermission(user, groups,docId);
|
|
946 | 1005 |
} |
947 | 1006 |
|
948 | 1007 |
/** |
... | ... | |
953 | 1012 |
private int getCurrentRevFromXMLDoumentsTable(String docId) |
954 | 1013 |
{ |
955 | 1014 |
int rev=-5; |
956 |
PreparedStatement pStmt; |
|
1015 |
PreparedStatement pStmt = null;
|
|
957 | 1016 |
ResultSet rs=null; |
958 | 1017 |
String query="SELECT rev from xml_documents where docId = ?"; |
1018 |
DBConnection dbConn=null; |
|
1019 |
int serialNumber = -1; |
|
959 | 1020 |
try |
960 | 1021 |
{ |
961 |
pStmt=conn.prepareStatement(query); |
|
1022 |
dbConn=DBConnectionPool. |
|
1023 |
getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable"); |
|
1024 |
serialNumber=dbConn.getCheckOutSerialNumber(); |
|
1025 |
pStmt=dbConn.prepareStatement(query); |
|
962 | 1026 |
//bind the value to query |
963 | 1027 |
pStmt.setString(1, docId); |
964 | 1028 |
//execute the query |
... | ... | |
977 | 1041 |
}//try |
978 | 1042 |
catch (SQLException e) |
979 | 1043 |
{ |
980 |
util.debugMessage("Error in getDocidListForDataPackadge: "
|
|
1044 |
util.debugMessage("Error in getCurrentRevFromXMLDoumentsTable: "
|
|
981 | 1045 |
+e.getMessage(), 30); |
982 |
} |
|
1046 |
}//catch |
|
1047 |
finally |
|
1048 |
{ |
|
1049 |
try |
|
1050 |
{ |
|
1051 |
pStmt.close(); |
|
1052 |
}//try |
|
1053 |
catch (SQLException ee) |
|
1054 |
{ |
|
1055 |
MetaCatUtil.debugMessage("Error in getCurrentRevFromXMLDoumentsTable: " |
|
1056 |
+ee.getMessage(), 30); |
|
1057 |
}//catch |
|
1058 |
finally |
|
1059 |
{ |
|
1060 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
1061 |
}//finally |
|
1062 |
}//finally |
|
983 | 1063 |
return rev; |
984 | 1064 |
}//getCurrentRevFromXMLDoumentsTable |
985 | 1065 |
|
... | ... | |
1018 | 1098 |
private Vector getCurrentAllDocumentImpl( Vector docIdList) |
1019 | 1099 |
throws McdbException,Exception |
Also available in: Unified diff
Merge DBConnection branch to head.