Package org.jivesoftware.database
Class DbConnectionManager
java.lang.Object
org.jivesoftware.database.DbConnectionManager
Central manager of database connections. All methods are static so that they
can be easily accessed throughout the classes in the database package.
This class also provides a set of utility methods that abstract out operations that may not work on all databases such as setting the max number or rows that a query should return.
- Author:
- Jive Software
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
A class that identifies the type of the database that Jive is connected to. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Closes a database connection (returning the connection to the connection pool).static void
closeConnection
(ResultSet rs, Statement stmt, Connection con) Closes a result set, statement and database connection (returning the connection to the connection pool).static void
closeConnection
(Statement stmt, Connection con) Closes a statement and database connection (returning the connection to the connection pool).static void
Closes a result set.static void
closeStatement
(ResultSet rs, Statement stmt) Closes a statement and a result set.static void
closeStatement
(Statement stmt) Closes a statement.static void
closeTransactionConnection
(Connection con, boolean abortTransaction) Closes a Connection.static void
closeTransactionConnection
(PreparedStatement pstmt, Connection con, boolean abortTransaction) Closes a PreparedStatement and Connection.static PreparedStatement
Creates a scroll insensitive PreparedStatement if the JDBC driver supports it, or a normal PreparedStatement otherwise.static void
Destroys the currennt connection provider.static void
fastcloseStmt
(PreparedStatement pstmt) Closes a statement.static void
fastcloseStmt
(ResultSet rs, PreparedStatement pstmt) Closes a statement and a result set.static Connection
Returns a database connection from the currently active connection provider.static ConnectionProvider
Returns the current connection provider.Returns the database type.static String
static String
getLargeTextField
(ResultSet rs, int columnIndex) Retrives a large text column from a result set, automatically performing streaming if the JDBC driver requires it.static SchemaManager
Returns a SchemaManager instance, which can be used to manage the database schema information for Openfire and plugins.static String
getTestSQL
(String driver) static Connection
Returns a Connection from the currently active connection provider that is ready to participate in transactions (auto commit is set to false).static boolean
static boolean
static boolean
static boolean
static boolean
Returns true if connection profiling is turned on.static boolean
static boolean
static boolean
static boolean
static boolean
static void
limitRowsAndFetchSize
(PreparedStatement pstmt, int startIndex, int numResults) Limits the number of the results in a result set (to startIndex + numResults).static void
scrollResultSet
(ResultSet rs, int rowNumber) Scrolls forward in a result set the specified number of rows.static void
setConnectionProvider
(ConnectionProvider provider) Sets the connection provider.static void
setFetchSize
(PreparedStatement pstmt, int fetchSize) Sets the number of rows that the JDBC driver should buffer at a time.static void
setFetchSize
(ResultSet rs, int fetchSize) Sets the number of rows that the JDBC driver should buffer at a time.static void
setLargeTextField
(PreparedStatement pstmt, int parameterIndex, String value) Sets a large text column in a result set, automatically performing streaming if the JDBC driver requires it.static void
setMaxRows
(Statement stmt, int maxRows) Sets the max number of rows that should be returned from executing a statement.static void
setProfilingEnabled
(boolean enable) Turns connection profiling on or off.static boolean
testConnection
(Map<String, String> errors) Attempts to create a connection to the database and execute a query.
-
Constructor Details
-
DbConnectionManager
public DbConnectionManager()
-
-
Method Details
-
testConnection
Attempts to create a connection to the database and execute a query.- Parameters:
errors
- A map populated with errors if they occur.- Returns:
- true if the test was successful, otherwise false.
-
getConnection
Returns a database connection from the currently active connection provider. An exception will be thrown if no connection was found. (auto commit is set to true).- Returns:
- a connection.
- Throws:
SQLException
- if a SQL exception occurs or no connection was found.
-
getTransactionConnection
Returns a Connection from the currently active connection provider that is ready to participate in transactions (auto commit is set to false).- Returns:
- a connection with transactions enabled.
- Throws:
SQLException
- if a SQL exception occurs.
-
closeTransactionConnection
public static void closeTransactionConnection(PreparedStatement pstmt, Connection con, boolean abortTransaction) Closes a PreparedStatement and Connection. However, it first rolls back the transaction or commits it depending on the value ofabortTransaction
.- Parameters:
pstmt
- the prepared statement to close.con
- the connection to close.abortTransaction
- true if the transaction should be rolled back.
-
closeTransactionConnection
Closes a Connection. However, it first rolls back the transaction or commits it depending on the value ofabortTransaction
.- Parameters:
con
- the connection to close.abortTransaction
- true if the transaction should be rolled back.
-
closeResultSet
Closes a result set. This method should be called within the finally section of your database logic, as in the following example:public void doSomething(Connection con) { ResultSet rs = null; PreparedStatement pstmt = null; try { pstmt = con.prepareStatement("select * from blah"); rs = pstmt.executeQuery(); .... } catch (SQLException sqle) { Log.error(sqle.getMessage(), sqle); } finally { ConnectionManager.closeResultSet(rs); ConnectionManager.closePreparedStatement(pstmt); } }
- Parameters:
rs
- the result set to close.
-
closeStatement
Closes a statement. This method should be called within the finally section of your database logic, as in the following example:public void doSomething(Connection con) { PreparedStatement pstmt = null; try { pstmt = con.prepareStatement("select * from blah"); .... } catch (SQLException sqle) { Log.error(sqle.getMessage(), sqle); } finally { ConnectionManager.closeStatement(pstmt); } }
- Parameters:
stmt
- the statement.
-
closeStatement
Closes a statement and a result set. This method should be called within the finally section of your database logic, as in the following example:public void doSomething(Connection con) { PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = con.prepareStatement("select * from blah"); rs = ... .... } catch (SQLException sqle) { Log.error(sqle.getMessage(), sqle); } finally { ConnectionManager.closeStatement(rs, pstmt); } }
- Parameters:
rs
- the result set to closestmt
- the statement.
-
fastcloseStmt
Closes a statement. This method should be called within the try section of your database logic when you reuse a statement. It may throws an exception, so don't place it in the finally section.
Example:public void doSomething(Connection con) { PreparedStatement pstmt = null; try { pstmt = con.prepareStatement("select * from dual"); pstmt.executeUpdate(); ... ConnectionManager.fastcloseStmt(pstmt); pstmt = con.prepareStatement("select * from blah"); ... } ... }
- Parameters:
pstmt
- the statement to close.- Throws:
SQLException
- if an exception occurs closing the statement
-
fastcloseStmt
Closes a statement and a result set. This method should be called within the try section of your database logic when you reuse a statement. It may throw an exception, so don't place it in the finally section.
Example:public void doSomething(Connection con) { PreparedStatement pstmt = null; try { pstmt = con.prepareStatement("select * from blah"); rs = pstmt.executeQuery(); ... ConnectionManager.fastcloseStmt(rs, pstmt); pstmt = con.prepareStatement("select * from blah"); ... } ... }
- Parameters:
rs
- The result set to closepstmt
- the statement to close.- Throws:
SQLException
- if an exception occurs closing the result set or statement
-
closeConnection
Closes a result set, statement and database connection (returning the connection to the connection pool). This method should be called within the finally section of your database logic, as in the following example:Connection con = null; PrepatedStatment pstmt = null; ResultSet rs = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement("select * from blah"); rs = psmt.executeQuery(); .... } catch (SQLException sqle) { Log.error(sqle.getMessage(), sqle); } finally { ConnectionManager.closeConnection(rs, pstmt, con); }
- Parameters:
rs
- the result set.stmt
- the statement.con
- the connection.
-
closeConnection
Closes a statement and database connection (returning the connection to the connection pool). This method should be called within the finally section of your database logic, as in the following example:Connection con = null; PrepatedStatment pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement("select * from blah"); .... } catch (SQLException sqle) { Log.error(sqle.getMessage(), sqle); } finally { DbConnectionManager.closeConnection(pstmt, con); }
- Parameters:
stmt
- the statement.con
- the connection.
-
closeConnection
Closes a database connection (returning the connection to the connection pool). Any statements associated with the connection should be closed before calling this method. This method should be called within the finally section of your database logic, as in the following example:Connection con = null; try { con = ConnectionManager.getConnection(); .... } catch (SQLException sqle) { Log.error(sqle.getMessage(), sqle); } finally { DbConnectionManager.closeConnection(con); }
- Parameters:
con
- the connection.
-
createScrollablePreparedStatement
public static PreparedStatement createScrollablePreparedStatement(Connection con, String sql) throws SQLException Creates a scroll insensitive PreparedStatement if the JDBC driver supports it, or a normal PreparedStatement otherwise.- Parameters:
con
- the database connection.sql
- the SQL to create the PreparedStatement with.- Returns:
- a PreparedStatement
- Throws:
SQLException
- if an error occurs.
-
scrollResultSet
Scrolls forward in a result set the specified number of rows. If the JDBC driver supports the feature, the cursor will be moved directly. Otherwise, we scroll through results one by one manually by callingrs.next()
.- Parameters:
rs
- the ResultSet object to scroll.rowNumber
- the row number to scroll forward to.- Throws:
SQLException
- if an error occurs.
-
limitRowsAndFetchSize
Limits the number of the results in a result set (to startIndex + numResults). Sets the fetch size depending on the features of the JDBC driver and make sure that the size is not bigger than 500.- Parameters:
pstmt
- the PreparedStatementstartIndex
- the first row with interesting datanumResults
- the number of interesting results
-
setFetchSize
Sets the number of rows that the JDBC driver should buffer at a time. The operation is automatically bypassed if Openfire knows that the the JDBC driver or database doesn't support it.- Parameters:
pstmt
- the PreparedStatement to set the fetch size for.fetchSize
- the fetchSize.
-
getConnectionProvider
Returns the current connection provider. The only case in which this method should be called is if more information about the current connection provider is needed. Database connections should always be obtained by calling the getConnection method of this class.- Returns:
- the connection provider.
-
setConnectionProvider
Sets the connection provider. The old provider (if it exists) is shut down before the new one is started. A connection provider should not be started before being passed to the connection manager because the manager will call the start() method automatically.- Parameters:
provider
- the ConnectionProvider that the manager should obtain connections from.
-
destroyConnectionProvider
public static void destroyConnectionProvider()Destroys the currennt connection provider. Future calls togetConnectionProvider()
will returnnull
until a new ConnectionProvider is set, or one is automatically loaded by a call togetConnection()
. -
getLargeTextField
Retrives a large text column from a result set, automatically performing streaming if the JDBC driver requires it. This is necessary because different JDBC drivers have different capabilities and methods for retrieving large text values.- Parameters:
rs
- the ResultSet to retrieve the text field from.columnIndex
- the column in the ResultSet of the text field.- Returns:
- the String value of the text field.
- Throws:
SQLException
- if an SQL exception occurs.
-
setLargeTextField
public static void setLargeTextField(PreparedStatement pstmt, int parameterIndex, String value) throws SQLException Sets a large text column in a result set, automatically performing streaming if the JDBC driver requires it. This is necessary because different JDBC drivers have different capabilities and methods for setting large text values.- Parameters:
pstmt
- the PreparedStatement to set the text field in.parameterIndex
- the index corresponding to the text field.value
- the String to set.- Throws:
SQLException
- if an SQL exception occurs.
-
setMaxRows
Sets the max number of rows that should be returned from executing a statement. The operation is automatically bypassed if Jive knows that the the JDBC driver or database doesn't support it.- Parameters:
stmt
- the Statement to set the max number of rows for.maxRows
- the max number of rows to return.
-
setFetchSize
Sets the number of rows that the JDBC driver should buffer at a time. The operation is automatically bypassed if Jive knows that the the JDBC driver or database doesn't support it.- Parameters:
rs
- the ResultSet to set the fetch size for.fetchSize
- the fetchSize.
-
getSchemaManager
Returns a SchemaManager instance, which can be used to manage the database schema information for Openfire and plugins.- Returns:
- a SchemaManager instance.
-
getDatabaseType
Returns the database type. The possible types are constants of the DatabaseType class. Any database that doesn't have its own constant falls into the "Other" category.- Returns:
- the database type.
-
isProfilingEnabled
public static boolean isProfilingEnabled()Returns true if connection profiling is turned on. You can collect profiling statistics by using the static methods of the ProfiledConnection class.- Returns:
- true if connection profiling is enabled.
-
setProfilingEnabled
public static void setProfilingEnabled(boolean enable) Turns connection profiling on or off. You can collect profiling statistics by using the static methods of the ProfiledConnection class.- Parameters:
enable
- true to enable profiling; false to disable.
-
isTransactionsSupported
public static boolean isTransactionsSupported() -
isStreamTextRequired
public static boolean isStreamTextRequired() -
isMaxRowsSupported
public static boolean isMaxRowsSupported() -
isFetchSizeSupported
public static boolean isFetchSizeSupported() -
isPstmtFetchSizeSupported
public static boolean isPstmtFetchSizeSupported() -
isSubqueriesSupported
public static boolean isSubqueriesSupported() -
isScrollResultsSupported
public static boolean isScrollResultsSupported() -
isBatchUpdatesSupported
public static boolean isBatchUpdatesSupported() -
isEmbeddedDB
public static boolean isEmbeddedDB() -
getIdentifierQuoteString
-
getTestSQL
-