org.jminor.common.db
Class DatabaseConnectionImpl

java.lang.Object
  extended by org.jminor.common.db.DatabaseConnectionImpl
All Implemented Interfaces:
DatabaseConnection, PoolableConnection

public class DatabaseConnectionImpl
extends Object
implements DatabaseConnection

A default DatabaseConnection implementation, which wraps a standard JDBC Connection object.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jminor.common.db.DatabaseConnection
DatabaseConnection.Function, DatabaseConnection.Operation, DatabaseConnection.Procedure
 
Constructor Summary
DatabaseConnectionImpl(Database database, User user)
          Constructs a new DatabaseConnectionImpl instance, initialized and ready for usage
DatabaseConnectionImpl(Database database, User user, Connection connection)
          Constructs a new DatabaseConnectionImpl instance, based on the given Connection object.
 
Method Summary
 void beginTransaction()
          Begins a transaction on this connection
 void commit()
          Performs a commit
 void commitTransaction()
          Performs a commit and ends the current transaction
 void disconnect()
          Disconnects this connection
 void execute(List<String> statements)
          Executes the given statements, in a batch if possible, which can be anything except a select query.
 void execute(String sql)
          Executes the given statement, which can be anything except a select query.
 Object executeCallableStatement(String sqlStatement, int outParameterType)
          Executes the statement.
 List<?> executeFunction(String functionID, Object... arguments)
          Executes the function with the given id
 void executeProcedure(String procedureID, Object... arguments)
          Executes the procedure with the given id
 Connection getConnection()
           
 Database getDatabase()
          
 List<LogEntry> getLogEntries()
          
 MethodLogger getMethodLogger()
          
 long getPoolTime()
          
 int getRetryCount()
          
 User getUser()
          
 boolean isConnected()
          
 boolean isLoggingEnabled()
          
 boolean isTransactionOpen()
          
 boolean isValid()
          
 List query(String sql, ResultPacker resultPacker, int fetchCount)
          Performs the given sql query and returns the result in a List
 int queryInteger(String sql)
          Performs the given query and returns the result as an integer
 List<Integer> queryIntegers(String sql)
          Performs the given query and returns the result as a List of Integers
 List<List> queryObjects(String sql, int fetchCount)
          
 List<String> queryStrings(String sql)
          Performs the given query and returns the result as a List of Strings
 byte[] readBlobField(String tableName, String columnName, String whereClause)
          Returns the contents of the given blob field.
 void rollback()
          Performs a rollback
 void rollbackTransaction()
          Performs a rollback and ends the current transaction
 void setLoggingEnabled(boolean enabled)
          
 void setPoolTime(long time)
          Sets the time this connection was checked into a connection pool
 void setRetryCount(int retryCount)
          
 String toString()
          
 void writeBlobField(byte[] blobData, String tableName, String columnName, String whereClause)
          Writes the given blob data into the given column.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DatabaseConnectionImpl

public DatabaseConnectionImpl(Database database,
                              User user)
                       throws ClassNotFoundException,
                              SQLException
Constructs a new DatabaseConnectionImpl instance, initialized and ready for usage

Parameters:
database - the database
user - the user for the db-connection
Throws:
SQLException - in case there is a problem connecting to the database
ClassNotFoundException - in case the database driver was not found

DatabaseConnectionImpl

public DatabaseConnectionImpl(Database database,
                              User user,
                              Connection connection)
Constructs a new DatabaseConnectionImpl instance, based on the given Connection object. NB. auto commit is disabled on the Connection that is provided.

Parameters:
database - the database
user - the user for the db-connection
connection - the Connection object to base this DatabaseConnectionImpl on
Method Detail

setPoolTime

public final void setPoolTime(long time)
Sets the time this connection was checked into a connection pool

Specified by:
setPoolTime in interface PoolableConnection
Parameters:
time - the time this connection was pooled

getPoolTime

public final long getPoolTime()

Specified by:
getPoolTime in interface PoolableConnection
Returns:
the time at which this connection was pooled

setRetryCount

public final void setRetryCount(int retryCount)

Specified by:
setRetryCount in interface PoolableConnection
Parameters:
retryCount - the number of retries used to retrieve this connection from the pool

getRetryCount

public final int getRetryCount()

Specified by:
getRetryCount in interface PoolableConnection
Returns:
the number of retries required to retrieve this connection from the pool

toString

public final String toString()

Overrides:
toString in class Object

getUser

public final User getUser()

Specified by:
getUser in interface PoolableConnection
Returns:
the connection user

setLoggingEnabled

public final void setLoggingEnabled(boolean enabled)

Specified by:
setLoggingEnabled in interface PoolableConnection
Parameters:
enabled - true to enable logging on this connection, false to disable

isLoggingEnabled

public final boolean isLoggingEnabled()

Specified by:
isLoggingEnabled in interface PoolableConnection
Returns:
true if logging is enabled, false otherwise

isValid

public final boolean isValid()

Specified by:
isValid in interface PoolableConnection
Returns:
true if the connection is valid

disconnect

public final void disconnect()
Disconnects this connection

Specified by:
disconnect in interface PoolableConnection

isConnected

public final boolean isConnected()

Specified by:
isConnected in interface DatabaseConnection
Returns:
true if the connection is connected

getConnection

public final Connection getConnection()
Specified by:
getConnection in interface PoolableConnection
Returns:
the underlying Connection object

getDatabase

public final Database getDatabase()

Specified by:
getDatabase in interface PoolableConnection
Returns:
the database implementation this connection is based on

beginTransaction

public final void beginTransaction()
Begins a transaction on this connection

Specified by:
beginTransaction in interface PoolableConnection

rollbackTransaction

