|
AdventNet Web NMS 4 API Specification | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.adventnet.management.transaction.ConnectionPool
ConnectionPool class acts as the gateway for making use of the resources that are pooled. It is a JDBC resource management layer, acts as a driver that performs the resource management functions before passing requests to the database drivers by non-intrusively caches and recycles database resources such as connections, prepared statements and statements.
All database operations in Web NMS are performed via the central resource pool using an instance of this class. Connection Pooling is supported in both transaction and non-transaction mode.
Using this class, you can carry out the following functions :
The first step to access the resources in the pool is to get the
ConnectionPool handle. The getInstance() method in
ConnectionPool can be used for this purpose. This returns
an instance of the already initialized ConnectionPool class, in the
same JVM as that of the Web NMS Server. User applications within
the same JVM are advised to make use of the ConnectionPool instance
created by Web NMS.
Code Snippet :Accessing Connections//getting the handle of ConnectionPool in the sameJVM as of WebNMS
ConnectionPool cp = ConnectionPool.getInstance();
Whenever a Connection is requested, the Connection Pool verifies if the request is made by a thread which is in transaction or not.
Code Snippet :Accessing Prepared StatementsConnectionPool cp = ConnectionPool.getInstance();
Connection con = cp.getConnection();
Whenever a prepared statement is requested, the Connection Pool verifies if the request is made by a thread which is in transaction or not.
Code Snippet :Accessing Statements// To get a reference of ConnectionPool
ConnectionPool cp = ConnectionPool.getInstance();
//To get a PreparedStatement and execute it.
PreparedStatementWrapper psw = null;
// Get unique ID of the prepared statement
int id = cp.getPreparedStatementID ("select * from ManagedObject where NAME = ?")
try
{
psw = cp.fetchPreparedStatement(id) ; // PreparedStatementWrapper reference
PreparedStatement ps = psw.getPreparedStatement();
ps.setString(1,"hostname");
}
catch (Exception e)
{
// handle Exception
}
finally
{
// To return the prepared statement - IMPORTANT
cp.returnPreparedStatement(psw);
}
A statement requested from a thread involved in transaction, returns the statement from the same connection used in the transaction. A statement requested from a non-transaction mode returns the statement which in not involved in a transaction.
Code Snippet :// To get the handle of ConnectionPool
ConnectionPool cp = ConnectionPool.getInstance();
// To execute the input SQL query string in an already created Statement
cp.executeQueryStmt(sqlString);
// For database operations like insert, update, delete
cp.executeUpdateStmt(sqlString);
| Field Summary | |
java.lang.String |
driverName
Database Driver name, for eg., sun.jdbc.odbc.JdbcOdbcDriver |
java.lang.String |
password
Login password of the database. |
LogUser |
transUser
Log User for logging messages to transaction logs. |
java.lang.String |
url
Database URL of the form jdbc:subprotocol:DataSourceName. |
java.lang.String |
userName
Login user name of the database. |
| Constructor Summary | |
ConnectionPool()
Constructs a ConnectionPool with default URL, user name, password and driver name. |
|
ConnectionPool(java.lang.String urlstr,
java.lang.String usrname,
java.lang.String pwd,
java.lang.String drivername)
Constructs a ConnectionPool with given URL, user name, password and driver name. |
|
ConnectionPool(java.lang.String urlstr,
java.lang.String usrname,
java.lang.String pwd,
java.lang.String drivername,
boolean enable,
int num_trans,
int num_nontrans,
java.lang.String conAuth)
Creates an instance of ConnectionPool. |
|
| Method Summary | |
int |
addPSToPool(java.lang.String sqlStat)
Adds the PreparedStatement corresponding the input SQL string to the connection pool and returns an integer ID, which uniquely references the prepared statement. |
void |
commitAllNonTransactionConnections()
Commits all the non transaction connections. |
void |
createConnection(int num_trans,
int num_nontrans)
Creates transaction and non-transaction connections as specified by the input parameters. |
void |
disconnect()
Disconnects all connections in the connection pool. |
void |
disconnectAll()
Disconnects all connections in the ConnectionPool. |
void |
execute(java.lang.String sqlString)
Executes the input SQL string. |
int[] |
executeBatch(java.sql.PreparedStatement batchedPs)
Executes a Prepared Statement which is batched with various set of values |
java.sql.ResultSet |
executeQuery(java.sql.PreparedStatement prstmt)
Executes the SQL query of the PreparedStatement object passed and returns the result set generated by the query. |
java.sql.ResultSet |
executeQueryStmt(java.lang.String sqlstr)
Executes the input SQL query string in an already created Statement contained in ConnectionPool and returns a ResultSet. |
void |
executeTheStatement(java.lang.Object stm)
Executes the SQL query as specified by the input. |
int |
executeUpdate(java.sql.PreparedStatement prstmt)
Executes the SQL INSERT, UPDATE or DELETE statement in the PreparedStatement object passed and returns the update count(number of records that gets effected into the database). |
int |
executeUpdateStmt(java.lang.String sqlstr)
Executes the input SQL query string in an already created Statement contained in ConnectionPool. |
PreparedStatementWrapper |
fetchPreparedStatement(int id)
Fetches the preparedStatement from the pool corresponding to the given unique ID. |
void |
freeConnectionForTransaction()
Frees the connection associated with a transaction over the current thread. |
java.sql.Connection |
getConnection()
Gets a Connection from the ConnectionPool. |
java.sql.Connection |
getConnectionForTransaction()
Gets the connection already dedicated to a transaction in the current thread. |
java.sql.Connection |
getConnectionForTransaction(java.lang.Thread threadObj)
Gets the connection already dedicated to a transaction over the specified thread. |
java.lang.Integer |
getConnectionId(java.sql.Connection con)
|
java.lang.Integer |
getIdForThread(java.lang.Thread obj)
|
static ConnectionPool |
getInstance()
Gets a reference to a ConnectionPool object. |
java.sql.DatabaseMetaData |
getMetaData()
Gets comprehensive information about the database as a whole. |
java.sql.DatabaseMetaData |
getMetaData(java.sql.Connection con)
Gets comprehensive information about the database as a whole. |
int |
getNestingLevel()
Gets the number of nested transactions in the current thread. |
int |
getNumOfNonTransactionConnections()
Gets number of connections that are reserved for transactions. |
int |
getNumOfTransactionConnections()
Gets number of connections reserved for non-transactional operations. |
java.sql.PreparedStatement |
getPreparedStatement(java.lang.String sqlstr)
Gets the PreparedStatement associated with the input SQL string. |
java.sql.PreparedStatement |
getPreparedStatement(java.lang.String sqlstr,
boolean isFromPool)
Gets the PreparedStatement associated with the input SQL string. |
int |
getPreparedStatementID(java.lang.String sqlstr)
Adds the PreparedStatement corresponding the input SQL string to the pool and returns the unique identifier. |
Scheduler |
getScheduler()
Gets the scheduler filed in the connection pool. |
TransactionAPI |
getTransactionAPI()
Gets a reference to TransactionAPI. |
int |
getTransactionTimeOut()
Returns the transaction timeout value as an integer. |
void |
handleConnectionFail(java.sql.Connection conn,
java.sql.SQLException sqle)
This is used to handle the connection failure. |
boolean |
isInTimedOutThreads()
Checks if there is a timed out transaction in the current thread. |
boolean |
isInTransaction()
Checks if there is a transaction in progress in the current thread. |
boolean |
isNestedTransaction()
Checks if the current thread already has a transaction in progress. |
boolean |
isTransactionEnabled()
Returns if transaction is supported. |
boolean |
lockConnectionForTransaction(int timeout)
Gets a dedicated connection for the transaction and locks it. |
java.sql.PreparedStatement |
prepareStatement(java.lang.String sqlStat)
Returns a PreparedStatement corresponding to the input SQL string. |
java.sql.Statement |
query(java.lang.String sqlstring,
boolean flag)
Executes the SQL string, returns the java.sql.Statement in which the SQL string was executed. |
void |
refreshConnectionPool()
|
void |
resetNestingLevel(java.lang.Thread threadObj)
Sets the transaction nesting level in the input thread to zero. |
void |
returnPreparedStatement(PreparedStatementWrapper ps)
Returns the PreparedStatement for use by other database operations. |
void |
setConAuthInterfaceClassName(java.lang.String conAuth)
Sets the class name which has implemented ConnectionAuthenticationInterface. |
void |
setConnectionBreakHandler(ConnectionBreakHandler connectionBreakHandler)
Registers the specified connection break handler to the Connectionpool. |
void |
setMaxTime(long time)
Sets the timeout period for all PreparedStatements. |
void |
setNumberInCache(int num)
Sets the number of PreparedStatements to be stored in cache for ready access. |
void |
setPSCacheSize(int size)
Sets the size of cache, i.e., the number of PreparedStatements which will be stored in cache for ready access. |
void |
setScheduler(Scheduler sch)
Sets the scheduler field in the connection pool. |
void |
setStmtCacheSize(int size)
Sets the number of Statements that will be kept in cache. |
void |
setTransactionMode(boolean mode)
Sets if transaction is enabled or not. |
void |
setTransactionTimeOut(int timeOutValue)
Sets the transaction time out value with the given parameter. |
void |
startConnectionMaintainer(java.lang.String queryStr)
Instantiates a ConnectionMaintainer object to ensure that a connection in the connection pool does not get timed out (because of non-use) by the database. |
void |
updateNestingLevel(boolean increment)
Either increases or decreases the number of nested transactions in the current thread. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public java.lang.String url
public java.lang.String userName
public java.lang.String password
public java.lang.String driverName
public LogUser transUser
| Constructor Detail |
public ConnectionPool()
public ConnectionPool(java.lang.String urlstr,
java.lang.String usrname,
java.lang.String pwd,
java.lang.String drivername)
urlstr - URL nameusrname - user namepwd - passworddrivername - driver name
public ConnectionPool(java.lang.String urlstr,
java.lang.String usrname,
java.lang.String pwd,
java.lang.String drivername,
boolean enable,
int num_trans,
int num_nontrans,
java.lang.String conAuth)
urlstr - URL nameusrname - user namepwd - passworddrivername - driver nameenable - indicates whether transaction is enabled or notnum_trans - number of transaction connectionsnum_nontrans - number of non-transaction connectionsconAuth - full classname for ConnectionAuthenticationInterface
class (can be null).ConnectionAuthentication is not necessary.| Method Detail |
public TransactionAPI getTransactionAPI()
public static ConnectionPool getInstance()
Note: This method does not instantiate a new instance of ConnectionPool if it had not been instantiated before. Applications have to explicitly initialize ConnectionPool either by creating an instance of RelationalAPI (recommended) or by using one of the constructors of this class.
public java.sql.Connection getConnection()
public void disconnectAll()
public int addPSToPool(java.lang.String sqlStat)
sqlStat - the SQL String for which the PreparedStatement is to be formed.public void setNumberInCache(int num)
num - number of PreparedStatements in cache.public boolean lockConnectionForTransaction(int timeout)
timeout - transaction timeout value.
public java.sql.Connection getConnectionForTransaction()
throws UserTransactionException
UserTransactionException - if the current transaction
is timed out.
public java.sql.Connection getConnectionForTransaction(java.lang.Thread threadObj)
throws UserTransactionException
threadObj - the thread over which transaction will be performed.UserTransactionException - if the transaction
is timed out.public void freeConnectionForTransaction()
public void setMaxTime(long time)
time - timeout period in milliseconds.public void setPSCacheSize(int size)
size - number of PreparedStatementspublic void setStmtCacheSize(int size)
size - number of Statements
public java.sql.PreparedStatement prepareStatement(java.lang.String sqlStat)
throws NmsStorageException,
UserTransactionException
sqlStat - SQL string for the PreparedStatement.NmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.public boolean isInTransaction()
public boolean isInTimedOutThreads()
public int executeUpdateStmt(java.lang.String sqlstr)
throws NmsStorageException,
UserTransactionException
sqlstr - SQL query string.NmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.
public java.sql.ResultSet executeQueryStmt(java.lang.String sqlstr)
throws NmsStorageException,
UserTransactionException
sqlstr - SQL query string.NmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.
public PreparedStatementWrapper fetchPreparedStatement(int id)
throws TransactionException
id - unique identifier for the PreparedStatement.TransactionException - if the current transaction
is timed out.public void setConAuthInterfaceClassName(java.lang.String conAuth)
conAuth - instance of ConnectionAuthenticationInterface.public boolean isTransactionEnabled()
public void setTransactionMode(boolean mode)
mode - true if transaction is enabled, false otherwise.public int getNumOfNonTransactionConnections()
public int getNumOfTransactionConnections()
public void createConnection(int num_trans,
int num_nontrans)
throws NmsStorageException
num_trans - number of connections reserved for transactions.num_nontrans - number of connections reserved for non-transactions.NmsStorageException - in case of any problem while creating a connection.public void startConnectionMaintainer(java.lang.String queryStr)
queryStr - SQL string to be used by the maintainer.public void setScheduler(Scheduler sch)
sch - Scheduler object.getScheduler()public Scheduler getScheduler()
setScheduler(com.adventnet.management.scheduler.Scheduler)public boolean isNestedTransaction()
public int getNestingLevel()
public void resetNestingLevel(java.lang.Thread threadObj)
threadObj - input threadpublic void updateNestingLevel(boolean increment)
increment - true if the nesting level should be incremented,
false otherwise.
public java.sql.DatabaseMetaData getMetaData()
throws java.sql.SQLException,
UserTransactionException
java.sql.SQLException - if any SQL error occurs while
doing database operationsUserTransactionException - if the current transaction
is timed out.
public java.sql.DatabaseMetaData getMetaData(java.sql.Connection con)
throws java.sql.SQLException
con - a Connection objectjava.sql.SQLException - if any SQL error occurs while
doing database operations.
public void disconnect()
throws java.sql.SQLException
java.sql.SQLException - if any SQL error occurs while
doing database operations.
public java.sql.Statement query(java.lang.String sqlstring,
boolean flag)
throws NmsStorageException,
UserTransactionException
sqlstring - the SQL query to be executedflag - this parameter has no effectNmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.
public java.sql.ResultSet executeQuery(java.sql.PreparedStatement prstmt)
throws NmsStorageException,
UserTransactionException
prstmt - a PreparedStatement objectNmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out or rolled back.
public int executeUpdate(java.sql.PreparedStatement prstmt)
throws NmsStorageException,
UserTransactionException
prstmt - a PreparedStatement objectNmsStorageException - if any SQL error occurs while
doing database operationsUserTransactionException - if the current transaction
is timed out or rolled back.public void returnPreparedStatement(PreparedStatementWrapper ps)
ps - PreparedStatementWrapper which contains the PreparedStatement.
public java.sql.PreparedStatement getPreparedStatement(java.lang.String sqlstr,
boolean isFromPool)
throws NmsStorageException,
UserTransactionException
sqlstr - SQL string associated with the PreparedStatement.isFromPool - specifies if the PreparedStatement should be obtained from the pool.NmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.public int getPreparedStatementID(java.lang.String sqlstr)
sqlstr - SQL string value.
public java.sql.PreparedStatement getPreparedStatement(java.lang.String sqlstr)
throws NmsStorageException,
UserTransactionException
sqlstr - SQL stringNmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.
public void executeTheStatement(java.lang.Object stm)
throws NmsStorageException,
UserTransactionException
java.sql.PreparedStatement.
Otherwise, an exception is thrown.
Note:This method is used for SQL queries which do not return any
values, i.e., DDL and DCL type SQL statements.stm - either a String or a java.sql.PreparedStatement.NmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.
public void commitAllNonTransactionConnections()
throws NmsStorageException
NmsStorageException - if any SQL error occurs while
doing database operations
public void execute(java.lang.String sqlString)
throws NmsStorageException,
UserTransactionException
sqlString - SQL String to be executedNmsStorageException - if any SQL error occurs while
doing database operations.UserTransactionException - if the current transaction
is timed out.
public int[] executeBatch(java.sql.PreparedStatement batchedPs)
throws NmsStorageException,
UserTransactionException
NmsStorageException - If a database access error occurs or the driver does not support batch statements or if one of the commands sent to the database fails to execute properly or attempts to return a result set.UserTransactionException - If an error occurs while handling connection fail.public void setTransactionTimeOut(int timeOutValue)
timeOutValue - - integer representing the timeout value.getTransactionTimeOut()public int getTransactionTimeOut()
setTransactionTimeOut(int)public void setConnectionBreakHandler(ConnectionBreakHandler connectionBreakHandler)
handleConnectionFail() method of
the registered ConnectionBrakeHandler will be invoked.Web NMS
by default reads the ConnectionBreakhandler from the
nmsInterfaces.conf file in WebNMS_HOME/conf
directory from the parameter
connectionBreakHandler="Implemetation_Class_of_
ConnectionBreakHandler" and registers this with the
ConnectionPool.
connectionBreakHandler - a ConnectionBreakHandler object instance.
public void handleConnectionFail(java.sql.Connection conn,
java.sql.SQLException sqle)
con - Connection reference, which is invalid after the DBServer failure/restartsqle - SQLException raised when we try to execute a query after the DBServer is down/
after the connection is invalidpublic void refreshConnectionPool()
public java.lang.Integer getConnectionId(java.sql.Connection con)
public java.lang.Integer getIdForThread(java.lang.Thread obj)
|
AdventNet Web NMS 4 API Specification | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||