Hooray... sonews/0.5.0 final
HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
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 ResultSet rs = this.pstmtDeleteArticle0.executeQuery();
488 // We trust the ON DELETE CASCADE functionality to delete
489 // orphaned references
492 this.conn.setAutoCommit(true);
494 catch(SQLException ex)
500 public Article getArticle(String messageID)
506 pstmtGetArticle0.setString(1, messageID);
507 rs = pstmtGetArticle0.executeQuery();
515 String body = new String(rs.getBytes("body"));
516 String headers = getArticleHeaders(rs.getInt("article_id"));
517 return new Article(headers, body);
520 catch(SQLException ex)
522 restartConnection(ex);
523 return getArticle(messageID);
530 restarts = 0; // Reset error count
536 * Retrieves an article by its ID.
539 * @throws java.sql.SQLException
541 public Article getArticle(long articleIndex, long gid)
548 this.pstmtGetArticle1.setLong(1, articleIndex);
549 this.pstmtGetArticle1.setLong(2, gid);
551 rs = this.pstmtGetArticle1.executeQuery();
555 String body = new String(rs.getBytes("body"));
556 String headers = getArticleHeaders(rs.getInt("article_id"));
557 return new Article(headers, body);
564 catch(SQLException ex)
566 restartConnection(ex);
567 return getArticle(articleIndex, gid);
579 public String getArticleHeaders(long articleID)
586 this.pstmtGetArticleHeaders.setLong(1, articleID);
587 rs = this.pstmtGetArticleHeaders.executeQuery();
589 StringBuilder buf = new StringBuilder();
594 buf.append(rs.getString(1)); // key
596 String foldedValue = MimeUtility.fold(0, rs.getString(2));
597 buf.append(foldedValue); // value
609 return buf.toString();
611 catch(SQLException ex)
613 restartConnection(ex);
614 return getArticleHeaders(articleID);
623 public long getArticleIndex(Article article, Group group)
630 this.pstmtGetArticleIndex.setString(1, article.getMessageID());
631 this.pstmtGetArticleIndex.setLong(2, group.getID());
633 rs = this.pstmtGetArticleIndex.executeQuery();
636 return rs.getLong(1);
643 catch(SQLException ex)
645 restartConnection(ex);
646 return getArticleIndex(article, group);
656 * Returns a list of Long/Article Pairs.
657 * @throws java.sql.SQLException
659 public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, int first, int last)
666 this.pstmtGetArticleHeads.setLong(1, group.getID());
667 this.pstmtGetArticleHeads.setInt(2, first);
668 this.pstmtGetArticleHeads.setInt(3, last);
669 rs = pstmtGetArticleHeads.executeQuery();
671 List<Pair<Long, ArticleHead>> articles
672 = new ArrayList<Pair<Long, ArticleHead>>();
676 long aid = rs.getLong("article_id");
677 long aidx = rs.getLong("article_index");
678 String headers = getArticleHeaders(aid);
679 articles.add(new Pair<Long, ArticleHead>(aidx,
680 new ArticleHead(headers)));
685 catch(SQLException ex)
687 restartConnection(ex);
688 return getArticleHeads(group, first, last);
697 public List<Long> getArticleNumbers(long gid)
703 List<Long> ids = new ArrayList<Long>();
704 this.pstmtGetArticleIDs.setLong(1, gid);
705 rs = this.pstmtGetArticleIDs.executeQuery();
708 ids.add(rs.getLong(1));
712 catch(SQLException ex)
714 restartConnection(ex);
715 return getArticleNumbers(gid);
722 restarts = 0; // Clear the restart count after successful request
727 public String getConfigValue(String key)
733 this.pstmtGetConfigValue.setString(1, key);
735 rs = this.pstmtGetConfigValue.executeQuery();
738 return rs.getString(1); // First data on index 1 not 0
745 catch(SQLException ex)
747 restartConnection(ex);
748 return getConfigValue(key);
755 restarts = 0; // Clear the restart count after successful request
760 public int getEventsCount(byte type, long start, long end, Group group)
769 this.pstmtGetEventsCount0.setInt(1, type);
770 this.pstmtGetEventsCount0.setLong(2, start);
771 this.pstmtGetEventsCount0.setLong(3, end);
772 rs = this.pstmtGetEventsCount0.executeQuery();
776 this.pstmtGetEventsCount1.setInt(1, type);
777 this.pstmtGetEventsCount1.setLong(2, start);
778 this.pstmtGetEventsCount1.setLong(3, end);
779 this.pstmtGetEventsCount1.setLong(4, group.getID());
780 rs = this.pstmtGetEventsCount1.executeQuery();
792 catch(SQLException ex)
794 restartConnection(ex);
795 return getEventsCount(type, start, end, group);
805 * Reads all Groups from the Database.
807 * @throws java.sql.SQLException
809 public List<Group> getGroups()
813 List<Group> buffer = new ArrayList<Group>();
814 Statement stmt = null;
818 stmt = conn.createStatement();
819 rs = stmt.executeQuery("SELECT * FROM groups ORDER BY name");
823 String name = rs.getString("name");
824 long id = rs.getLong("group_id");
825 int flags = rs.getInt("flags");
827 Group group = new Group(name, id, flags);
833 catch(SQLException ex)
835 restartConnection(ex);
841 stmt.close(); // Implicitely closes ResultSets
845 public String getGroupForList(InternetAddress listAddress)
852 this.pstmtGetGroupForList.setString(1, listAddress.getAddress());
854 rs = this.pstmtGetGroupForList.executeQuery();
857 return rs.getString(1);
864 catch(SQLException ex)
866 restartConnection(ex);
867 return getGroupForList(listAddress);
877 * Returns the Group that is identified by the name.
880 * @throws java.sql.SQLException
882 public Group getGroup(String name)
889 this.pstmtGetGroup0.setString(1, name);
890 rs = this.pstmtGetGroup0.executeQuery();
898 long id = rs.getLong("group_id");
899 int flags = rs.getInt("flags");
900 return new Group(name, id, flags);
903 catch(SQLException ex)
905 restartConnection(ex);
906 return getGroup(name);
915 public String getListForGroup(String group)
922 this.pstmtGetListForGroup.setString(1, group);
923 rs = this.pstmtGetListForGroup.executeQuery();
926 return rs.getString(1);
933 catch(SQLException ex)
935 restartConnection(ex);
936 return getListForGroup(group);
945 private int getMaxArticleIndex(long groupID)
952 this.pstmtGetMaxArticleIndex.setLong(1, groupID);
953 rs = this.pstmtGetMaxArticleIndex.executeQuery();
958 maxIndex = rs.getInt(1);
963 catch(SQLException ex)
965 restartConnection(ex);
966 return getMaxArticleIndex(groupID);
975 private int getMaxArticleID()
982 rs = this.pstmtGetMaxArticleID.executeQuery();
987 maxIndex = rs.getInt(1);
992 catch(SQLException ex)
994 restartConnection(ex);
995 return getMaxArticleID();
1004 public int getLastArticleNumber(Group group)
1007 ResultSet rs = null;
1011 this.pstmtGetLastArticleNumber.setLong(1, group.getID());
1012 rs = this.pstmtGetLastArticleNumber.executeQuery();
1015 return rs.getInt(1);
1022 catch(SQLException ex)
1024 restartConnection(ex);
1025 return getLastArticleNumber(group);
1034 public int getFirstArticleNumber(Group group)
1037 ResultSet rs = null;
1040 this.pstmtGetFirstArticleNumber.setLong(1, group.getID());
1041 rs = this.pstmtGetFirstArticleNumber.executeQuery();
1044 return rs.getInt(1);
1051 catch(SQLException ex)
1053 restartConnection(ex);
1054 return getFirstArticleNumber(group);
1064 * Returns a group name identified by the given id.
1067 * @throws java.sql.SQLException
1069 public String getGroup(int id)
1072 ResultSet rs = null;
1076 this.pstmtGetGroup1.setInt(1, id);
1077 rs = this.pstmtGetGroup1.executeQuery();
1081 return rs.getString(1);
1088 catch(SQLException ex)
1090 restartConnection(ex);
1091 return getGroup(id);
1100 public double getNumberOfEventsPerHour(int key, long gid)
1103 String gidquery = "";
1106 gidquery = " AND group_id = " + gid;
1109 Statement stmt = null;
1110 ResultSet rs = null;
1114 stmt = this.conn.createStatement();
1115 rs = stmt.executeQuery("SELECT Count(*) / (Max(event_time) - Min(event_time))" +
1116 " * 1000 * 60 * 60 FROM events WHERE event_key = " + key + gidquery);
1120 restarts = 0; // reset error count
1121 return rs.getDouble(1);
1128 catch(SQLException ex)
1130 restartConnection(ex);
1131 return getNumberOfEventsPerHour(key, gid);
1147 public int getPostingsCount(String groupname)
1150 ResultSet rs = null;
1154 this.pstmtGetPostingsCount.setString(1, groupname);
1155 rs = this.pstmtGetPostingsCount.executeQuery();
1158 return rs.getInt(1);
1162 Log.msg("Warning: Count on postings return nothing!", true);
1166 catch(SQLException ex)
1168 restartConnection(ex);
1169 return getPostingsCount(groupname);
1178 public List<Subscription> getSubscriptions(int feedtype)
1181 ResultSet rs = null;
1185 List<Subscription> subs = new ArrayList<Subscription>();
1186 this.pstmtGetSubscriptions.setInt(1, feedtype);
1187 rs = this.pstmtGetSubscriptions.executeQuery();
1191 String host = rs.getString("host");
1192 String group = rs.getString("name");
1193 int port = rs.getInt("port");
1194 subs.add(new Subscription(host, port, feedtype, group));
1199 catch(SQLException ex)
1201 restartConnection(ex);
1202 return getSubscriptions(feedtype);
1212 * Checks if there is an article with the given messageid in the Database.
1215 * @throws java.sql.SQLException
1217 public boolean isArticleExisting(String messageID)
1220 ResultSet rs = null;
1224 this.pstmtIsArticleExisting.setString(1, messageID);
1225 rs = this.pstmtIsArticleExisting.executeQuery();
1226 return rs.next() && rs.getInt(1) == 1;
1228 catch(SQLException ex)
1230 restartConnection(ex);
1231 return isArticleExisting(messageID);
1241 * Checks if there is a group with the given name in the Database.
1244 * @throws java.sql.SQLException
1246 public boolean isGroupExisting(String name)
1249 ResultSet rs = null;
1253 this.pstmtIsGroupExisting.setString(1, name);
1254 rs = this.pstmtIsGroupExisting.executeQuery();
1257 catch(SQLException ex)
1259 restartConnection(ex);
1260 return isGroupExisting(name);
1269 public void setConfigValue(String key, String value)
1274 conn.setAutoCommit(false);
1275 this.pstmtSetConfigValue0.setString(1, key);
1276 this.pstmtSetConfigValue0.execute();
1277 this.pstmtSetConfigValue1.setString(1, key);
1278 this.pstmtSetConfigValue1.setString(2, value);
1279 this.pstmtSetConfigValue1.execute();
1281 conn.setAutoCommit(true);
1283 catch(SQLException ex)
1285 restartConnection(ex);
1286 setConfigValue(key, value);
1291 * Closes the Database connection.
1293 public void shutdown()
1296 if(this.conn != null)
1302 private void restartConnection(SQLException cause)
1306 Log.msg(Thread.currentThread()
1307 + ": Database connection was closed (restart " + restarts + ").", false);
1309 if(restarts >= MAX_RESTARTS)
1311 // Delete the current, probably broken Database instance.
1312 // So no one can use the instance any more.
1313 Database.instances.remove(Thread.currentThread());
1315 // Throw the exception upwards
1321 Thread.sleep(1500L * restarts);
1323 catch(InterruptedException ex)
1325 Log.msg("Interrupted: " + ex.getMessage(), false);
1328 // Try to properly close the old database connection
1331 if(this.conn != null)
1336 catch(SQLException ex)
1338 Log.msg(ex.getMessage(), true);
1343 // Try to reinitialize database connection
1346 catch(SQLException ex)
1348 Log.msg(ex.getMessage(), true);
1349 restartConnection(ex);