DODS 6.5 API

com.lutris.appserver.server.sql.datasource
Class SimpleDataSourceDBConnection

java.lang.Object
  extended bycom.lutris.appserver.server.sql.datasource.SimpleDataSourceDBConnection
All Implemented Interfaces:
DBConnection, ExtendedDBConnection

public class SimpleDataSourceDBConnection
extends java.lang.Object
implements ExtendedDBConnection

Standard implementation of the DBConnection object.

Since:
LBS1.7
Version:
$Revision: 1.1 $
Author:
Mark Diekhans, Kyle Clark
See Also:
DBConnection

Field Summary
protected  boolean allocated
           
protected  boolean closed
           
protected  java.sql.Connection connection
          JDBC connection object.
protected  ConnectionAllocator connectionAllocator
           
protected  long connectionEnterPoolTime
           
protected  int connectionUsageCounter
           
protected  java.sql.Statement currentStmt
           
protected  boolean dropConnection
           
protected  boolean dropped
           
protected  javax.sql.DataSource ds
           
protected  int generation
           
protected  int id
           
protected  boolean logging
           
protected  int maxPreparedStmts
           
protected static int nextId
           
protected  java.lang.String password
          Database user password.
protected  java.util.Hashtable preparedStmtCache
           
protected  boolean reset
           
protected  java.lang.String url
          Database URL, used for error messages.
protected  java.lang.String user
          Database user, for error messages.
 
Constructor Summary
protected SimpleDataSourceDBConnection(ConnectionAllocator connectionAllocatorObj, javax.sql.DataSource dataSource, int maxPreparedStatements, boolean logging, int generation)
          Initialize the connection to a database.
  SimpleDataSourceDBConnection(ConnectionAllocator connectionAllocatorObj, java.lang.String url, java.lang.String user, java.lang.String password, int maxPreparedStatements, boolean logging, int generation)
          Initialize the connection to a database.
 
Method Summary
 void allocate()
          Method called when this connection object is allocated from the connection allocator.
protected  void allocatedCheck()
          Check to see that this database connection has has been properly allocated before is is used.
 void close()
          Close this connection.
protected  void closedCheck()
          Check to see that this database connection has has not been closed.
 void commit()
          Commit a transaction.
protected  void drop()
          Drop this connection from the connection allocator.
 boolean execute(java.lang.String sql)
          Execute a SQL statement that does not return a resultset.
 java.sql.ResultSet executeQuery(java.sql.PreparedStatement preparedStmt, java.lang.String msg)
          Execute a prepared query statement.
 java.sql.ResultSet executeQuery(java.lang.String sql)
          Execute a SQL query statement.
 int executeUpdate(java.sql.PreparedStatement preparedStmt, java.lang.String msg)
          Execute a prepared update statement.
 int executeUpdate(java.lang.String sql)
          Execute a SQL update statement.
 java.sql.Connection getConnection()
          Get the underlying Connection object.
 long getConnectionEnterPoolTime()
          Return value off ConnectionEnterPoolTime.
 int getConnectionUsageCounter()
          Return value off connectionUsageCounter.
 java.lang.String getDatabaseName()
           
 int getGeneration()
          Get the generation number specified when the connection was created.
 int getMaxPreparedStmts()
           
protected  int getResultSetConcurrency()
           
protected  int getResultSetType()
           
 java.lang.String getUrl()
          Get the database URL.
 java.lang.String getUser()
          Get the database user name.
 boolean handleException(java.sql.SQLException sqlExcept)
          Check if a connection is valid after an SQL exception is thrown.
 void incrRequestCount()
          Increment the count of the number of requests against this connection.
 boolean isClosed()
          Is connection closed.
 boolean isDroped()
          Is connection droped.
 boolean isMarkedForDrop()
           
protected  void logDebug(java.lang.String str)
          Debug logging.
protected  void logDebug(java.lang.String str, java.sql.Statement stmt)
          Debug logging.
 java.sql.CallableStatement prepareCall(java.lang.String sql)
          Creates a CallableStatement object for calling database stored procedures.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
          Get a prepared statement given an SQL string.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int iResultSetType, int iResultSetConcurrency)
          Get a prepared statement given an SQL string.
 void release()
          Return this connection to its allocator.
 void reset()
          Closes down all query-specific resources.
