annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 30:88972c6daa4f

Added a cleanup thread which periodically removes outdated artifacts from database and calls there endOfLife() method. artifacts/trunk@70 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Sep 2009 23:16:18 +0000
parents
children c2d53bd30ab8
rev   line source
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.artifactdatabase;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import de.intevation.artifacts.Artifact;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import java.sql.Connection;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.sql.SQLException;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.sql.PreparedStatement;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 import java.sql.ResultSet;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 import javax.sql.DataSource;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import org.apache.log4j.Logger;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import java.util.ArrayList;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 /**
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 * @author Sascha L. Teichmann
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 */
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 public class DatabaseCleaner
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 extends Thread
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 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
23
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 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
25
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 public static final String SQL_OUTDATED =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 SQL.get("artifacts.outdated");
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 public static final String SQL_DELETE =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 SQL.get("artifacts.delete");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 public static final String SLEEP_XPATH =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 "/artifact-database/cleaner/sleep-time/text()";
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 public static final long SLEEP_DEFAULT =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 5 * 60 * 1000L; // 5 minutes
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 protected long sleepTime;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 protected Object sleepLock = new Object();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 protected Object context;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 public DatabaseCleaner() {
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
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 public DatabaseCleaner(Object context) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 setDaemon(true);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 sleepTime = getSleepTime();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 this.context = context;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 public void wakeup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 sleepLock.notify();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 protected static long getSleepTime() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 String sleepTimeString = Config.getStringXPath(SLEEP_XPATH);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 if (sleepTimeString == null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 // sleep at least one second
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 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
68 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 catch (NumberFormatException nfe) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 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
71 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 private static final class IdData {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 int id;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 byte [] data;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 public IdData(int id, byte [] data) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 this.id = id;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 this.data = data;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 } // class IdData
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 /**
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 * 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
88 * 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
89 * 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
90 * 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
91 * 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
92 * 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
93 * 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
94 * is repeated.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 */
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 protected void cleanup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 logger.info("database cleanup");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 Connection connection = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 PreparedStatement fetchIds = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 PreparedStatement deleteId = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 ResultSet result = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 int removedArtifacts = 0;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 DataSource dataSource = DBConnection.getDataSource();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 connection = dataSource.getConnection();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 connection.setAutoCommit(false);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 fetchIds = connection.prepareStatement(SQL_OUTDATED);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 deleteId = connection.prepareStatement(SQL_DELETE);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 // 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
114 // in SQL statements.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 fetchIds.setMaxRows(MAX_ROWS);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 for (;;) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 ArrayList ids = new ArrayList();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 result = fetchIds.executeQuery();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 while (result.next()) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 ids.add(new IdData(
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 result.getInt(1), result.getBytes(2)));
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 result.close(); result = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 if (ids.isEmpty()) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 break;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 for (int i = ids.size()-1; i >= 0; --i) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 IdData idData = (IdData)ids.get(i);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 Artifact artifact = Backend.restoreArtifact(
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 idData.data);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 idData.data = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 deleteId.setInt(1, idData.id);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 deleteId.execute();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 connection.commit();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 if (artifact != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 artifact.endOfLife(context);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 catch (Exception e) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 logger.error(e.getLocalizedMessage(), e);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 } // for all fetched data
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 removedArtifacts += ids.size();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 catch (SQLException sqle) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 logger.error(sqle.getLocalizedMessage(), sqle);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 finally {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 if (result != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161 try { result.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 if (fetchIds != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 try { fetchIds.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 catch (SQLException sqle) {}
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 if (deleteId != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 try { deleteId.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 if (connection != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 try { connection.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 logger.info("artifacts removed: " + removedArtifacts);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 public void run() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 logger.info("sleep time: " + sleepTime + "ms");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 for (;;) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 cleanup();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 sleepLock.wait(sleepTime);
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 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
190 catch (InterruptedException ie) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192 } // for (;;)
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org