com.nec.tdd.tools.dbMapper
Class DBConnectionImpl

java.lang.Object
  |
  +--com.nec.tdd.tools.dbMapper.DBConnectionImpl
All Implemented Interfaces:
DBConnection

public class DBConnectionImpl
extends java.lang.Object
implements DBConnection

A concrete implementation of DBConnection interface.

It maintains a hashtable of frequently used prepared and callable statements for this database connection.


Field Summary
protected  java.util.Hashtable callStmts
          A HashTable of callable statements for this DB connection.
Callable statements are indexed by user given name.
protected  java.sql.Connection conn
          The underlying JDBC connection.
protected  java.util.Hashtable prepStmts
          A HashTable of prepared statements for this DB connection.
Prepared statements are indexed by user given name.
protected  java.sql.Statement stmt
          A statement associated with this DB connection.
Created during object construction and closed when object is destroyed or destroy method is invoked.
 
Constructor Summary
DBConnectionImpl(java.sql.Connection conn)
          Constructs a DBConnectionImpl with the given database connection.
DBConnectionImpl(java.lang.String dbUrl)
          Constructs a DBConnectionImpl with the given database url.
It is assumed that an appropiate JDBC driver is loaded and registered with DriverManager before constructing any DBConnectionImpl object.
DBConnectionImpl(java.lang.String dbUrl, java.util.Properties info)
          Constructs a DBConnectionImpl with the given database url, and connection arguemnts.
It is assumed that an appropiate JDBC driver is loaded and registered with DriverManager before constructing any DBConnectionImpl object.
DBConnectionImpl(java.lang.String dbUrl, java.lang.String user, java.lang.String passwd)
          Constructs a DBConnectionImpl with the given database url, user and password.
It is assumed that an appropiate JDBC driver is loaded and registered with DriverManager before constructing any DBConnectionImpl object.
 
Method Summary
 void addCallableStatement(java.lang.String name, java.lang.String sql)
          Prepare and add a callable statement to this DB connection.
 void addCallableStatement(java.lang.String name, java.lang.String sql, boolean isOverWritable)
          Prepare and add a callable statement to this DB connection.
If an entry with same name exists, isOverWritable parameter determines whether or not to overwrite the cached entry.
 void addPreparedStatement(java.lang.String name, java.lang.String sql)
          Prepare and add a prepared statement to this DB connection.
 void addPreparedStatement(java.lang.String name, java.lang.String sql, boolean isOverWritable)
          Prepare and add a prepared statement to this DB connection.
If an entry with same name exists, isOverWritable parameter determines whether or not to overwrite the cached entry.
 void commit()
          Makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by the Connection.
 void destroy()
          Cleanup for database connection object.
protected  void finalize()
          Override Object's finalize method to free all the resources acquired by this DBConnection during garbage collection.
 boolean getAutoCommit()
          Get the current auto-commit state.
 java.sql.CallableStatement getCallableStatement(java.lang.String name)
          Get a cached callable statement associated with this DB connection.
Lifecycle of callable statement will be managed by DBConnection.
 java.sql.DatabaseMetaData getMetaData()
          Gets the metadata regarding this connection's database.
 java.sql.PreparedStatement getPreparedStatement(java.lang.String name)
          Get a cached prepared statement associated with this DB connection.
Lifecycle of prepared statement will be managed by DBConnection.
 java.sql.Statement getStatement()
          Get statement associated with this DB connection.
Lifecycle of statement will be managed by DBConnection.
 boolean isClosed()
          Tests to see if a Connection is closed.
 boolean isReadOnly()
          Tests to see if the connection is in read-only mode.
 java.sql.CallableStatement makeCallableStatement(java.lang.String sql)
          Make/create a callable statement for this DB connection.
 java.sql.PreparedStatement makePreparedStatement(java.lang.String sql)
          Make/create a prepared statement for this DB connection.
 void rollback()
          Drops all changes made since the previous commit/rollback and releases any database locks currently held by this Connection.
 void setAutoCommit(boolean isEnabled)
          Sets this connection's auto-commit state.
 void setReadOnly(boolean isReadOnly)
          Puts this connection in read-only mode as a hint to enable database optimizations.
 java.lang.String toString()
          Returns string representation of database connection.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

conn

protected java.sql.Connection conn
The underlying JDBC connection.

stmt

protected java.sql.Statement stmt
A statement associated with this DB connection.
Created during object construction and closed when object is destroyed or destroy method is invoked.

prepStmts

protected java.util.Hashtable prepStmts
A HashTable of prepared statements for this DB connection.
Prepared statements are indexed by user given name.

callStmts

protected java.util.Hashtable callStmts
A HashTable of callable statements for this DB connection.
Callable statements are indexed by user given name.
Constructor Detail

DBConnectionImpl

