Connection Class Reference

Represents a database connection. More...

#include <Session.h>

Collaboration diagram for Connection:

Collaboration graph
[legend]

Public Member Functions

 Connection ()
 ~Connection ()
DbRetVal open (const char *username, const char *password)
 opens connection to the database
DbRetVal close ()
 closes connection to the database and releases all the resources
DatabaseManagergetDatabaseManager ()
 gets the database manager object.
UserManagergetUserManager ()
 gets the user manager object.
DbRetVal startTransaction (IsolationLevel level=READ_COMMITTED)
 Starts a transaction.
DbRetVal commit ()
 Commits active transaction.
DbRetVal rollback ()
 Aborts the active transaction.

Detailed Description

Represents a database connection.

All database operations shall be done within the context of the connection.
Application should first create object of this class for accessing the database.
Each connection has only one active transaction at any given point of time, all
operations which happen using this connection object will be done as part of that
transaction.

Functionality:
1.Connection Management (connect and disconnect)
2.Transaction Management (start, commit, abort)
3.Provides getter methods for database manager and user manager

TODO:
1.Isolation Level support needs to be added. Currently it supports REPEATABLE READ
isolation level as the default. It will soon support READ COMMITTED and
READ UNCOMMITTED isolation levels
2.AutoCommit mode

Note:
SERIALIZABLE isolation level is not supported.

Author:
Prabakaran Thirumalai

Definition at line 61 of file Session.h.


Constructor & Destructor Documentation

Connection::Connection (  )  [inline]

Definition at line 65 of file Session.h.

00065 { session = NULL; }

Connection::~Connection (  ) 

Definition at line 21 of file Connection.cxx.

References Index::destroy(), and Session::rollback().

00022 { 
00023     if (NULL != session) {
00024        session->rollback();
00025        //session->close();
00026        delete session; 
00027        session = NULL;
00028     }
00029     Index::destroy();
00030 }

Here is the call graph for this function:


Member Function Documentation

DbRetVal Connection::close (  ) 

closes connection to the database and releases all the resources

Returns:
DbRetVal

Definition at line 55 of file Connection.cxx.

References ErrNoConnection, logFinest, logger, OK, Session::rollback(), and Logger::stopLogger().

Referenced by SqlConnection::disconnect(), handleEchoAndComment(), CacheTableLoader::load(), main(), CacheTableLoader::recoverAllCachedTables(), CacheTableLoader::unload(), and verifyPrimKeyFldVal().

00056 {
00057     if (session == NULL) return ErrNoConnection;
00058     logFinest(logger, "User logged out");
00059     logger.stopLogger();
00060     session->rollback();
00061     delete session;  // this inturn calls session->close
00062     session = NULL;
00063     return OK;
00064 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal Connection::commit (  ) 

Commits active transaction.

It makes all the changes made in the current transaction permanent and
it also releases the locks held by the current transaction.
After a transaction commits, application is required to start another
transaction for further database operations.

Returns:
DbRetVal

Definition at line 86 of file Connection.cxx.

References Session::commit(), and ErrNoConnection.

Referenced by SqlConnection::commit(), getInput(), handleTransaction(), insert(), CacheTableLoader::load(), remove(), CacheTableLoader::unload(), and verifyPrimKeyFldVal().

00087 {
00088     if (session == NULL) return ErrNoConnection;
00089     return session->commit();
00090 }

Here is the call graph for this function:

Here is the caller graph for this function:

DatabaseManager * Connection::getDatabaseManager (  ) 

gets the database manager object.

Returns:
DatabaseManager

Definition at line 66 of file Connection.cxx.

References Session::getDatabaseManager().

Referenced by getRecordsFromTargetDb(), handleEchoAndComment(), CacheTableLoader::load(), main(), SqlStatement::prepare(), CacheTableLoader::unload(), and verifyPrimKeyFldVal().

00067 {
00068     if (session == NULL) return NULL;
00069     return session->getDatabaseManager();
00070 }

Here is the call graph for this function:

Here is the caller graph for this function:

UserManager * Connection::getUserManager (  ) 

gets the user manager object.

Returns:
UserManager

Definition at line 72 of file Connection.cxx.

References Session::getUserManager().

00073 {
00074     if (session == NULL) return NULL;
00075     return session->getUserManager();
00076 }

Here is the call graph for this function:

DbRetVal Connection::open ( const char *  username,
const char *  password 
)

opens connection to the database

Parameters:
username username for authentication
password password for authentication
Returns:
DbRetVal

Definition at line 32 of file Connection.cxx.

References Conf::config, ErrAlready, ErrBadArg, Index::init(), logFinest, logger, OK, Session::open(), printError, and Logger::startLogger().

Referenced by SqlConnection::connect(), handleEchoAndComment(), CacheTableLoader::load(), main(), CacheTableLoader::recoverAllCachedTables(), CacheTableLoader::unload(), and verifyPrimKeyFldVal().

00033 {
00034     if (username == NULL || password == NULL ) 
00035     {
00036         printError(ErrBadArg, "Username or password should not be NULL\n");
00037         return ErrBadArg;
00038     }
00039     if (strlen(username) > 64 || strlen(password) >64) return ErrBadArg;
00040     if (session == NULL) session = new SessionImpl(); 
00041     else
00042     {
00043        printError(ErrAlready, "User already logged in");
00044        return ErrAlready;
00045     }
00046     DbRetVal rv = session->open(username, password);
00047     if (rv != OK) { delete session; session = NULL; return rv; }
00048     rv = logger.startLogger(Conf::config.getLogFile());
00049     if (rv != OK) { delete session; session = NULL; return rv; }
00050     logFinest(logger, "User logged in %s",username);
00051     Index::init();
00052     return OK;
00053 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal Connection::rollback (  ) 

Aborts the active transaction.

undo all the changes made in the current transaction and it also
releases the locks held by the current transaction.
After a transaction rollback, application is required to start another
transaction for further database operations.

Returns:
DbRetVal

Definition at line 93 of file Connection.cxx.

References ErrNoConnection, and Session::rollback().

Referenced by handleTransaction(), main(), remove(), and SqlConnection::rollback().

00094 {
00095     if (session == NULL) return ErrNoConnection;
00096     return session->rollback();
00097 }

Here is the call graph for this function:

Here is the caller graph for this function:

DbRetVal Connection::startTransaction ( IsolationLevel  level = READ_COMMITTED  ) 

Starts a transaction.

The previous transaction should be either committed or rollback
before startTransaction is called.
Application are required to start transaction before they attempt any
database operation.

Returns:
DbRetVal

Definition at line 78 of file Connection.cxx.

References ErrNoConnection, READ_REPEATABLE, Session::startTransaction(), and WRITE_OSYNC.

Referenced by SqlConnection::beginTrans(), insert(), CacheTableLoader::load(), remove(), CacheTableLoader::unload(), and verifyPrimKeyFldVal().

00079 {
00080     if (session == NULL) return ErrNoConnection;
00081     if (level == WRITE_OSYNC) level = READ_REPEATABLE;
00082     return session->startTransaction(level);
00083 }

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 22:44:59 2008 for csql by  doxygen 1.4.7