org.apache.jackrabbit.core.persistence.bundle.util
Class ConnectionRecoveryManager

java.lang.Object
  extended by org.apache.jackrabbit.core.persistence.bundle.util.ConnectionRecoveryManager

public class ConnectionRecoveryManager
extends Object

This class provides methods to get a database connection and to execute SQL statements. It also contains reconnection logic. If the connection has been closed with the close() method, then a call to any public method except for setAutoReconnect(boolean) will try to reestablish the connection, but only if the autoReconnect equals true.

The reconnection attempt can either be blocking or non-blocking, which is configured during construction. In the latter case a fixed number of reconnection attempts is made. When the reconnection failed an SQLException is thrown.

The methods of this class that execute SQL statements automatically call close() when they encounter an SQLException.


Nested Class Summary
static class ConnectionRecoveryManager.StreamWrapper
          A wrapper for a binary stream that includes the size of the stream.
 
Field Summary
static int TRIALS
          Number of reconnection attempts per method call.
 
Constructor Summary
ConnectionRecoveryManager(boolean block, String driver, String url, String user, String password)
          Creates a ConnectionRecoveryManager and establishes a database Connection using the driver, user, password and url arguments.
 
Method Summary
 void close()
          Closes all resources held by this ConnectionRecoveryManager.
 ResultSet executeQuery(String sql)
          Executes the given SQL query.
 PreparedStatement executeStmt(String sql, Object[] params)
          Executes the given SQL statement with the specified parameters.
 PreparedStatement executeStmt(String sql, Object[] params, boolean returnGeneratedKeys, int maxRows)
          Executes the given SQL statement with the specified parameters.
 Connection getConnection()
          Gets the database connection that is managed.
 void setAutoReconnect(boolean autoReconnect)
          Starts a transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRIALS

public static final int TRIALS
Number of reconnection attempts per method call. Only used if block == false.

See Also:
Constant Field Values
Constructor Detail

ConnectionRecoveryManager

public ConnectionRecoveryManager(boolean block,
                                 String driver,
                                 String url,
                                 String user,
                                 String password)
                          throws RepositoryException
Creates a ConnectionRecoveryManager and establishes a database Connection using the driver, user, password and url arguments.

By default, the connection is in auto-commit mode, and this manager tries to reconnect if the connection is lost.

Parameters:
block - whether this class should block until the connection can be recovered
driver - the driver to use for the connection
url - the url to use for the connection
user - the user to use for the connection
password - the password to use for the connection
Throws:
RepositoryException - if the database driver could not be loaded
Method Detail

getConnection

public Connection getConnection()
                         throws SQLException,
                                RepositoryException
Gets the database connection that is managed. If the connection has been closed, and autoReconnect==true then an attempt is made to reestablish the connection.

Returns:
the database connection that is managed
Throws:
SQLException - on error
RepositoryException - if the database driver could not be loaded

setAutoReconnect

public void setAutoReconnect(boolean autoReconnect)
Starts a transaction. I.e., the auto-commit is set to false, and the manager does not try to reconnect if the connection is lost. This method call should be followed by a call to endTransaction.

Throws:
SQLException - on error

executeQuery

public ResultSet executeQuery(String sql)
                       throws SQLException,
                              RepositoryException
Executes the given SQL query. Retries once or blocks (when the block parameter has been set to true on construction) if this fails and autoReconnect is enabled.

Parameters:
sql - the SQL query to execute
Returns:
the executed ResultSet
Throws:
SQLException - on error
RepositoryException - if the database driver could not be loaded

executeStmt

public PreparedStatement executeStmt(String sql,
                                     Object[] params)
                              throws SQLException,
                                     RepositoryException
Executes the given SQL statement with the specified parameters.

Parameters:
sql - statement to execute
params - parameters to set
Returns:
the Statement object that had been executed
Throws:
SQLException - if an error occurs
RepositoryException - if the database driver could not be loaded

executeStmt

public PreparedStatement executeStmt(String sql,
                                     Object[] params,
                                     boolean returnGeneratedKeys,
                                     int maxRows)
                              throws SQLException,
                                     RepositoryException
Executes the given SQL statement with the specified parameters.

Parameters:
sql - statement to execute
params - parameters to set
returnGeneratedKeys - if the statement should return auto generated keys
maxRows - the maximum number of rows to return (0 for all rows)
Returns:
the Statement object that had been executed
Throws:
SQLException - if an error occurs
RepositoryException - if the database driver could not be loaded

close

public void close()
Closes all resources held by this ConnectionRecoveryManager. An ongoing transaction is discarded.



Copyright © 2004-2008 The Apache Software Foundation. All Rights Reserved.