public final void rollbackTransaction()
Performs a rollback and ends the current transaction

Specified by:
rollbackTransaction in interface PoolableConnection

commitTransaction

public final void commitTransaction()
Performs a commit and ends the current transaction

Specified by:
commitTransaction in interface PoolableConnection

isTransactionOpen

public final boolean isTransactionOpen()

Specified by:
isTransactionOpen in interface PoolableConnection
Returns:
true if a transaction is open

query

public final List query(String sql,
                        ResultPacker resultPacker,
                        int fetchCount)
                 throws SQLException
Performs the given sql query and returns the result in a List

Specified by:
query in interface DatabaseConnection
Parameters:
sql - the query
resultPacker - a ResultPacker instance for creating the return List
fetchCount - the number of records to retrieve, use -1 to retrieve all
Returns:
the query result in a List
Throws:
SQLException - thrown if anything goes wrong during the query execution

queryStrings

public final List<String> queryStrings(String sql)
                                throws SQLException
Performs the given query and returns the result as a List of Strings

Specified by:
queryStrings in interface DatabaseConnection
Parameters:
sql - the query, it must select at least a single string column, any other subsequent columns are disregarded
Returns:
a List of Strings representing the query result
Throws:
SQLException - thrown if anything goes wrong during the execution

queryInteger

public final int queryInteger(String sql)
                       throws SQLException,
                              DatabaseException
Performs the given query and returns the result as an integer

Specified by:
queryInteger in interface DatabaseConnection
Parameters:
sql - the query must select at least a single number column, any other subsequent columns are disregarded
Returns:
the first record in the result as a integer
Throws:
SQLException - thrown if anything goes wrong during the execution
DatabaseException - thrown if no record is found

queryIntegers

public final List<Integer> queryIntegers(String sql)
                                  throws SQLException
Performs the given query and returns the result as a List of Integers

Specified by:
queryIntegers in interface DatabaseConnection
Parameters:
sql - the query, it must select at least a single number column, any other subsequent columns are disregarded
Returns:
a List of Integers representing the query result
Throws:
SQLException - thrown if anything goes wrong during the execution

queryObjects

public final List<List> queryObjects(String sql,
                                     int fetchCount)
                              throws SQLException

Specified by:
queryObjects in interface DatabaseConnection
Parameters:
sql - the query
fetchCount - the maximum number of records to return, -1 for all
Returns:
the result of this query, in a List of rows represented as Lists
Throws:
SQLException - thrown if anything goes wrong during the query execution

readBlobField

public final byte[] readBlobField(String tableName,
                                  String columnName,
                                  String whereClause)
                           throws SQLException
Returns the contents of the given blob field.

Specified by:
readBlobField in interface DatabaseConnection
Parameters:
tableName - the table name
columnName - the name of the blob column
whereClause - the where clause
Returns:
the blob contents
Throws:
SQLException - thrown if anything goes wrong during the execution

writeBlobField

public final void writeBlobField(byte[] blobData,
                                 String tableName,
                                 String columnName,
                                 String whereClause)
                          throws SQLException
Writes the given blob data into the given column.

Specified by:
writeBlobField in interface DatabaseConnection
Parameters:
blobData - the blob data
tableName - the table name
columnName - the blob column name
whereClause - the where clause
Throws:
SQLException - thrown if anything goes wrong during the execution

commit

public final void commit()
                  throws SQLException
Performs a commit

Specified by:
commit in interface PoolableConnection
Throws:
SQLException - thrown if anything goes wrong during the execution

rollback

public final void rollback()
                    throws SQLException
Performs a rollback

Specified by:
rollback in interface PoolableConnection
Throws:
SQLException - thrown if anything goes wrong during the execution

executeCallableStatement

public final Object executeCallableStatement(String sqlStatement,
                                             int outParameterType)
                                      throws SQLException
Executes the statement.

Specified by:
executeCallableStatement in interface DatabaseConnection
Parameters:
sqlStatement - the statement to execute
outParameterType - the type of the out parameter, -1 if no out parameter, java.sql.Types.*
Returns:
the out parameter, null if none is specified
Throws:
SQLException - thrown if anything goes wrong during execution

execute

public final void execute(String sql)
                   throws SQLException
Executes the given statement, which can be anything except a select query.

Specified by:
execute in interface DatabaseConnection
Parameters:
sql - the statement to execute
Throws:
SQLException - thrown if anything goes wrong during execution

executeFunction

public final List<?> executeFunction(String functionID,
                                     Object... arguments)
                              throws DatabaseException
Executes the function with the given id

Specified by:
executeFunction in interface DatabaseConnection
Parameters:
functionID - the function ID
arguments - the arguments, if any
Returns:
the procedure return arguments, if any
Throws:
DatabaseException - in case anyhing goes wrong during the execution

executeProcedure

public final void executeProcedure(String procedureID,
                                   Object... arguments)
                            throws DatabaseException
Executes the procedure with the given id

Specified by:
executeProcedure in interface DatabaseConnection
Parameters:
procedureID - the procedure ID
arguments - the arguments, if any
Throws:
DatabaseException - in case anyhing goes wrong during the execution

execute

public final void execute(List<String> statements)
                   throws SQLException
Executes the given statements, in a batch if possible, which can be anything except a select query.

Specified by:
execute in interface DatabaseConnection
Parameters:
statements - the statements to execute
Throws:
SQLException - thrown if anything goes wrong during execution

getLogEntries

public final List<LogEntry> getLogEntries()

Specified by:
getLogEntries in interface PoolableConnection
Returns:
the log entries

getMethodLogger

public MethodLogger getMethodLogger()

Specified by:
getMethodLogger in interface PoolableConnection
Returns:
the MethodLogger being used by this db connection