public DBConnectionImpl(java.lang.String dbUrl)
                 throws java.sql.SQLException
Constructs a DBConnectionImpl with the given database url.
It is assumed that an appropiate JDBC driver is loaded and registered with DriverManager before constructing any DBConnectionImpl object.
Parameters:
dbUrl - a database url of the form jdbc:subprotocol:subname
Throws:
java.sql.SQLException - if a database access error occurs.

DBConnectionImpl

public DBConnectionImpl(java.lang.String dbUrl,
                        java.lang.String user,
                        java.lang.String passwd)
                 throws java.sql.SQLException
Constructs a DBConnectionImpl with the given database url, user and password.
It is assumed that an appropiate JDBC driver is loaded and registered with DriverManager before constructing any DBConnectionImpl object.
Parameters:
dbUrl - a database url of the form jdbc:subprotocol:subname
user - the database user on whose behalf the connection is being made
passwd - the user's password
Throws:
java.sql.SQLException - if a database access error occurs.

DBConnectionImpl

public DBConnectionImpl(java.lang.String dbUrl,
                        java.util.Properties info)
                 throws java.sql.SQLException
Constructs a DBConnectionImpl with the given database url, and connection arguemnts.
It is assumed that an appropiate JDBC driver is loaded and registered with DriverManager before constructing any DBConnectionImpl object.
Parameters:
dbUrl - a database url of the form jdbc:subprotocol:subname
info - a list of arbitrary string tag/value pairs as connection arguments; normally at least a "user" and "password" property should be included
Throws:
java.sql.SQLException - if a database access error occurs.

DBConnectionImpl

public DBConnectionImpl(java.sql.Connection conn)
                 throws java.sql.SQLException
Constructs a DBConnectionImpl with the given database connection.
Parameters:
conn - a database connection.
Throws:
java.sql.SQLException - if a database access error occurs.
Method Detail

finalize

protected void finalize()
                 throws java.lang.Exception
Override Object's finalize method to free all the resources acquired by this DBConnection during garbage collection.
Overrides:
finalize in class java.lang.Object

destroy

public void destroy()
Cleanup for database connection object. Close all statements, clear cache tables.

getStatement

public java.sql.Statement getStatement()
Get statement associated with this DB connection.
Lifecycle of statement will be managed by DBConnection. It is created during object construction and closed when DBConnection is destroyed. So external routines SHOULD NOT close the statement.
Specified by:
getStatement in interface DBConnection
Returns:
the statement associated with this DB connection.

getPreparedStatement

public java.sql.PreparedStatement getPreparedStatement(java.lang.String name)
Get a cached prepared statement associated with this DB connection.
Lifecycle of prepared statement will be managed by DBConnection. So external routines should not close the prepared statement.
Specified by:
getPreparedStatement in interface DBConnection
Parameters:
name - key whose associated cached prepared statement is to be returned (provided during addPreparedStatement).
Returns:
the prepared statement to which name is mapped. Returns null if no mapping for the name.

makePreparedStatement

public java.sql.PreparedStatement makePreparedStatement(java.lang.String sql)
                                                 throws java.sql.SQLException
Make/create a prepared statement for this DB connection. Returned statement is not cached and its lifecycle should be managed by calling method. User should close the prepared statement when done with it.
Specified by:
makePreparedStatement in interface DBConnection
Parameters:
sql - a SQL statement that may contain one or more '?' IN parameter placeholders
Returns:
a new PreparedStatement object containing the pre-compiled sql statement
Throws:
java.sql.SQLException - if a database access error occurs.

addPreparedStatement

public void addPreparedStatement(java.lang.String name,
                                 java.lang.String sql,
                                 boolean isOverWritable)
                          throws java.sql.SQLException
Prepare and add a prepared statement to this DB connection.
If an entry with same name exists, isOverWritable parameter determines whether or not to overwrite the cached entry.
Specified by:
addPreparedStatement in interface DBConnection
Parameters:
name - key with which the prepared statement is to be associated
sql - a SQL statement that may contain one or more '?' IN parameter placeholders
isOverWritable - specifies whether or not to overwrite cached entry.
Throws:
java.sql.SQLException - if a database access error occurs.
See Also:
addPreparedStatement(String,String)

addPreparedStatement

public void addPreparedStatement(java.lang.String name,
                                 java.lang.String sql)
                          throws java.sql.SQLException
Prepare and add a prepared statement to this DB connection. If an entry with same name exists, it will be overwritten.
Specified by:
addPreparedStatement in interface DBConnection
Parameters:
name - key with which the prepared statement is to be associated
sql - a SQL statement that may contain one or more '?' IN parameter placeholders
Throws:
java.sql.SQLException - if a database access error occurs.
See Also:
addPreparedStatement(String,String,boolean)

getCallableStatement