protected  void resetCheck()
          Check to see that the previous database connection has had reset called.
 void rollback()
          Rollback a transaction.
 void setAutoCommit(boolean on)
          Autocommit on/off.
 void setConnectionEnterPoolTime(long i)
          Set new value for ConnectionEnterPoolTime.
 void setConnectionUsageCounter(int i)
          Set new value for connectionUsageCounter.
 void validate()
          Validates this connection.
 void warningCheck(java.sql.ResultSet resultSet)
          Check for warnings in a result set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connection

protected java.sql.Connection connection
JDBC connection object.


url

protected java.lang.String url
Database URL, used for error messages.


user

protected java.lang.String user
Database user, for error messages.


ds

protected javax.sql.DataSource ds

password

protected java.lang.String password
Database user password.


id

protected int id

nextId

protected static int nextId

preparedStmtCache

protected java.util.Hashtable preparedStmtCache

currentStmt

protected java.sql.Statement currentStmt

connectionAllocator

protected ConnectionAllocator connectionAllocator

logging

protected boolean logging

generation

protected int generation

closed

protected boolean closed

dropConnection

protected boolean dropConnection

dropped

protected boolean dropped

reset

protected boolean reset

allocated

protected boolean allocated

maxPreparedStmts

protected int maxPreparedStmts

connectionUsageCounter

protected int connectionUsageCounter

connectionEnterPoolTime

protected long connectionEnterPoolTime
Constructor Detail

SimpleDataSourceDBConnection

public SimpleDataSourceDBConnection(ConnectionAllocator connectionAllocatorObj,
                                    java.lang.String url,
                                    java.lang.String user,
                                    java.lang.String password,
                                    int maxPreparedStatements,
                                    boolean logging,
                                    int generation)
                             throws java.sql.SQLException
Initialize the connection to a database.

Parameters:
url - JDBC URL of database.
user - SQL user name.
password - SQL password.
connectionAllocatorObj - The connection allocator that this connection belongs to.
maxPreparedStatements - Maximum number of preparse statements. a value of less than zero queries JDBC for the value.
logging - Specifying true enables SQL logging.
generation - A generation number used to drop old connection when they are released.
Throws:
java.sql.SQLException - If a connection can't be established.

SimpleDataSourceDBConnection

protected SimpleDataSourceDBConnection(ConnectionAllocator connectionAllocatorObj,
                                       javax.sql.DataSource dataSource,
                                       int maxPreparedStatements,
                                       boolean logging,
                                       int generation)
                                throws java.sql.SQLException
Initialize the connection to a database.

Parameters:
connectionAllocatorObj - The connection allocator that this connection belongs to.
maxPreparedStatements - Maximum number of preparse statements. a value of less than zero queries JDBC for the value.
logging - Specifying true enables SQL logging.
generation - A generation number used to drop old connection when they are released.
Throws:
java.sql.SQLException - If a connection can't be established.
Method Detail

allocate

public void allocate()
              throws java.sql.SQLException
Method called when this connection object is allocated from the connection allocator.

Specified by:
allocate in interface DBConnection
Throws:
java.sql.SQLException - If reset had no been called on the previous operation.

resetCheck

protected void resetCheck()
                   throws java.sql.SQLException
Check to see that the previous database connection has had reset called. This ensures that the database resources have always been freed in a timely manner.

Throws:
java.sql.SQLException - If reset has not been called on the previous operation.

allocatedCheck

protected void allocatedCheck()
                       throws java.sql.SQLException
Check to see that this database connection has has been properly allocated before is is used.

Throws:
java.sql.SQLException - If it was not properly allocated.

closedCheck

protected void closedCheck()
                    throws java.sql.SQLException
Check to see that this database connection has has not been closed.

Throws:
java.sql.SQLException - If it is closed.

validate

public void validate()
              throws java.sql.SQLException
Validates this connection. Check to see that it is not closed and has been properly allocated.

Specified by:
validate in interface DBConnection
Throws:
java.sql.SQLException - If it is not valid.

reset

public void reset()
           throws java.sql.SQLException
Closes down all query-specific resources.

Specified by:
reset in interface DBConnection
Throws:
java.sql.SQLException - If a database error occurs.

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
                                            throws java.sql.SQLException
Get a prepared statement given an SQL string. If the statement is cached, return that statement, otherwise prepare and save in the cache.

