3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.daemon.storage;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.sql.PreparedStatement;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
31 import java.util.concurrent.ConcurrentHashMap;
32 import javax.mail.Header;
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeUtility;
35 import org.sonews.daemon.BootstrapConfig;
36 import org.sonews.util.Log;
37 import org.sonews.feed.Subscription;
38 import org.sonews.util.Pair;
41 * Database facade class.
42 * @author Christian Lins
45 // TODO: Refactor this class to reduce size (e.g. ArticleDatabase GroupDatabase)
49 public static final int MAX_RESTARTS = 3;
51 private static final Map<Thread, Database> instances
52 = new ConcurrentHashMap<Thread, Database>();
55 * @return Instance of the current Database backend. Returns null if an error
58 public static Database getInstance(boolean create)
61 if(!instances.containsKey(Thread.currentThread()) && create)
63 Database db = new Database();
65 instances.put(Thread.currentThread(), db);
70 return instances.get(Thread.currentThread());
74 public static Database getInstance()
77 return getInstance(true);
80 private Connection conn = null;
81 private PreparedStatement pstmtAddArticle1 = null;
82 private PreparedStatement pstmtAddArticle2 = null;
83 private PreparedStatement pstmtAddArticle3 = null;
84 private PreparedStatement pstmtAddArticle4 = null;
85 private PreparedStatement pstmtAddGroup0 = null;
86 private PreparedStatement pstmtAddEvent = null;
87 private PreparedStatement pstmtCountArticles = null;
88 private PreparedStatement pstmtCountGroups = null;
89 private PreparedStatement pstmtDeleteArticle0 = null;
90 private PreparedStatement pstmtGetArticle0 = null;
91 private PreparedStatement pstmtGetArticle1 = null;
92 private PreparedStatement pstmtGetArticleHeaders = null;
93 private PreparedStatement pstmtGetArticleHeads = null;
94 private PreparedStatement pstmtGetArticleIDs = null;
95 private PreparedStatement pstmtGetArticleIndex = null;
96 private PreparedStatement pstmtGetConfigValue = null;
97 private PreparedStatement pstmtGetEventsCount0 = null;
98 private PreparedStatement pstmtGetEventsCount1 = null;
99 private PreparedStatement pstmtGetGroupForList = null;
100 private PreparedStatement pstmtGetGroup0 = null;
101 private PreparedStatement pstmtGetGroup1 = null;
102 private PreparedStatement pstmtGetFirstArticleNumber = null;
103 private PreparedStatement pstmtGetListForGroup = null;
104 private PreparedStatement pstmtGetLastArticleNumber = null;
105 private PreparedStatement pstmtGetMaxArticleID = null;
106 private PreparedStatement pstmtGetMaxArticleIndex = null;
107 private PreparedStatement pstmtGetPostingsCount = null;
108 private PreparedStatement pstmtGetSubscriptions = null;
109 private PreparedStatement pstmtIsArticleExisting = null;
110 private PreparedStatement pstmtIsGroupExisting = null;
111 private PreparedStatement pstmtSetConfigValue0 = null;
112 private PreparedStatement pstmtSetConfigValue1 = null;
114 /** How many times the database connection was reinitialized */
115 private int restarts = 0;
118 * Rises the database: reconnect and recreate all prepared statements.
119 * @throws java.lang.SQLException
126 // Load database driver
128 BootstrapConfig.getInstance().get(BootstrapConfig.STORAGE_DBMSDRIVER, "java.lang.Object"));
130 // Establish database connection
131 this.conn = DriverManager.getConnection(
132 BootstrapConfig.getInstance().get(BootstrapConfig.STORAGE_DATABASE, "<not specified>"),
133 BootstrapConfig.getInstance().get(BootstrapConfig.STORAGE_USER, "root"),
134 BootstrapConfig.getInstance().get(BootstrapConfig.STORAGE_PASSWORD, ""));
136 this.conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
137 if(this.conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE)
139 Log.msg("Warning: Database is NOT fully serializable!", false);
142 // Prepare statements for method addArticle()
143 this.pstmtAddArticle1 = conn.prepareStatement(
144 "INSERT INTO articles (article_id, body) VALUES(?, ?)");
145 this.pstmtAddArticle2 = conn.prepareStatement(
146 "INSERT INTO headers (article_id, header_key, header_value, header_index) " +
147 "VALUES (?, ?, ?, ?)");
148 this.pstmtAddArticle3 = conn.prepareStatement(
149 "INSERT INTO postings (group_id, article_id, article_index)" +
151 this.pstmtAddArticle4 = conn.prepareStatement(
152 "INSERT INTO article_ids (article_id, message_id) VALUES (?, ?)");
154 // Prepare statement for method addStatValue()
155 this.pstmtAddEvent = conn.prepareStatement(
156 "INSERT INTO events VALUES (?, ?, ?)");
158 // Prepare statement for method addGroup()
159 this.pstmtAddGroup0 = conn.prepareStatement(
160 "INSERT INTO groups (name, flags) VALUES (?, ?)");
162 // Prepare statement for method countArticles()
163 this.pstmtCountArticles = conn.prepareStatement(
164 "SELECT Count(article_id) FROM article_ids");
166 // Prepare statement for method countGroups()
167 this.pstmtCountGroups = conn.prepareStatement(
168 "SELECT Count(group_id) FROM groups WHERE " +
169 "flags & " + Group.DELETED + " = 0");
171 // Prepare statements for method delete(article)
172 this.pstmtDeleteArticle0 = conn.prepareStatement(
173 "DELETE FROM articles WHERE article_id = " +
174 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
176 // Prepare statements for methods getArticle()
177 this.pstmtGetArticle0 = conn.prepareStatement(
178 "SELECT * FROM articles WHERE article_id = " +
179 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
180 this.pstmtGetArticle1 = conn.prepareStatement(
181 "SELECT * FROM articles WHERE article_id = " +
182 "(SELECT article_id FROM postings WHERE " +
183 "article_index = ? AND group_id = ?)");
185 // Prepare statement for method getArticleHeaders()
186 this.pstmtGetArticleHeaders = conn.prepareStatement(
187 "SELECT header_key, header_value FROM headers WHERE article_id = ? " +
188 "ORDER BY header_index ASC");
190 this.pstmtGetArticleIDs = conn.prepareStatement(
191 "SELECT article_index FROM postings WHERE group_id = ?");
193 // Prepare statement for method getArticleIndex
194 this.pstmtGetArticleIndex = conn.prepareStatement(
195 "SELECT article_index FROM postings WHERE " +
196 "article_id = (SELECT article_id FROM article_ids " +
197 "WHERE message_id = ?) " +
198 " AND group_id = ?");
200 // Prepare statements for method getArticleHeads()
201 this.pstmtGetArticleHeads = conn.prepareStatement(
202 "SELECT article_id, article_index FROM postings WHERE " +
203 "postings.group_id = ? AND article_index >= ? AND " +
204 "article_index <= ?");
206 // Prepare statements for method getConfigValue()
207 this.pstmtGetConfigValue = conn.prepareStatement(
208 "SELECT config_value FROM config WHERE config_key = ?");
210 // Prepare statements for method getEventsCount()
211 this.pstmtGetEventsCount0 = conn.prepareStatement(
212 "SELECT Count(*) FROM events WHERE event_key = ? AND " +
213 "event_time >= ? AND event_time < ?");
215 this.pstmtGetEventsCount1 = conn.prepareStatement(
216 "SELECT Count(*) FROM events WHERE event_key = ? AND " +
217 "event_time >= ? AND event_time < ? AND group_id = ?");
219 // Prepare statement for method getGroupForList()
220 this.pstmtGetGroupForList = conn.prepareStatement(
221 "SELECT name FROM groups INNER JOIN groups2list " +
222 "ON groups.group_id = groups2list.group_id " +
223 "WHERE groups2list.listaddress = ?");
225 // Prepare statement for method getGroup()
226 this.pstmtGetGroup0 = conn.prepareStatement(
227 "SELECT group_id, flags FROM groups WHERE Name = ?");
228 this.pstmtGetGroup1 = conn.prepareStatement(
229 "SELECT name FROM groups WHERE group_id = ?");
231 // Prepare statement for method getLastArticleNumber()
232 this.pstmtGetLastArticleNumber = conn.prepareStatement(
233 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
235 // Prepare statement for method getListForGroup()
236 this.pstmtGetListForGroup = conn.prepareStatement(
237 "SELECT listaddress FROM groups2list INNER JOIN groups " +
238 "ON groups.group_id = groups2list.group_id WHERE name = ?");
240 // Prepare statement for method getMaxArticleID()
241 this.pstmtGetMaxArticleID = conn.prepareStatement(
242 "SELECT Max(article_id) FROM articles");
244 // Prepare statement for method getMaxArticleIndex()
245 this.pstmtGetMaxArticleIndex = conn.prepareStatement(
246 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
248 // Prepare statement for method getFirstArticleNumber()
249 this.pstmtGetFirstArticleNumber = conn.prepareStatement(
250 "SELECT Min(article_index) FROM postings WHERE group_id = ?");
252 // Prepare statement for method getPostingsCount()
253 this.pstmtGetPostingsCount = conn.prepareStatement(
254 "SELECT Count(*) FROM postings NATURAL JOIN groups " +
255 "WHERE groups.name = ?");
257 // Prepare statement for method getSubscriptions()
258 this.pstmtGetSubscriptions = conn.prepareStatement(
259 "SELECT host, port, name FROM peers NATURAL JOIN " +
260 "peer_subscriptions NATURAL JOIN groups WHERE feedtype = ?");
262 // Prepare statement for method isArticleExisting()
263 this.pstmtIsArticleExisting = conn.prepareStatement(
264 "SELECT Count(article_id) FROM article_ids WHERE message_id = ?");
266 // Prepare statement for method isGroupExisting()
267 this.pstmtIsGroupExisting = conn.prepareStatement(
268 "SELECT * FROM groups WHERE name = ?");
270 // Prepare statement for method setConfigValue()
271 this.pstmtSetConfigValue0 = conn.prepareStatement(
272 "DELETE FROM config WHERE config_key = ?");
273 this.pstmtSetConfigValue1 = conn.prepareStatement(
274 "INSERT INTO config VALUES(?, ?)");
276 catch(ClassNotFoundException ex)
278 throw new Error("JDBC Driver not found!", ex);
283 * Adds an article to the database.
286 * @throws java.sql.SQLException
288 public void addArticle(final Article article)
293 this.conn.setAutoCommit(false);
295 int newArticleID = getMaxArticleID() + 1;
297 // Fill prepared statement with values;
298 // writes body to article table
299 pstmtAddArticle1.setInt(1, newArticleID);
300 pstmtAddArticle1.setBytes(2, article.getBody().getBytes());
301 pstmtAddArticle1.execute();
304 Enumeration headers = article.getAllHeaders();
305 for(int n = 0; headers.hasMoreElements(); n++)
307 Header header = (Header)headers.nextElement();
308 pstmtAddArticle2.setInt(1, newArticleID);
309 pstmtAddArticle2.setString(2, header.getName().toLowerCase());
310 pstmtAddArticle2.setString(3,
311 header.getValue().replaceAll("[\r\n]", ""));
312 pstmtAddArticle2.setInt(4, n);
313 pstmtAddArticle2.execute();
316 // For each newsgroup add a reference
317 List<Group> groups = article.getGroups();
318 for(Group group : groups)
320 pstmtAddArticle3.setLong(1, group.getID());
321 pstmtAddArticle3.setInt(2, newArticleID);
322 pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getID()) + 1);
323 pstmtAddArticle3.execute();
326 // Write message-id to article_ids table
327 this.pstmtAddArticle4.setInt(1, newArticleID);
328 this.pstmtAddArticle4.setString(2, article.getMessageID());
329 this.pstmtAddArticle4.execute();
332 this.conn.setAutoCommit(true);
334 this.restarts = 0; // Reset error count
336 catch(SQLException ex)
340 this.conn.rollback(); // Rollback changes
342 catch(SQLException ex2)
344 Log.msg("Rollback of addArticle() failed: " + ex2, false);
349 this.conn.setAutoCommit(true); // and release locks
351 catch(SQLException ex2)
353 Log.msg("setAutoCommit(true) of addArticle() failed: " + ex2, false);
356 restartConnection(ex);
362 * Adds a group to the Database. This method is not accessible via NNTP.
364 * @throws java.sql.SQLException
366 public void addGroup(String name, int flags)
371 this.conn.setAutoCommit(false);
372 pstmtAddGroup0.setString(1, name);
373 pstmtAddGroup0.setInt(2, flags);
375 pstmtAddGroup0.executeUpdate();
377 this.conn.setAutoCommit(true);
378 this.restarts = 0; // Reset error count
380 catch(SQLException ex)
382 this.conn.rollback();
383 this.conn.setAutoCommit(true);
384 restartConnection(ex);
385 addGroup(name, flags);
389 public void addEvent(long time, byte type, long gid)
394 this.conn.setAutoCommit(false);
395 this.pstmtAddEvent.setLong(1, time);
396 this.pstmtAddEvent.setInt(2, type);
397 this.pstmtAddEvent.setLong(3, gid);
398 this.pstmtAddEvent.executeUpdate();
400 this.conn.setAutoCommit(true);
403 catch(SQLException ex)
405 this.conn.rollback();
406 this.conn.setAutoCommit(true);
408 restartConnection(ex);
409 addEvent(time, type, gid);
413 public int countArticles()
420 rs = this.pstmtCountArticles.executeQuery();
430 catch(SQLException ex)
432 restartConnection(ex);
433 return countArticles();
445 public int countGroups()
452 rs = this.pstmtCountGroups.executeQuery();
462 catch(SQLException ex)
464 restartConnection(ex);
465 return countGroups();
477 public void delete(final String messageID)
482 this.conn.setAutoCommit(false);
484 this.pstmtDeleteArticle0.setString(1, messageID);
485 int rs = this.pstmtDeleteArticle0.executeUpdate();
487 // We trust the ON DELETE CASCADE functionality to delete
488 // orphaned references
491 this.conn.setAutoCommit(true);
493 catch(SQLException ex)
499 public Article getArticle(String messageID)
505 pstmtGetArticle0.setString(1, messageID);
506 rs = pstmtGetArticle0.executeQuery();
514 String body = new String(rs.getBytes("body"));
515 String headers = getArticleHeaders(rs.getInt("article_id"));
516 return new Article(headers, body);
519 catch(SQLException ex)
521 restartConnection(ex);
522 return getArticle(messageID);
529 restarts = 0; // Reset error count
535 * Retrieves an article by its ID.
538 * @throws java.sql.SQLException
540 public Article getArticle(long articleIndex, long gid)
547 this.pstmtGetArticle1.setLong(1, articleIndex);
548 this.pstmtGetArticle1.setLong(2, gid);
550 rs = this.pstmtGetArticle1.executeQuery();
554 String body = new String(rs.getBytes("body"));
555 String headers = getArticleHeaders(rs.getInt("article_id"));
556 return new Article(headers, body);
563 catch(SQLException ex)
565 restartConnection(ex);
566 return getArticle(articleIndex, gid);
578 public String getArticleHeaders(long articleID)
585 this.pstmtGetArticleHeaders.setLong(1, articleID);
586 rs = this.pstmtGetArticleHeaders.executeQuery();
588 StringBuilder buf = new StringBuilder();
593 buf.append(rs.getString(1)); // key
595 String foldedValue = MimeUtility.fold(0, rs.getString(2));
596 buf.append(foldedValue); // value
608 return buf.toString();
610 catch(SQLException ex)
612 restartConnection(ex);
613 return getArticleHeaders(articleID);
622 public long getArticleIndex(Article article, Group group)
629 this.pstmtGetArticleIndex.setString(1, article.getMessageID());
630 this.pstmtGetArticleIndex.setLong(2, group.getID());
632 rs = this.pstmtGetArticleIndex.executeQuery();
635 return rs.getLong(1);
642 catch(SQLException ex)
644 restartConnection(ex);
645 return getArticleIndex(article, group);
655 * Returns a list of Long/Article Pairs.
656 * @throws java.sql.SQLException
658 public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, int first, int last)
665 this.pstmtGetArticleHeads.setLong(1, group.getID());
666 this.pstmtGetArticleHeads.setInt(2, first);
667 this.pstmtGetArticleHeads.setInt(3, last);
668 rs = pstmtGetArticleHeads.executeQuery();
670 List<Pair<Long, ArticleHead>> articles
671 = new ArrayList<Pair<Long, ArticleHead>>();
675 long aid = rs.getLong("article_id");
676 long aidx = rs.getLong("article_index");
677 String headers = getArticleHeaders(aid);
678 articles.add(new Pair<Long, ArticleHead>(aidx,
679 new ArticleHead(headers)));
684 catch(SQLException ex)
686 restartConnection(ex);
687 return getArticleHeads(group, first, last);
696 public List<Long> getArticleNumbers(long gid)
702 List<Long> ids = new ArrayList<Long>();
703 this.pstmtGetArticleIDs.setLong(1, gid);
704 rs = this.pstmtGetArticleIDs.executeQuery();
707 ids.add(rs.getLong(1));
711 catch(SQLException ex)
713 restartConnection(ex);
714 return getArticleNumbers(gid);
721 restarts = 0; // Clear the restart count after successful request
726 public String getConfigValue(String key)
732 this.pstmtGetConfigValue.setString(1, key);
734 rs = this.pstmtGetConfigValue.executeQuery();
737 return rs.getString(1); // First data on index 1 not 0
744 catch(SQLException ex)
746 restartConnection(ex);
747 return getConfigValue(key);
754 restarts = 0; // Clear the restart count after successful request
759 public int getEventsCount(byte type, long start, long end, Group group)
768 this.pstmtGetEventsCount0.setInt(1, type);
769 this.pstmtGetEventsCount0.setLong(2, start);
770 this.pstmtGetEventsCount0.setLong(3, end);
771 rs = this.pstmtGetEventsCount0.executeQuery();
775 this.pstmtGetEventsCount1.setInt(1, type);
776 this.pstmtGetEventsCount1.setLong(2, start);
777 this.pstmtGetEventsCount1.setLong(3, end);
778 this.pstmtGetEventsCount1.setLong(4, group.getID());
779 rs = this.pstmtGetEventsCount1.executeQuery();
791 catch(SQLException ex)
793 restartConnection(ex);
794 return getEventsCount(type, start, end, group);
804 * Reads all Groups from the Database.
806 * @throws java.sql.SQLException
808 public List<Group> getGroups()
812 List<Group> buffer = new ArrayList<Group>();
813 Statement stmt = null;
817 stmt = conn.createStatement();
818 rs = stmt.executeQuery("SELECT * FROM groups ORDER BY name");
822 String name = rs.getString("name");
823 long id = rs.getLong("group_id");
824 int flags = rs.getInt("flags");
826 Group group = new Group(name, id, flags);
832 catch(SQLException ex)
834 restartConnection(ex);
840 stmt.close(); // Implicitely closes ResultSets
844 public String getGroupForList(InternetAddress listAddress)
851 this.pstmtGetGroupForList.setString(1, listAddress.getAddress());
853 rs = this.pstmtGetGroupForList.executeQuery();
856 return rs.getString(1);
863 catch(SQLException ex)
865 restartConnection(ex);
866 return getGroupForList(listAddress);
876 * Returns the Group that is identified by the name.
879 * @throws java.sql.SQLException
881 public Group getGroup(String name)
888 this.pstmtGetGroup0.setString(1, name);
889 rs = this.pstmtGetGroup0.executeQuery();
897 long id = rs.getLong("group_id");
898 int flags = rs.getInt("flags");
899 return new Group(name, id, flags);
902 catch(SQLException ex)
904 restartConnection(ex);
905 return getGroup(name);
914 public String getListForGroup(String group)
921 this.pstmtGetListForGroup.setString(1, group);
922 rs = this.pstmtGetListForGroup.executeQuery();
925 return rs.getString(1);
932 catch(SQLException ex)
934 restartConnection(ex);
935 return getListForGroup(group);
944 private int getMaxArticleIndex(long groupID)
951 this.pstmtGetMaxArticleIndex.setLong(1, groupID);
952 rs = this.pstmtGetMaxArticleIndex.executeQuery();
957 maxIndex = rs.getInt(1);
962 catch(SQLException ex)
964 restartConnection(ex);
965 return getMaxArticleIndex(groupID);
974 private int getMaxArticleID()
981 rs = this.pstmtGetMaxArticleID.executeQuery();
986 maxIndex = rs.getInt(1);
991 catch(SQLException ex)
993 restartConnection(ex);
994 return getMaxArticleID();
1003 public int getLastArticleNumber(Group group)
1006 ResultSet rs = null;
1010 this.pstmtGetLastArticleNumber.setLong(1, group.getID());
1011 rs = this.pstmtGetLastArticleNumber.executeQuery();
1014 return rs.getInt(1);
1021 catch(SQLException ex)
1023 restartConnection(ex);
1024 return getLastArticleNumber(group);
1033 public int getFirstArticleNumber(Group group)
1036 ResultSet rs = null;
1039 this.pstmtGetFirstArticleNumber.setLong(1, group.getID());
1040 rs = this.pstmtGetFirstArticleNumber.executeQuery();
1043 return rs.getInt(1);
1050 catch(SQLException ex)
1052 restartConnection(ex);
1053 return getFirstArticleNumber(group);
1063 * Returns a group name identified by the given id.
1066 * @throws java.sql.SQLException
1068 public String getGroup(int id)
1071 ResultSet rs = null;
1075 this.pstmtGetGroup1.setInt(1, id);
1076 rs = this.pstmtGetGroup1.executeQuery();
1080 return rs.getString(1);
1087 catch(SQLException ex)
1089 restartConnection(ex);
1090 return getGroup(id);
1099 public double getNumberOfEventsPerHour(int key, long gid)
1102 String gidquery = "";
1105 gidquery = " AND group_id = " + gid;
1108 Statement stmt = null;
1109 ResultSet rs = null;
1113 stmt = this.conn.createStatement();
1114 rs = stmt.executeQuery("SELECT Count(*) / (Max(event_time) - Min(event_time))" +
1115 " * 1000 * 60 * 60 FROM events WHERE event_key = " + key + gidquery);
1119 restarts = 0; // reset error count
1120 return rs.getDouble(1);
1127 catch(SQLException ex)
1129 restartConnection(ex);
1130 return getNumberOfEventsPerHour(key, gid);
1146 public int getPostingsCount(String groupname)
1149 ResultSet rs = null;
1153 this.pstmtGetPostingsCount.setString(1, groupname);
1154 rs = this.pstmtGetPostingsCount.executeQuery();
1157 return rs.getInt(1);
1161 Log.msg("Warning: Count on postings return nothing!", true);
1165 catch(SQLException ex)
1167 restartConnection(ex);
1168 return getPostingsCount(groupname);
1177 public List<Subscription> getSubscriptions(int feedtype)
1180 ResultSet rs = null;
1184 List<Subscription> subs = new ArrayList<Subscription>();
1185 this.pstmtGetSubscriptions.setInt(1, feedtype);
1186 rs = this.pstmtGetSubscriptions.executeQuery();
1190 String host = rs.getString("host");
1191 String group = rs.getString("name");
1192 int port = rs.getInt("port");
1193 subs.add(new Subscription(host, port, feedtype, group));
1198 catch(SQLException ex)
1200 restartConnection(ex);
1201 return getSubscriptions(feedtype);
1211 * Checks if there is an article with the given messageid in the Database.
1214 * @throws java.sql.SQLException
1216 public boolean isArticleExisting(String messageID)
1219 ResultSet rs = null;
1223 this.pstmtIsArticleExisting.setString(1, messageID);
1224 rs = this.pstmtIsArticleExisting.executeQuery();
1225 return rs.next() && rs.getInt(1) == 1;
1227 catch(SQLException ex)
1229 restartConnection(ex);
1230 return isArticleExisting(messageID);
1240 * Checks if there is a group with the given name in the Database.
1243 * @throws java.sql.SQLException
1245 public boolean isGroupExisting(String name)
1248 ResultSet rs = null;
1252 this.pstmtIsGroupExisting.setString(1, name);
1253 rs = this.pstmtIsGroupExisting.executeQuery();
1256 catch(SQLException ex)
1258 restartConnection(ex);
1259 return isGroupExisting(name);
1268 public void setConfigValue(String key, String value)
1273 conn.setAutoCommit(false);
1274 this.pstmtSetConfigValue0.setString(1, key);
1275 this.pstmtSetConfigValue0.execute();
1276 this.pstmtSetConfigValue1.setString(1, key);
1277 this.pstmtSetConfigValue1.setString(2, value);
1278 this.pstmtSetConfigValue1.execute();
1280 conn.setAutoCommit(true);
1282 catch(SQLException ex)
1284 restartConnection(ex);
1285 setConfigValue(key, value);
1290 * Closes the Database connection.
1292 public void shutdown()
1295 if(this.conn != null)
1301 private void restartConnection(SQLException cause)
1305 Log.msg(Thread.currentThread()
1306 + ": Database connection was closed (restart " + restarts + ").", false);
1308 if(restarts >= MAX_RESTARTS)
1310 // Delete the current, probably broken Database instance.
1311 // So no one can use the instance any more.
1312 Database.instances.remove(Thread.currentThread());
1314 // Throw the exception upwards
1320 Thread.sleep(1500L * restarts);
1322 catch(InterruptedException ex)
1324 Log.msg("Interrupted: " + ex.getMessage(), false);
1327 // Try to properly close the old database connection
1330 if(this.conn != null)
1335 catch(SQLException ex)
1337 Log.msg(ex.getMessage(), true);
1342 // Try to reinitialize database connection
1345 catch(SQLException ex)
1347 Log.msg(ex.getMessage(), true);
1348 restartConnection(ex);