Package org.jivesoftware.database
Class ProfiledConnection
- java.lang.Object
-
- org.jivesoftware.database.AbstractConnection
-
- org.jivesoftware.database.ProfiledConnection
-
- All Implemented Interfaces:
AutoCloseable
,Connection
,Wrapper
public class ProfiledConnection extends AbstractConnection
Wraps a Connection object and collects statistics about the database queries that are performed.Statistics of the profiled Connections can be obtained from the static methods of this class. Instances of this class are the actual wrappers that perform profiling.
- Author:
- Jive Software
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ProfiledConnection.Type
-
Field Summary
-
Fields inherited from class org.jivesoftware.database.AbstractConnection
connection
-
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
-
-
Constructor Summary
Constructors Constructor Description ProfiledConnection(Connection connection)
Creates a new ProfiledConnection that wraps the specified connection.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
addQuery(ProfiledConnection.Type type, String sql, long time)
Adds a query.void
close()
Statement
createStatement()
Statement
createStatement(int resultSetType, int resultSetConcurrency)
static double
getAverageQueryTime(ProfiledConnection.Type type)
Returns the average amount of time spent executing the specified type of query.static double
getQueriesPerSecond(ProfiledConnection.Type type)
Returns the average number of queries of a certain type that have been performed per second since profiling started.static long
getQueryCount(ProfiledConnection.Type type)
Returns the total number database queries of a particular type performed.static ProfiledConnectionEntry[]
getSortedQueries(ProfiledConnection.Type type, boolean sortByTime)
Returns an array of sorted queries (as ProfiledConnectionEntry objects) by typestatic long
getTotalQueryTime(ProfiledConnection.Type type)
Returns the total amount of time in milliseconds spent doing a particular type of query.CallableStatement
prepareCall(String sql)
CallableStatement
prepareCall(String sql, int i, int i1)
PreparedStatement
prepareStatement(String sql)
PreparedStatement
prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
static void
resetStatistics()
Reset all statistics.static void
start()
Start profiling.static void
stop()
Stop profiling.-
Methods inherited from class org.jivesoftware.database.AbstractConnection
abort, clearWarnings, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStruct, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getHoldability, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, isClosed, isReadOnly, isValid, isWrapperFor, nativeSQL, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setClientInfo, setClientInfo, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMap, unwrap
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.sql.Connection
beginRequest, endRequest, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid
-
-
-
-
Constructor Detail
-
ProfiledConnection
public ProfiledConnection(Connection connection)
Creates a new ProfiledConnection that wraps the specified connection.- Parameters:
connection
- the Connection to wrap and collect stats for.
-
-
Method Detail
-
start
public static void start()
Start profiling.
-
stop
public static void stop()
Stop profiling.
-
getQueryCount
public static long getQueryCount(ProfiledConnection.Type type)
Returns the total number database queries of a particular type performed. Valid types are ProfiledConnection.SELECT, ProfiledConnection.UPDATE, ProfiledConnection.INSERT, and ProfiledConnection.DELETE.- Parameters:
type
- the type of query to get the count for.- Returns:
- the number queries of type
type
performed.
-
addQuery
public static void addQuery(ProfiledConnection.Type type, String sql, long time)
Adds a query.- Parameters:
type
- the type of the query.sql
- the insert sql string.time
- the length of time the query took in milliseconds
-
getQueriesPerSecond
public static double getQueriesPerSecond(ProfiledConnection.Type type)
Returns the average number of queries of a certain type that have been performed per second since profiling started. If profiling has been stopped, that moment in time is used for the calculation. Otherwise, the current moment in time is used.- Parameters:
type
- the type of database query to check.- Returns:
- the average number of queries of a certain typed performed per second.
-
getAverageQueryTime
public static double getAverageQueryTime(ProfiledConnection.Type type)
Returns the average amount of time spent executing the specified type of query.- Parameters:
type
- the type of query.- Returns:
- a double representing the average time spent executing the type of query.
-
getTotalQueryTime
public static long getTotalQueryTime(ProfiledConnection.Type type)
Returns the total amount of time in milliseconds spent doing a particular type of query. Note that this isn't necessarily representative of actual real time since db queries often occur in parallel.- Parameters:
type
- the type of query to check.- Returns:
- the number of milliseconds spent executing the specified type of query.
-
getSortedQueries
public static ProfiledConnectionEntry[] getSortedQueries(ProfiledConnection.Type type, boolean sortByTime)
Returns an array of sorted queries (as ProfiledConnectionEntry objects) by type- Parameters:
type
- the type of query to checksortByTime
- sort the resulting list by Time if true, otherwise sort by count if false (default)- Returns:
- an array of ProfiledConnectionEntry objects
-
resetStatistics
public static void resetStatistics()
Reset all statistics.
-
close
public void close() throws SQLException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceConnection
- Overrides:
close
in classAbstractConnection
- Throws:
SQLException
-
createStatement
public Statement createStatement() throws SQLException
- Specified by:
createStatement
in interfaceConnection
- Overrides:
createStatement
in classAbstractConnection
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql) throws SQLException
- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classAbstractConnection
- Throws:
SQLException
-
createStatement
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
- Specified by:
createStatement
in interfaceConnection
- Overrides:
createStatement
in classAbstractConnection
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
- Specified by:
prepareStatement
in interfaceConnection
- Overrides:
prepareStatement
in classAbstractConnection
- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(String sql) throws SQLException
- Specified by:
prepareCall
in interfaceConnection
- Overrides:
prepareCall
in classAbstractConnection
- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(String sql, int i, int i1) throws SQLException
- Specified by:
prepareCall
in interfaceConnection
- Overrides:
prepareCall
in classAbstractConnection
- Throws:
SQLException
-
-