annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 406:10aa92f2eca0

Add a account column to the users table artifacts/trunk@5242 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Thu, 23 Aug 2012 13:33:27 +0000
parents f367be55dd35
children
rev   line source
100
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
1 /*
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
2 * Copyright (c) 2010 by Intevation GmbH
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
3 *
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
4 * This program is free software under the LGPL (>=v2.1)
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
5 * Read the file LGPL.txt coming with the software for details
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
7 */
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
8
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 package de.intevation.artifactdatabase;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
207
b2115f484edb Move Config and XMLUtils totally to commons package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 100
diff changeset
11 import de.intevation.artifacts.common.utils.Config;
301
b0a949d3fe09 Moved StringUtils to common package. Added some Override annotations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 241
diff changeset
12 import de.intevation.artifacts.common.utils.StringUtils;
207
b2115f484edb Move Config and XMLUtils totally to commons package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 100
diff changeset
13
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import de.intevation.artifacts.Artifact;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
16 import de.intevation.artifactdatabase.db.SQL;
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
17 import de.intevation.artifactdatabase.db.DBConnection;
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
18
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import java.sql.Connection;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 import java.sql.PreparedStatement;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 import java.sql.ResultSet;
93
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
22 import java.sql.SQLException;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
23
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
24 import java.util.ArrayList;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
25 import java.util.List;
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
26 import java.util.Set;
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
27 import java.util.Collections;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 import javax.sql.DataSource;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 import org.apache.log4j.Logger;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 /**
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
34 * The database cleaner runs in background. It sleep for a configurable
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
35 * while and when it wakes up it removes outdated artifacts from the
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
36 * database. Outdated means that the the last access to the artifact
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
37 * is longer aga then the time to live of this artifact.<br>
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
38 * Before the artifact is finally removed from the system it is
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
39 * revived one last time an the #endOfLife() method of the artifact
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
40 * is called.<br>
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
41 * The artifact implementations may e.g. use this to remove some extrenal
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
42 * resources form the system.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
43 *
80
8447467cef86 Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 48
diff changeset
44 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 */
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 public class DatabaseCleaner
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 extends Thread
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 {
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
49 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
50 * Implementors of this interface are able to create a
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
51 * living artifact from a given byte array.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
52 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
53 public interface ArtifactReviver {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
54
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
55 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
56 * Called to revive an artifact from a given byte array.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
57 * @param factoryName The name of the factory which
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
58 * created this artifact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
59 * @param bytes The bytes of the serialized artifact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
60 * @return The revived artfiact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
61 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
62 Artifact reviveArtifact(String factoryName, byte [] bytes);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
63
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
64 void killedArtifacts(List<String> identifiers);
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
65 void killedCollections(List<String> identifiers);
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
66
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
67 } // interface ArtifactReviver
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
68
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
69 public interface LockedIdsProvider {
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
70 Set<Integer> getLockedIds();
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
71 } // interface LockedIdsProvider
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
72
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 private static Logger logger = Logger.getLogger(DatabaseCleaner.class);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
75 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
76 * Number of artifacts to be loaded at once. Used to
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
77 * mitigate the problem of a massive denial of service
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
78 * if too many artifacts have died since last cleanup.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
79 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 public static final int MAX_ROWS = 50;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
82 public static final Set<Integer> EMPTY_IDS = Collections.emptySet();
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
83
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
84 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
85 * The SQL statement to select the outdated artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
86 */
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
87 public String SQL_OUTDATED;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
89 public String SQL_OUTDATED_COLLECTIONS;
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
90 public String SQL_DELETE_COLLECTION_ITEMS;
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
91 public String SQL_DELETE_COLLECTION;
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
92
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
93 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
94 * The SQL statement to delete some artifacts from the database.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
95 */
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
96 public String SQL_DELETE_ARTIFACT;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
98 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
99 * XPath to figure out how long the cleaner should sleep between
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
100 * cleanups. This is stored in the global configuration.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
101 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 public static final String SLEEP_XPATH =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 "/artifact-database/cleaner/sleep-time/text()";
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
105 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
106 * Default nap time between cleanups: 5 minutes.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
107 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 public static final long SLEEP_DEFAULT =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 5 * 60 * 1000L; // 5 minutes
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
111 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
112 * The configured nap time.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
113 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 protected long sleepTime;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
116 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
117 * Internal locking mechanism to prevent some race conditions.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
118 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 protected Object sleepLock = new Object();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
121 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
122 * A reference to the global context.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
123 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 protected Object context;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
126 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
127 * A specialized Id filter which only delete some artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
128 * This is used to prevent deletion of living artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
129 */
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
130 protected LockedIdsProvider lockedIdsProvider;
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
131
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
132 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
133 * The reviver used to bring the dead artifact on last
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
134 * time back to live to call endOfLife() on them.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
135 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
136 protected ArtifactReviver reviver;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
137
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
138 protected DBConnection dbConnection;
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
139
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
140 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
141 * Default constructor.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
142 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 public DatabaseCleaner() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
146 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
147 * Constructor to create a cleaner with a given global context
91
730ff077a58c More javadoc, some code formatting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 90
diff changeset
148 * and a given reviver.
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
149 * @param context The global context of the artifact database
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
150 * @param reviver The reviver to awake artifact one last time.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
151 */
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
152 public DatabaseCleaner(Object context, ArtifactReviver reviver, DBConfig config) {
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 setDaemon(true);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 sleepTime = getSleepTime();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 this.context = context;
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
156 this.reviver = reviver;
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
157 this.dbConnection = config.getDBConnection();
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
158 setupSQL(config.getSQL());
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
159 }
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
160
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
161 protected void setupSQL(SQL sql) {
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
162 SQL_OUTDATED = sql.get("artifacts.outdated");
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
163 SQL_OUTDATED_COLLECTIONS = sql.get("collections.outdated");
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
164 SQL_DELETE_COLLECTION_ITEMS = sql.get("delete.collection.items");
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
165 SQL_DELETE_COLLECTION = sql.get("delete.collection");
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
166 SQL_DELETE_ARTIFACT = sql.get("artifacts.delete");
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
169 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
170 * Sets the filter that prevents deletion of living artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
171 * Living artifacts are artifacts which are currently active
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
172 * inside the artifact database. Deleting them in this state
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
173 * would create severe internal problems.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
174 */
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
175 public void setLockedIdsProvider(LockedIdsProvider lockedIdsProvider) {
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
176 this.lockedIdsProvider = lockedIdsProvider;
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
177 }
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
178
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
179 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
180 * External hook to tell the cleaner to wake up before its
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
181 * regular nap time is over. This is the case when the artifact
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
182 * database finds an artifact which is already outdated.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
183 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 public void wakeup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 sleepLock.notify();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
190 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
191 * Fetches the sleep time from the global configuration.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
192 * @return the time to sleep between database cleanups in ms.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
193 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 protected static long getSleepTime() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 String sleepTimeString = Config.getStringXPath(SLEEP_XPATH);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197 if (sleepTimeString == null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201 // sleep at least one second
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
202 return Math.max(Long.parseLong(sleepTimeString), 1000L);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
203 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 catch (NumberFormatException nfe) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 logger.warn("Cleaner sleep time defaults to " + SLEEP_DEFAULT);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
210 private static class IdIdentifier {
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
211
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
212 int id;
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
213 String identifier;
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
214
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
215 private IdIdentifier(int id, String identifier) {
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
216 this.id = id;
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
217 this.identifier = identifier;
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
218 }
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
219 } // class IdIdentifier
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
220
394
c40729bfe06d Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 393
diff changeset
221 private static final class IdData
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
222 extends IdIdentifier
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
223 {
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
224 byte [] data;
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
225 String factoryName;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
226
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
227 public IdData(
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
228 int id,
394
c40729bfe06d Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 393
diff changeset
229 String factoryName,
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
230 byte [] data,
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
231 String identifier
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
232 ) {
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
233 super(id, identifier);
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
234 this.factoryName = factoryName;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
235 this.data = data;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
236 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
237 } // class IdData
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
238
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
239 /**
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
240 * Cleaning is done in two phases. First we fetch a list of ids
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
241 * of artifacts. If there are artifacts the cleaning is done.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
242 * Second we load the artifacts one by one one and call there
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
243 * endOfLife() method. In this loop we remove them from database, too.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
244 * Each deletion is commited to ensure that a sudden failure
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
245 * of the artifact database server does delete artifacts twice
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
246 * or does not delete them at all. After this the first step
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
247 * is repeated.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
248 */
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
249 protected void cleanup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
250 logger.info("database cleanup");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
251
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
252 Connection connection = null;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
253 PreparedStatement fetchIds = null;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
254 PreparedStatement stmnt = null;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
255 ResultSet result = null;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
256
305
f33401ea2a6c Artifact database: Refactorized the usage of dialect independent SQL to be reusable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 301
diff changeset
257 DataSource dataSource = dbConnection.getDataSource();
231
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
258
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
259 Set<Integer> lockedIds = lockedIdsProvider != null
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
260 ? lockedIdsProvider.getLockedIds()
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
261 : EMPTY_IDS;
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
262
238
61b7041204a7 Fix for flys/issue8 (part 2)
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 232
diff changeset
263 String questionMarks = lockedIds.isEmpty()
241
4253995c970e Fix for flys/issue8 (part 3)
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 238
diff changeset
264 ? "-666" // XXX: A bit hackish.
238
61b7041204a7 Fix for flys/issue8 (part 2)
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 232
diff changeset
265 : StringUtils.repeat('?', lockedIds.size(), ',');
231
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
266
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
267 List<String> deletedCollections = new ArrayList<String>();
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
268 List<String> deletedArtifacts = new ArrayList<String>();
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
269
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
270 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
271 connection = dataSource.getConnection();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
272 connection.setAutoCommit(false);
231
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
273
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
274 fetchIds = connection.prepareStatement(
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
275 SQL_OUTDATED.replace("$LOCKED_IDS$", questionMarks));
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
276
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
277 // some dbms like derby do not support LIMIT
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
278 // in SQL statements.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
279 fetchIds.setMaxRows(MAX_ROWS);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
280
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
281 // Fetch ids of outdated collections
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
282 stmnt = connection.prepareStatement(
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
283 SQL_OUTDATED_COLLECTIONS.replace(
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
284 "$LOCKED_IDS$", questionMarks));
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
285
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
286 // fill in the locked ids
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
287 int idx = 1;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
288 for (Integer id: lockedIds) {
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
289 fetchIds.setInt(idx, id);
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
290 stmnt .setInt(idx, id);
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
291 ++idx;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
292 }
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
293
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
294 ArrayList<IdIdentifier> cs = new ArrayList<IdIdentifier>();
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
295 result = stmnt.executeQuery();
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
296 while (result.next()) {
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
297 cs.add(new IdIdentifier(
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
298 result.getInt(1),
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
299 result.getString(2)));
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
300 }
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
301
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
302 result.close(); result = null;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
303 stmnt.close(); stmnt = null;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
304
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
305 // delete collection items
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
306 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION_ITEMS);
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
307
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
308 for (IdIdentifier id: cs) {
393
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
309 logger.debug("Mark collection for deletion: " + id.id);
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
310 stmnt.setInt(1, id.id);
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
311 stmnt.execute();
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
312 }
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
313
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
314 stmnt.close(); stmnt = null;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
315
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
316 // delete collections
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
317 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION);
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
318
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
319 for (IdIdentifier id: cs) {
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
320 stmnt.setInt(1, id.id);
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
321 stmnt.execute();
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
322 deletedCollections.add(id.identifier);
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
323 }
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
324
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
325 stmnt.close(); stmnt = null;
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
326 connection.commit();
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
327
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
328 cs = null;
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
329
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
330 // remove artifacts
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
331 stmnt = connection.prepareStatement(SQL_DELETE_ARTIFACT);
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
332
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
333 for (;;) {
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
334 List<IdData> ids = new ArrayList<IdData>();
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
335
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
336 result = fetchIds.executeQuery();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
337
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
338 while (result.next()) {
231
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
339 ids.add(new IdData(
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
340 result.getInt(1),
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
341 result.getString(2),
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
342 result.getBytes(3),
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
343 result.getString(4)));
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
344 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
345
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
346 result.close(); result = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
347
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
348 if (ids.isEmpty()) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
349 break;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
350 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
351
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
352 for (int i = ids.size()-1; i >= 0; --i) {
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
353 IdData idData = ids.get(i);
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
354 Artifact artifact = reviver.reviveArtifact(
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
355 idData.factoryName, idData.data);
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
356 idData.data = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
357
393
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
358 logger.debug("Prepare Artifact (id="
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
359 + idData.id + ") for deletion.");
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
360
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
361 stmnt.setInt(1, idData.id);
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
362 stmnt.execute();
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
363 connection.commit();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
364
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
365 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
366 if (artifact != null) {
393
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
367 logger.debug("Call endOfLife for Artifact: "
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
368 + artifact.identifier());
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
369
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
370 artifact.endOfLife(context);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
371 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
372 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
373 catch (Exception e) {
393
b7831cefbb62 Added some more debug output during the process of removing Artifacts and Collections by the DatabaseCleaner.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 315
diff changeset
374 logger.error(e.getMessage(), e);
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
375 }
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
376
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
377 deletedArtifacts.add(idData.identifier);
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
378 } // for all fetched data
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
379 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
380 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
381 catch (SQLException sqle) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
382 logger.error(sqle.getLocalizedMessage(), sqle);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
383 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
384 finally {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
385 if (result != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
386 try { result.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
387 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
388 }
232
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
389 if (stmnt != null) {
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
390 try { stmnt.close(); }
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
391 catch (SQLException sqle) {}
202b6ae2fcad Database cleaner: Removes outdated collections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 231
diff changeset
392 }
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
393 if (fetchIds != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
394 try { fetchIds.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
395 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
396 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
397 if (connection != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
398 try { connection.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
399 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
400 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
401 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
402
315
03e508e57b85 Generate kill events if and only if there were killed collections or artifacts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
403 if (!deletedCollections.isEmpty()) {
03e508e57b85 Generate kill events if and only if there were killed collections or artifacts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
404 reviver.killedCollections(deletedCollections);
03e508e57b85 Generate kill events if and only if there were killed collections or artifacts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
405 }
03e508e57b85 Generate kill events if and only if there were killed collections or artifacts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
406
03e508e57b85 Generate kill events if and only if there were killed collections or artifacts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
407 if (!deletedArtifacts.isEmpty()) {
03e508e57b85 Generate kill events if and only if there were killed collections or artifacts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
408 reviver.killedArtifacts(deletedArtifacts);
03e508e57b85 Generate kill events if and only if there were killed collections or artifacts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 314
diff changeset
409 }
314
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
410
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
411 if (logger.isDebugEnabled()) {
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
412 logger.debug(
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
413 "collections removed: " + deletedCollections.size());
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
414 logger.debug(
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
415 "artifacts removed: " + deletedArtifacts.size());
31ee2b3b5a57 forward list of deleted collections and artifacts from data cleaner to backend to backend listeners.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 305
diff changeset
416 }
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
417 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
418
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
419 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
420 * The main code of the cleaner. It sleeps for the configured
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
421 * nap time, cleans up the database, sleeps again and so on.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
422 */
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
423 @Override
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
424 public void run() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
425 logger.info("sleep time: " + sleepTime + "ms");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
426 for (;;) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
427 cleanup();
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
428 long startTime = System.currentTimeMillis();
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
429
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
430 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
431 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
432 sleepLock.wait(sleepTime);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
433 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
434 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
435 catch (InterruptedException ie) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
436 }
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
437
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
438 long stopTime = System.currentTimeMillis();
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
439
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
440 if (logger.isDebugEnabled()) {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
441 logger.debug("Cleaner slept " + (stopTime - startTime) + "ms");
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
442 }
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
443 } // for (;;)
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
444 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
445 }
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
446 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org