Specified by:
prepareStatement in interface DBConnection
Parameters:
sql - The SQL statement to prepared.
Returns:
The prepared statement, which is associated only with this connection and must not be used once the connection is released.
Throws:
java.sql.SQLException - If a SQL error occured compiling the statement.

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int iResultSetType,
                                                   int iResultSetConcurrency)
                                            throws java.sql.SQLException
Get a prepared statement given an SQL string. If the statement is cached, return that statement, otherwise prepare and save in the cache.

Specified by:
prepareStatement in interface ExtendedDBConnection
Parameters:
sql - The SQL statement to prepared.
iResultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE.
iResultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE.
Returns:
The prepared statement, which is associated only with this connection and must not be used once the connection is released.
Throws:
java.sql.SQLException - If a SQL error occured compiling the statement.

getResultSetType

protected int getResultSetType()

getResultSetConcurrency

protected int getResultSetConcurrency()

prepareCall

public java.sql.CallableStatement prepareCall(java.lang.String sql)
                                       throws java.sql.SQLException
Creates a CallableStatement object for calling database stored procedures. Refer to jdk api refernece.

Specified by:
prepareCall in interface DBConnection
Parameters:
sql - The SQL statement to be called.
Returns:
a new CallableStatement object containing the pre-compiled SQL statement.
Throws:
java.sql.SQLException - If a database access error occurs statement.

executeQuery

public java.sql.ResultSet executeQuery(java.sql.PreparedStatement preparedStmt,
                                       java.lang.String msg)
                                throws java.sql.SQLException
Execute a prepared query statement. Once the query has completed, reset() should be called.

Specified by:
executeQuery in interface DBConnection
Parameters:
preparedStmt - The statement to execute.
msg - for logging/debug purposes
Returns:
Query result set, do not call close, use reset(),
Throws:
java.sql.SQLException - If a SQL error occured executing the statement.

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql)
                                throws java.sql.SQLException
Execute a SQL query statement. This is a wrapper that adds logging. Once the query has completed, reset() should be called.

Specified by:
executeQuery in interface DBConnection
Parameters:
sql - The SQL query statement
Returns:
Query result set, do not call close, use reset(),
Throws:
java.sql.SQLException - If a SQL error occured executing the statement.

executeUpdate

public int executeUpdate(java.lang.String sql)
                  throws java.sql.SQLException
Execute a SQL update statement. This is a wrapper that adds logging. Once the update has completed, reset() should be called.

Specified by:
executeUpdate in interface DBConnection
Parameters:
sql - The SQL query statement
Returns:
Either the row count for UPDATE, INSERT, DELETE or 0 for SQL statements that return nothing.
Throws:
java.sql.SQLException - If a SQL error occured executing the update.

executeUpdate

public int executeUpdate(java.sql.PreparedStatement preparedStmt,
                         java.lang.String msg)
                  throws java.sql.SQLException
Execute a prepared update statement. Once the update has completed, reset() should be called.

Specified by:
executeUpdate in interface DBConnection
Parameters:
preparedStmt - The statement to execute.
msg - for logging/debug purposes
Returns:
Either the row count for UPDATE, INSERT, DELETE or 0 for SQL statements that return nothing.
Throws:
java.sql.SQLException - If a SQL error occured executing the statement.

execute

public boolean execute(java.lang.String sql)
                throws java.sql.SQLException
Execute a SQL statement that does not return a resultset. This is a wrapper that adds logging. Once the query has completed, reset() should be called.

Specified by:
execute in interface DBConnection
Parameters:
sql - The SQL query statement
Returns:
True if the next result is a ResultSet; false if it is an update count or there are no more results.
Throws:
java.sql.SQLException - If a SQL error occured executing the statement.

warningCheck

public void warningCheck(java.sql.ResultSet resultSet)
                  throws java.sql.SQLException
Check for warnings in a result set.

Specified by:
warningCheck in interface DBConnection
Parameters:
resultSet - The result set to check for warnings.
Throws:
java.sql.SQLException - If a SQL error occured compiling the statement.

release

public void release()
Return this connection to its allocator. This object should not be used after calling this function.

Specified by:
release in interface DBConnection

drop

protected void drop()
Drop this connection from the connection allocator.


handleException

public boolean handleException(java.sql.SQLException sqlExcept)
Check if a connection is valid after an SQL exception is thrown. If it is not usable, then the connection is dropped from the connection allocator and closed. The connection is then no longer usable.

