src/org/sonews/storage/impl/JDBCDatabase.java
changeset 45 7e24949b87b0
parent 44 5d7d1adf387f
child 48 b78e77619152
     1.1 --- a/src/org/sonews/storage/impl/JDBCDatabase.java	Tue Jun 07 11:55:22 2011 +0200
     1.2 +++ b/src/org/sonews/storage/impl/JDBCDatabase.java	Sat Sep 10 18:18:05 2011 +0200
     1.3 @@ -27,6 +27,7 @@
     1.4  import java.util.ArrayList;
     1.5  import java.util.Enumeration;
     1.6  import java.util.List;
     1.7 +import java.util.logging.Level;
     1.8  import java.util.regex.Matcher;
     1.9  import java.util.regex.Pattern;
    1.10  import java.util.regex.PatternSyntaxException;
    1.11 @@ -97,6 +98,29 @@
    1.12  	/** How many times the database connection was reinitialized */
    1.13  	protected int restarts = 0;
    1.14  
    1.15 +	protected void prepareAddGroupStatement() throws SQLException {
    1.16 +		this.pstmtAddGroup0 = conn.prepareStatement(
    1.17 +				"INSERT INTO groups (name, flags) VALUES (?, ?)");
    1.18 +	}
    1.19 +
    1.20 +	protected void prepareCountGroupsStatement() throws SQLException {
    1.21 +		this.pstmtCountGroups = conn.prepareStatement(
    1.22 +				"SELECT Count(group_id) FROM groups WHERE "
    1.23 +				+ "flags & " + Channel.DELETED + " = 0");
    1.24 +	}
    1.25 +
    1.26 +	protected void prepareGetPostingsCountStatement() throws SQLException {
    1.27 +		this.pstmtGetPostingsCount = conn.prepareStatement(
    1.28 +				"SELECT Count(*) FROM postings NATURAL JOIN groups "
    1.29 +				+ "WHERE groups.name = ?");
    1.30 +	}
    1.31 +
    1.32 +	protected void prepareGetSubscriptionsStatement() throws SQLException {
    1.33 +		this.pstmtGetSubscriptions = conn.prepareStatement(
    1.34 +				"SELECT host, port, name FROM peers NATURAL JOIN "
    1.35 +				+ "peer_subscriptions NATURAL JOIN groups WHERE feedtype = ?");
    1.36 +	}
    1.37 +
    1.38  	/**
    1.39  	 * Rises the database: reconnect and recreate all prepared statements.
    1.40  	 * @throws java.lang.SQLException
    1.41 @@ -137,17 +161,14 @@
    1.42  				"INSERT INTO events VALUES (?, ?, ?)");
    1.43  
    1.44  			// Prepare statement for method addGroup()
    1.45 -			this.pstmtAddGroup0 = conn.prepareStatement(
    1.46 -				"INSERT INTO groups (name, flags) VALUES (?, ?)");
    1.47 +			prepareAddGroupStatement();
    1.48  
    1.49  			// Prepare statement for method countArticles()
    1.50  			this.pstmtCountArticles = conn.prepareStatement(
    1.51  				"SELECT Count(article_id) FROM article_ids");
    1.52  
    1.53  			// Prepare statement for method countGroups()
    1.54 -			this.pstmtCountGroups = conn.prepareStatement(
    1.55 -				"SELECT Count(group_id) FROM groups WHERE "
    1.56 -				+ "flags & " + Channel.DELETED + " = 0");
    1.57 +			prepareCountGroupsStatement();
    1.58  
    1.59  			// Prepare statements for method delete(article)
    1.60  			this.pstmtDeleteArticle0 = conn.prepareStatement(
    1.61 @@ -254,14 +275,10 @@
    1.62  				"SELECT Min(article_index) FROM postings WHERE group_id = ?");
    1.63  
    1.64  			// Prepare statement for method getPostingsCount()
    1.65 -			this.pstmtGetPostingsCount = conn.prepareStatement(
    1.66 -				"SELECT Count(*) FROM postings NATURAL JOIN groups "
    1.67 -				+ "WHERE groups.name = ?");
    1.68 +			prepareGetPostingsCountStatement();
    1.69  
    1.70  			// Prepare statement for method getSubscriptions()
    1.71 -			this.pstmtGetSubscriptions = conn.prepareStatement(
    1.72 -				"SELECT host, port, name FROM peers NATURAL JOIN "
    1.73 -				+ "peer_subscriptions NATURAL JOIN groups WHERE feedtype = ?");
    1.74 +			prepareGetSubscriptionsStatement();
    1.75  
    1.76  			// Prepare statement for method isArticleExisting()
    1.77  			this.pstmtIsArticleExisting = conn.prepareStatement(
    1.78 @@ -1364,12 +1381,12 @@
    1.79  		}
    1.80  	}
    1.81  
    1.82 -	private void restartConnection(SQLException cause)
    1.83 +	protected void restartConnection(SQLException cause)
    1.84  		throws StorageBackendException
    1.85  	{
    1.86  		restarts++;
    1.87 -		Log.get().severe(Thread.currentThread()
    1.88 -			+ ": Database connection was closed (restart " + restarts + ").");
    1.89 +		Log.get().log(Level.SEVERE, Thread.currentThread()
    1.90 +			+ ": Database connection was closed (restart " + restarts + ").", cause);
    1.91  
    1.92  		if (restarts >= MAX_RESTARTS) {
    1.93  			// Delete the current, probably broken JDBCDatabase instance.