public java.sql.CallableStatement getCallableStatement(java.lang.String name)
Get a cached callable statement associated with this DB connection.
Lifecycle of callable statement will be managed by DBConnection. So external routines should not close the callable statement.
Specified by:
getCallableStatement in interface DBConnection
Parameters:
name - key whose associated cached callable statement is to be returned (provided during addCallableStatement).
Returns:
the callable statement to which name is mapped. Returns null if no mapping for the name.

makeCallableStatement

public java.sql.CallableStatement makeCallableStatement(java.lang.String sql)
                                                 throws java.sql.SQLException
Make/create a callable statement for this DB connection. Returned statement is not cached and its lifecycle should be managed by calling method. User should close the callable statement when done with it.
Specified by:
makeCallableStatement in interface DBConnection
Parameters:
sql - a SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is a JDBC function call escape string.
Returns:
a new CallableStatement object containing the pre-compiled sql statement
Throws:
java.sql.SQLException - if a database access error occurs.

addCallableStatement

public void addCallableStatement(java.lang.String name,
                                 java.lang.String sql,
                                 boolean isOverWritable)
                          throws java.sql.SQLException
Prepare and add a callable statement to this DB connection.
If an entry with same name exists, isOverWritable parameter determines whether or not to overwrite the cached entry.
Specified by:
addCallableStatement in interface DBConnection
Parameters:
name - key with which the callable statement is to be associated
sql - a SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is a JDBC function call escape string.
isOverWritable - specifies whether or not to overwrite cached entry.
Throws:
java.sql.SQLException - if a database access error occurs.
See Also:
addCallableStatement(String,String)

addCallableStatement

public void addCallableStatement(java.lang.String name,
                                 java.lang.String sql)
                          throws java.sql.SQLException
Prepare and add a callable statement to this DB connection. If an entry with same name exists, it will be overwritten.
Specified by:
addCallableStatement in interface DBConnection
Parameters:
name - key with which the callable statement is to be associated
sql - a SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is a JDBC function call escape string.
Throws:
java.sql.SQLException - if a database access error occurs.
See Also:
addCallableStatement(String,String,boolean)

getAutoCommit

public boolean getAutoCommit()
                      throws java.sql.SQLException
Get the current auto-commit state. By default a new connection will be in auto commit mode.
Specified by:
getAutoCommit in interface DBConnection
Returns:
the current state of auto-commit mode
Throws:
java.sql.SQLException - if a database access error occurs.

setAutoCommit

public void setAutoCommit(boolean isEnabled)
                   throws java.sql.SQLException
Sets this connection's auto-commit state. For more detail refer to java.sql.Connection interface documentation.
Specified by:
setAutoCommit in interface DBConnection
Parameters:
isEnabled - true enables auto-commit; false disables auto-commit.
Throws:
java.sql.SQLException - if a database access error occurs.

commit

public void commit()
            throws java.sql.SQLException
Makes all changes made since the previous commit/rollback permanent and releases any database locks currently held by the Connection. This method should be used only when auto-commit mode has been disabled.
Specified by:
commit in interface DBConnection
Throws:
java.sql.SQLException - if a database access error occurs.

rollback

public void rollback()
              throws java.sql.SQLException
Drops all changes made since the previous commit/rollback and releases any database locks currently held by this Connection. This method should be used only when auto- commit has been disabled.
Specified by:
rollback in interface DBConnection
Throws:
java.sql.SQLException - if a database access error occurs.

getMetaData

public java.sql.DatabaseMetaData getMetaData()
                                      throws java.sql.SQLException
Gets the metadata regarding this connection's database.
Specified by:
getMetaData in interface DBConnection
Returns:
a DatabaseMetaData object for this Connection
Throws:
java.sql.SQLException - if a database access error occurs.

isReadOnly

public boolean isReadOnly()
                   throws java.sql.SQLException
Tests to see if the connection is in read-only mode.
Specified by:
isReadOnly in interface DBConnection
Returns:
true if connection is read-only and false otherwise
Throws:
java.sql.SQLException - if a database access error occurs.

setReadOnly

public void setReadOnly(boolean isReadOnly)
                 throws java.sql.SQLException
Puts this connection in read-only mode as a hint to enable database optimizations.

Note:
This method cannot be called while in the middle of a transaction.
Calling routines are responsible to to check the connection type before using it.

Specified by:
setReadOnly in interface DBConnection
Parameters:
isReadOnly - true enables read-only mode; false disables read-only mode.
Throws:
java.sql.SQLException - if a database access error occurs.

isClosed

public boolean isClosed()
                 throws java.sql.SQLException
Tests to see if a Connection is closed.
Specified by:
isClosed in interface DBConnection
Returns:
true if the connection is closed; false if it's still open.
Throws:
java.sql.SQLException - if a database access error occurs.

toString

public java.lang.String toString()
Returns string representation of database connection.
Overrides:
toString in class java.lang.Object