Enterprise Information Portal APIs

com.ibm.gcs.db.util
Class ConnectionManager

java.lang.Object
  |
  +--com.ibm.gcs.db.util.ConnectionManager

public class ConnectionManager
extends java.lang.Object

The ConnectionManager serves 3 functions:

  1. Reads values from a database properties file specifying the database where the url tables are located.
  2. Establishes the JDBC connection to the specified database.
  3. Acts as a monitor to synchronize all threads using the same connection.

As part of its initialization, ConnectionManager constructs an instance of itself using the values in a file called "db2properties.txt". [Note: In future versions this intialization might create more than one instance (one per per database, for example).] This properties file contains "driver", "database", "user", and "password" properties. A sample db2 properties file looks as follows:

 # The full classname of the JDBC driver to load.
 driver = COM.ibm.db2.jdbc.app.DB2Driver
 #The URL of the server and database to connect to
 database = jdbc:db2:urlpool
 #The name of the database user account
 user = gcs
 #The password for the database user account
 password= gcs
All objects that need to use a database connection must get the connnection through a ConnectionManager object. Moreover, they must obtain the instance of this object through the static method getManager(). This ensures that only one ConnectionManager instance exists per database connection. Objects may access the database connection object using getConnection(). Any object that successfully obtains a connection must return it through releaseConnection(int) which indicates whether or not the connection is being returned in an erroneous state. If the connection state is in error, the ConnectionManger closes it and establishes a new connection. [Eventually, should have a watchdog thread to detect timed out connections.]


Method Summary
 java.sql.Connection getConnection(int level)
          In order to obtain a JDBC connection, the application must request it from the ConnectionManager by calling this method.
 java.lang.String getDBName()
          Get the database instance name.
static ConnectionManager getManager()
          Returns a reference to the ConnectionManager object created at initialization with the default db2 properties.
 int getMaxSqlBytes()
           
 java.lang.String getPassword()
          Get the user password.
 java.lang.String getUser()
          Get the database user name.
static void initialize()
          Initializes the ConnectionManager using the default properties file: db2properties.txt
static void initialize(java.lang.String filename)
          Create a new instance of the ConnectionManager using the values in a properties file.
static void initialize(java.lang.String driver, java.lang.String database, java.lang.String user, java.lang.String password)
          Create a new instance of the ConnectionManager using the specified parameters.
static void main(java.lang.String[] args)
           
 void releaseConnection(int error)
          The application object calls this method to return the connection that it has obtained.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getManager

public static ConnectionManager getManager()
Returns a reference to the ConnectionManager object created at initialization with the default db2 properties. All threads must use this method to obtain the ConnectionManager object so share the same instance and hence the same connection.
NOTE: Future versions should enable an object to get a ConnectionManager based on dbname, username, password, etc.
Returns:
ConnectionManager The object through which objects obtain the database connection.

getConnection

public java.sql.Connection getConnection(int level)
                                  throws TransactionException
In order to obtain a JDBC connection, the application must request it from the ConnectionManager by calling this method. If no free connections are available, the caller waits. The caller is responsible for releasing the connection. Failure to release it will result in deadlock within the application. [Currently, ConnectionManager has no mechanism to detect deadlock so will not recover.]
Parameters:
level - The Transaction Isolation level for the connection. (Defined in java.sql.Connection.)
Returns:
java.sql.Connection A java.sql.Connection object.

releaseConnection

public void releaseConnection(int error)
The application object calls this method to return the connection that it has obtained. If an error occurred as a result of a bad connection, the caller indicates that the connection is in error by returning error code -1. Otherwise, the caller returns the connection with error code 0.
Parameters:
int - error -1 if connection is in error, 0 if connection is okay.

getUser

public java.lang.String getUser()
Get the database user name.
Returns:
String The database user name.

getPassword

public java.lang.String getPassword()
Get the user password.
Returns:
String The database user password.

getMaxSqlBytes

public int getMaxSqlBytes()
Returns:
int The maximum number of bytes per SQL insert or update statement (by default 31K).

getDBName

public java.lang.String getDBName()
Get the database instance name.
Returns:
String The database instance name.

initialize

public static void initialize()
Initializes the ConnectionManager using the default properties file: db2properties.txt

initialize

public static void initialize(java.lang.String filename)
Create a new instance of the ConnectionManager using the values in a properties file.

initialize

public static void initialize(java.lang.String driver,
                              java.lang.String database,
                              java.lang.String user,
                              java.lang.String password)
Create a new instance of the ConnectionManager using the specified parameters. This method should only be called once.
Parameters:
driver - The full classname of the jdbc driver to use (e.g., COM.ibm.db2.jdbc.app.DB2Driver)
database - The uri of the database to connect to (e.g., jdbc:db2:eip ).
user - The name of the user (e.g., db2admin)
password - The password to use (e.g., foobar )

main

public static void main(java.lang.String[] args)

EIP Web Crawler APIs

(c) Copyright International Business Machines Corporation 1996, 2002. IBM Corp. All rights reserved.