comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 232:202b6ae2fcad

Database cleaner: Removes outdated collections, too. artifacts/trunk@1636 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 31 Mar 2011 17:14:22 +0000
parents 1ea35226a6de
children 61b7041204a7
comparison
equal deleted inserted replaced
231:1ea35226a6de 232:202b6ae2fcad
78 * The SQL statement to select the outdated artifacts. 78 * The SQL statement to select the outdated artifacts.
79 */ 79 */
80 public static final String SQL_OUTDATED = 80 public static final String SQL_OUTDATED =
81 SQL.get("artifacts.outdated"); 81 SQL.get("artifacts.outdated");
82 82
83 public static final String SQL_OUTDATED_COLLECTIONS =
84 SQL.get("collections.outdated");
85
86 public static final String SQL_DELETE_COLLECTION_ITEMS =
87 SQL.get("delete.collection.items");
88
89 public static final String SQL_DELETE_COLLECTION =
90 SQL.get("delete.collection");
91
83 /** 92 /**
84 * The SQL statement to delete some artifacts from the database. 93 * The SQL statement to delete some artifacts from the database.
85 */ 94 */
86 public static final String SQL_DELETE = 95 public static final String SQL_DELETE_ARTIFACT =
87 SQL.get("artifacts.delete"); 96 SQL.get("artifacts.delete");
88 97
89 /** 98 /**
90 * XPath to figure out how long the cleaner should sleep between 99 * XPath to figure out how long the cleaner should sleep between
91 * cleanups. This is stored in the global configuration. 100 * cleanups. This is stored in the global configuration.
211 * is repeated. 220 * is repeated.
212 */ 221 */
213 protected void cleanup() { 222 protected void cleanup() {
214 logger.info("database cleanup"); 223 logger.info("database cleanup");
215 224
216 Connection connection = null; 225 Connection connection = null;
217 PreparedStatement fetchIds = null; 226 PreparedStatement fetchIds = null;
218 PreparedStatement deleteId = null; 227 PreparedStatement stmnt = null;
219 ResultSet result = null; 228 ResultSet result = null;
220 229
221 int removedArtifacts = 0; 230 int removedCollections = 0;
231 int removedArtifacts = 0;
222 232
223 DataSource dataSource = DBConnection.getDataSource(); 233 DataSource dataSource = DBConnection.getDataSource();
224 234
225 Set<Integer> lockedIds = lockedIdsProvider != null 235 Set<Integer> lockedIds = lockedIdsProvider != null
226 ? lockedIdsProvider.getLockedIds() 236 ? lockedIdsProvider.getLockedIds()
233 connection.setAutoCommit(false); 243 connection.setAutoCommit(false);
234 244
235 fetchIds = connection.prepareStatement( 245 fetchIds = connection.prepareStatement(
236 SQL_OUTDATED.replace("$LOCKED_IDS$", questionMarks)); 246 SQL_OUTDATED.replace("$LOCKED_IDS$", questionMarks));
237 247
238 int idx = 1;
239 for (Integer id: lockedIds) {
240 fetchIds.setInt(idx++, id);
241 }
242
243 deleteId = connection.prepareStatement(SQL_DELETE);
244
245 // some dbms like derby do not support LIMIT 248 // some dbms like derby do not support LIMIT
246 // in SQL statements. 249 // in SQL statements.
247 fetchIds.setMaxRows(MAX_ROWS); 250 fetchIds.setMaxRows(MAX_ROWS);
251
252 // Fetch ids of outdated collections
253 stmnt = connection.prepareStatement(
254 SQL_OUTDATED_COLLECTIONS.replace(
255 "$LOCKED_IDS$", questionMarks));
256
257 // fill in the locked ids
258 int idx = 1;
259 for (Integer id: lockedIds) {
260 fetchIds.setInt(idx, id);
261 stmnt .setInt(idx, id);
262 ++idx;
263 }
264
265 ArrayList<Integer> cs = new ArrayList<Integer>();
266 result = stmnt.executeQuery();
267 while (result.next()) {
268 cs.add(result.getInt(1));
269 }
270
271 result.close(); result = null;
272 stmnt.close(); stmnt = null;
273
274 // delete collection items
275 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION_ITEMS);
276
277 for (Integer id: cs) {
278 stmnt.setInt(1, id);
279 stmnt.execute();
280 }
281
282 stmnt.close(); stmnt = null;
283
284 // delete collections
285 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION);
286
287 for (Integer id: cs) {
288 stmnt.setInt(1, id);
289 stmnt.execute();
290 }
291
292 stmnt.close(); stmnt = null;
293 connection.commit();
294
295 removedCollections = cs.size(); cs = null;
296
297 // remove artifacts
298 stmnt = connection.prepareStatement(SQL_DELETE_ARTIFACT);
248 299
249 for (;;) { 300 for (;;) {
250 List<IdData> ids = new ArrayList<IdData>(); 301 List<IdData> ids = new ArrayList<IdData>();
251 302
252 result = fetchIds.executeQuery(); 303 result = fetchIds.executeQuery();
268 IdData idData = ids.get(i); 319 IdData idData = ids.get(i);
269 Artifact artifact = reviver.reviveArtifact( 320 Artifact artifact = reviver.reviveArtifact(
270 idData.factoryName, idData.data); 321 idData.factoryName, idData.data);
271 idData.data = null; 322 idData.data = null;
272 323
273 deleteId.setInt(1, idData.id); 324 stmnt.setInt(1, idData.id);
274 deleteId.execute(); 325 stmnt.execute();
275 connection.commit(); 326 connection.commit();
276 327
277 try { 328 try {
278 if (artifact != null) { 329 if (artifact != null) {
279 artifact.endOfLife(context); 330 artifact.endOfLife(context);
293 finally { 344 finally {
294 if (result != null) { 345 if (result != null) {
295 try { result.close(); } 346 try { result.close(); }
296 catch (SQLException sqle) {} 347 catch (SQLException sqle) {}
297 } 348 }
349 if (stmnt != null) {
350 try { stmnt.close(); }
351 catch (SQLException sqle) {}
352 }
298 if (fetchIds != null) { 353 if (fetchIds != null) {
299 try { fetchIds.close(); } 354 try { fetchIds.close(); }
300 catch (SQLException sqle) {} 355 catch (SQLException sqle) {}
301 } 356 }
302 if (deleteId != null) {
303 try { deleteId.close(); }
304 catch (SQLException sqle) {}
305 }
306 if (connection != null) { 357 if (connection != null) {
307 try { connection.close(); } 358 try { connection.close(); }
308 catch (SQLException sqle) {} 359 catch (SQLException sqle) {}
309 } 360 }
310 } 361 }
311 362
312 logger.info("artifacts removed: " + removedArtifacts); 363 logger.info("collections removed: " + removedCollections);
364 logger.info("artifacts removed: " + removedArtifacts);
313 } 365 }
314 366
315 /** 367 /**
316 * The main code of the cleaner. It sleeps for the configured 368 * The main code of the cleaner. It sleeps for the configured
317 * nap time, cleans up the database, sleeps again and so on. 369 * nap time, cleans up the database, sleeps again and so on.

http://dive4elements.wald.intevation.org