Revision 2381
Added by Duane Costa almost 20 years ago
src/edu/ucsb/nceas/metacat/harvesterClient/Harvester.java | ||
---|---|---|
172 | 172 |
Thread.sleep(delay * oneHour); |
173 | 173 |
} |
174 | 174 |
catch (InterruptedException e) { |
175 |
System.err.println("InterruptedException: " + e.getMessage());
|
|
176 |
System.exit(1);
|
|
175 |
System.err.println("InterruptedException: " + e.getMessage()); |
|
176 |
System.exit(1); |
|
177 | 177 |
} |
178 | 178 |
} |
179 | 179 |
|
... | ... | |
365 | 365 |
*/ |
366 | 366 |
public Connection getConnection() { |
367 | 367 |
String dbDriver = ""; |
368 |
String defaultDB;
|
|
368 |
String defaultDB;
|
|
369 | 369 |
String password; |
370 | 370 |
String user; |
371 | 371 |
SQLWarning warn; |
... | ... | |
452 | 452 |
private int getMaxValue(String tableName, String fieldName) { |
453 | 453 |
int maxValue = 0; |
454 | 454 |
int fieldValue; |
455 |
String query = "SELECT " + fieldName + " FROM " + tableName;
|
|
456 |
Statement stmt;
|
|
455 |
String query = "SELECT " + fieldName + " FROM " + tableName;
|
|
456 |
Statement stmt;
|
|
457 | 457 |
|
458 |
try {
|
|
459 |
stmt = conn.createStatement();
|
|
460 |
ResultSet rs = stmt.executeQuery(query);
|
|
458 |
try { |
|
459 |
stmt = conn.createStatement();
|
|
460 |
ResultSet rs = stmt.executeQuery(query);
|
|
461 | 461 |
|
462 |
while (rs.next()) {
|
|
463 |
fieldValue = rs.getInt(fieldName);
|
|
462 |
while (rs.next()) {
|
|
463 |
fieldValue = rs.getInt(fieldName);
|
|
464 | 464 |
maxValue = Math.max(maxValue, fieldValue); |
465 |
}
|
|
466 |
|
|
467 |
stmt.close();
|
|
468 |
}
|
|
465 |
}
|
|
466 |
|
|
467 |
stmt.close();
|
|
468 |
}
|
|
469 | 469 |
catch(SQLException ex) { |
470 |
System.out.println("SQLException: " + ex.getMessage());
|
|
471 |
}
|
|
470 |
System.out.println("SQLException: " + ex.getMessage());
|
|
471 |
}
|
|
472 | 472 |
|
473 | 473 |
return maxValue; |
474 | 474 |
} |
... | ... | |
484 | 484 |
private int getMinValue(String tableName, String fieldName) { |
485 | 485 |
int minValue = 0; |
486 | 486 |
int fieldValue; |
487 |
String query = "SELECT " + fieldName + " FROM " + tableName;
|
|
488 |
Statement stmt;
|
|
487 |
String query = "SELECT " + fieldName + " FROM " + tableName;
|
|
488 |
Statement stmt;
|
|
489 | 489 |
|
490 |
try {
|
|
491 |
stmt = conn.createStatement();
|
|
492 |
ResultSet rs = stmt.executeQuery(query);
|
|
490 |
try {
|
|
491 |
stmt = conn.createStatement();
|
|
492 |
ResultSet rs = stmt.executeQuery(query);
|
|
493 | 493 |
|
494 |
while (rs.next()) {
|
|
495 |
fieldValue = rs.getInt(fieldName);
|
|
494 |
while (rs.next()) {
|
|
495 |
fieldValue = rs.getInt(fieldName);
|
|
496 | 496 |
|
497 | 497 |
if (minValue == 0) { |
498 | 498 |
minValue = fieldValue; |
... | ... | |
500 | 500 |
else { |
501 | 501 |
minValue = Math.min(minValue, fieldValue); |
502 | 502 |
} |
503 |
}
|
|
504 |
|
|
505 |
stmt.close();
|
|
506 |
}
|
|
503 |
}
|
|
504 |
|
|
505 |
stmt.close();
|
|
506 |
}
|
|
507 | 507 |
catch(SQLException ex) { |
508 |
System.out.println("SQLException: " + ex.getMessage());
|
|
509 |
}
|
|
510 |
|
|
508 |
System.out.println("SQLException: " + ex.getMessage());
|
|
509 |
}
|
|
510 |
|
|
511 | 511 |
return minValue; |
512 | 512 |
} |
513 | 513 |
|
... | ... | |
630 | 630 |
* @param siteScheduleID the primary key in the HARVEST_SITE_SCHEDULE table |
631 | 631 |
*/ |
632 | 632 |
void printHarvestSiteSchedule(PrintStream out, int siteScheduleID) { |
633 |
HarvestSiteSchedule harvestSiteSchedule;
|
|
633 |
HarvestSiteSchedule harvestSiteSchedule; |
|
634 | 634 |
|
635 | 635 |
for (int i = 0; i < harvestSiteScheduleList.size(); i++) { |
636 | 636 |
harvestSiteSchedule = (HarvestSiteSchedule)harvestSiteScheduleList.get(i); |
... | ... | |
642 | 642 |
|
643 | 643 |
|
644 | 644 |
/** |
645 |
* Prunes old records from the HARVEST_DETAIL_LOG table. Records are |
|
646 |
* removed if the HARVEST_LOG_ID foreign key is less than the lowest |
|
647 |
* HARVEST_LOG_ID primary key in the HARVEST_LOG table. |
|
648 |
*/ |
|
649 |
private void pruneHarvestDetailLog() { |
|
650 |
String deleteString; |
|
651 |
int minHarvestLogID; |
|
652 |
int recordsDeleted; |
|
653 |
Statement stmt; |
|
654 |
|
|
655 |
minHarvestLogID = getMinValue("HARVEST_LOG", "HARVEST_LOG_ID"); |
|
656 |
deleteString = "DELETE FROM HARVEST_DETAIL_LOG WHERE HARVEST_LOG_ID < " + |
|
657 |
minHarvestLogID; |
|
658 |
|
|
659 |
try { |
|
660 |
System.out.print("Pruning log entries from HARVEST_DETAIL_LOG: "); |
|
661 |
System.out.println(deleteString); |
|
662 |
stmt = conn.createStatement(); |
|
663 |
recordsDeleted = stmt.executeUpdate(deleteString); |
|
664 |
System.out.println(recordsDeleted + " records deleted"); |
|
665 |
stmt.close(); |
|
666 |
} |
|
667 |
catch(SQLException e) { |
|
668 |
System.out.println("SQLException: " + e.getMessage()); |
|
669 |
} |
|
670 |
} |
|
671 |
|
|
672 |
|
|
673 |
/** |
|
674 | 645 |
* Prunes old records from the HARVEST_LOG table. Records are removed if |
675 | 646 |
* their HARVEST_DATE is older than a given number of days, as stored in the |
676 |
* logPeriod object field. |
|
647 |
* logPeriod object field. First deletes records from the HARVEST_DETAIL_LOG |
|
648 |
* table that reference the to-be-pruned entries in the HARVEST_LOG table. |
|
677 | 649 |
*/ |
678 | 650 |
private void pruneHarvestLog() { |
679 | 651 |
long currentTime = harvestStartTime.getTime(); // time in milliseconds |
680 | 652 |
Date dateLastLog; // Prune everything prior to this date |
681 |
String deleteString; |
|
653 |
String deleteString; |
|
654 |
String deleteStringDetailLog; |
|
682 | 655 |
long delta; |
683 | 656 |
final long millisecondsPerDay = (1000 * 60 * 60 * 24); |
684 | 657 |
int recordsDeleted; |
658 |
int recordsDeletedDetail = 0; |
|
685 | 659 |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy"); |
686 | 660 |
String dateString; |
687 |
Statement stmt; |
|
661 |
ResultSet rs; |
|
662 |
String selectString; |
|
663 |
Statement stmt; |
|
688 | 664 |
long timeLastLog = 0; |
689 |
|
|
665 |
SQLWarning warn; |
|
666 |
|
|
690 | 667 |
delta = logPeriod * millisecondsPerDay; |
691 | 668 |
deleteString = "DELETE FROM HARVEST_LOG WHERE HARVEST_DATE < "; |
669 |
selectString="SELECT HARVEST_LOG_ID FROM HARVEST_LOG WHERE HARVEST_DATE < "; |
|
670 |
deleteStringDetailLog = |
|
671 |
"DELETE FROM HARVEST_DETAIL_LOG WHERE HARVEST_LOG_ID = "; |
|
692 | 672 |
timeLastLog = currentTime - delta; |
693 | 673 |
dateLastLog = new Date(timeLastLog); |
694 | 674 |
dateString = "'" + simpleDateFormat.format(dateLastLog) + "'"; |
695 | 675 |
deleteString += dateString; |
676 |
selectString += dateString; |
|
696 | 677 |
|
697 |
try { |
|
698 |
System.out.print("Pruning log entries from HARVEST_LOG: "); |
|
699 |
System.out.println(deleteString); |
|
700 |
stmt = conn.createStatement(); |
|
701 |
recordsDeleted = stmt.executeUpdate(deleteString); |
|
702 |
System.out.println(recordsDeleted + " records deleted"); |
|
703 |
stmt.close(); |
|
704 |
} |
|
678 |
try { |
|
679 |
System.out.println( |
|
680 |
"Pruning log entries from HARVEST_DETAIL_LOG and HARVEST_LOG:"); |
|
681 |
|
|
682 |
/* Get the list of entries that need to be pruned from the HARVEST_LOG |
|
683 |
* table. |
|
684 |
*/ |
|
685 |
stmt = conn.createStatement(); |
|
686 |
rs = stmt.executeQuery(selectString); |
|
687 |
warn = rs.getWarnings(); |
|
688 |
|
|
689 |
if (warn != null) { |
|
690 |
System.out.println("\n---Warning---\n"); |
|
691 |
|
|
692 |
while (warn != null) { |
|
693 |
System.out.println("Message: " + warn.getMessage()); |
|
694 |
System.out.println("SQLState: " + warn.getSQLState()); |
|
695 |
System.out.print("Vendor error code: "); |
|
696 |
System.out.println(warn.getErrorCode()); |
|
697 |
System.out.println(""); |
|
698 |
warn = warn.getNextWarning(); |
|
699 |
} |
|
700 |
} |
|
701 |
|
|
702 |
/* Delete any entries from the HARVEST_DETAIL_LOG which reference |
|
703 |
* HARVEST_LOG_IDs that are about to be pruned. HARVEST_DETAIL_LOG must |
|
704 |
* be pruned first because its records have a child relationship to those |
|
705 |
* in HARVEST_LOG. |
|
706 |
*/ |
|
707 |
while (rs.next()) { |
|
708 |
harvestLogID = rs.getInt("HARVEST_LOG_ID"); |
|
709 |
stmt = conn.createStatement(); |
|
710 |
recordsDeleted = stmt.executeUpdate(deleteStringDetailLog + |
|
711 |
harvestLogID); |
|
712 |
recordsDeletedDetail += recordsDeleted; |
|
713 |
stmt.close(); |
|
714 |
} |
|
715 |
|
|
716 |
/* Now prune entries from the HARVEST_LOG table using a single update. |
|
717 |
*/ |
|
718 |
stmt = conn.createStatement(); |
|
719 |
recordsDeleted = stmt.executeUpdate(deleteString); |
|
720 |
stmt.close(); |
|
721 |
|
|
722 |
System.out.println(" " + recordsDeletedDetail + |
|
723 |
" records deleted from HARVEST_DETAIL_LOG"); |
|
724 |
System.out.println(" " + recordsDeleted + |
|
725 |
" records deleted from HARVEST_LOG"); |
|
726 |
} |
|
705 | 727 |
catch (SQLException e) { |
706 |
System.out.println("SQLException: " + e.getMessage());
|
|
707 |
}
|
|
728 |
System.out.println("SQLException: " + e.getMessage());
|
|
729 |
}
|
|
708 | 730 |
} |
709 | 731 |
|
710 | 732 |
|
... | ... | |
886 | 908 |
System.out.println("Shutting Down Harvester"); |
887 | 909 |
addLogEntry(0, "Shutting Down Harvester", "HarvesterShutdown", 0, null, ""); |
888 | 910 |
pruneHarvestLog(); |
889 |
pruneHarvestDetailLog(); |
|
890 | 911 |
closeConnection(); |
891 | 912 |
// Print log to standard output and then email the Harvester administrator |
892 | 913 |
printHarvestLog(System.out, maxCodeLevel, siteScheduleID); |
Also available in: Unified diff
Re-implement logic to prune old log entries from the HARVEST_LOG and HARVEST_DETAIL_LOG tables. The old logic caused integrity constraint violations in the database because it tried to delete parent records from HARVEST_LOG prior to deleting child records from HARVEST_DETAIL_LOG.
Also formatting improvements.