Specified by:
handleException in interface DBConnection
Parameters:
sqlExcept - The SQL exception that occured.
Returns:
true if the exception does not affect this connection object. False otherwise - in which case this connection object is no longer usable.

getGeneration

public int getGeneration()
Get the generation number specified when the connection was created.

Specified by:
getGeneration in interface DBConnection
Returns:
The generation number.

close

public void close()
Close this connection. Use by the connection allocator when this object is no longer used. Errors are ignored.

Specified by:
close in interface DBConnection

setAutoCommit

public void setAutoCommit(boolean on)
                   throws java.sql.SQLException
Autocommit on/off.

Specified by:
setAutoCommit in interface DBConnection
Parameters:
on - - False to disable auto commit mode. True to enable.
Throws:
java.sql.SQLException - If a database access error occurs.

commit

public void commit()
            throws java.sql.SQLException
Commit a transaction.

Specified by:
commit in interface DBConnection
Throws:
java.sql.SQLException - If a database access error occurs.

rollback

public void rollback()
              throws java.sql.SQLException
Rollback a transaction. Should only be used when auto-commit mode has been disabled.

Specified by:
rollback in interface DBConnection
Throws:
java.sql.SQLException - If a database access error occurs.

logDebug

protected void logDebug(java.lang.String str)
Debug logging.

Parameters:
str - The data to log.

logDebug

protected void logDebug(java.lang.String str,
                        java.sql.Statement stmt)
Debug logging.

Parameters:
str - The data to log.
stmt - The statement to log.

incrRequestCount

public void incrRequestCount()
Increment the count of the number of requests against this connection.

Specified by:
incrRequestCount in interface DBConnection

getUrl

public java.lang.String getUrl()
Get the database URL.

Specified by:
getUrl in interface DBConnection
Returns:
The database URL.

getUser

public java.lang.String getUser()
Get the database user name. Normally user for error messages.

Specified by:
getUser in interface DBConnection
Returns:
The database user name.

getConnection

public java.sql.Connection getConnection()
Get the underlying Connection object. Use with extreme caution.

Specified by:
getConnection in interface DBConnection
Returns:
the connection object

isMarkedForDrop

public boolean isMarkedForDrop()
Specified by:
isMarkedForDrop in interface DBConnection
Returns:
true if this connection is marked to be dropped out of the connection pool and closed.

getDatabaseName

public java.lang.String getDatabaseName()
Specified by:
getDatabaseName in interface DBConnection
Returns:
database name of current connection

getConnectionUsageCounter

public int getConnectionUsageCounter()
Description copied from interface: ExtendedDBConnection
Return value off connectionUsageCounter.

Specified by:
getConnectionUsageCounter in interface ExtendedDBConnection
Returns:
connectionUsageCounter

setConnectionUsageCounter

public void setConnectionUsageCounter(int i)
Description copied from interface: ExtendedDBConnection
Set new value for connectionUsageCounter.

Specified by:
setConnectionUsageCounter in interface ExtendedDBConnection
Parameters:
i - new value for connectionUsageCounter

isDroped

public boolean isDroped()
Description copied from interface: ExtendedDBConnection
Is connection droped.

Specified by:
isDroped in interface ExtendedDBConnection
Returns:
dropped

isClosed

public boolean isClosed()
Description copied from interface: ExtendedDBConnection
Is connection closed.

Specified by:
isClosed in interface ExtendedDBConnection
Returns:
dropped

setConnectionEnterPoolTime

public void setConnectionEnterPoolTime(long i)
Description copied from interface: ExtendedDBConnection
Set new value for ConnectionEnterPoolTime.

Specified by:
setConnectionEnterPoolTime in interface ExtendedDBConnection
Parameters:
i - New value.

getConnectionEnterPoolTime

public long getConnectionEnterPoolTime()
Description copied from interface: ExtendedDBConnection
Return value off ConnectionEnterPoolTime.

Specified by:
getConnectionEnterPoolTime in interface ExtendedDBConnection
Returns:
ConnectionEnterPoolTime value.

getMaxPreparedStmts

public int getMaxPreparedStmts()
Specified by:
getMaxPreparedStmts in interface ExtendedDBConnection
Returns:
Returns the maxPreparedStmts.

DODS 6.5 API