SourcePro® API Reference Guide

 
List of all members | Public Types | Public Member Functions
RWDBConnection Class Reference

Represents an explicit database connection object that can be used in place of the implicit database connection provided by RWDBDatabase. More...

#include <rw/db/connect.h>

Public Types

enum  ConnectionType { Synchronous, Asynchronous }
 
enum  IsolationType { Unknown, ANSILevel1, ANSILevel2, ANSILevel3 }
 

Public Member Functions

 RWDBConnection ()
 
 RWDBConnection (const RWDBConnection &connection)
 
void acquire (void) const
 
RWDBStatus beginTransaction (const RWCString &name=RWCString())
 
RWDBStatus close ()
 
RWDBStatus commitTransaction (const RWCString &name=RWCString())
 
ConnectionType connectionType () const
 
RWDBDatabase database () const
 
RWDBStatus::ErrorHandler errorHandler () const
 
RWDBResult executeSql (const RWCString &sqlProgram) const
 
RWDBStatus isolation (IsolationType level)
 
IsolationType isolation () const
 
bool isReady () const
 
bool isValid () const
 
RWDBStatus open ()
 
bool operator!= (const RWDBConnection &conn) const
 
RWDBConnectionoperator= (const RWDBConnection &connection)
 
bool operator== (const RWDBConnection &conn) const
 
void release (void) const
 
RWDBStatus rollbackTransaction (const RWCString &name=RWCString())
 
void setErrorHandler (RWDBStatus::ErrorHandler handler)
 
RWDBStatus setSavepoint (const RWCString &name)
 
RWDBStatus status () const
 
RWDBSystemHandlesystemHandle () const
 
const RWZonetimeZone () const
 
const RWZonetimeZone (const RWZone *zone)
 

Detailed Description

Connections represent a scarce resource that applications can allocate and manage themselves. An RWDBConnection is an object that may be requested from an RWDBDatabase, and passed to many methods to specify that the method should use the given connection. Operations requested without supplying a connection are performed using a connection supplied invisibly by RWDBDatabase. If none are available, an error results. Thus, the use of explicit connections eliminates one likely source of errors, at some cost in program complexity.

Each RWDBConnection knows which RWDBDatabase produced it, and each RWDBDatabase knows the connections it has produced.

RWDBConnection is designed around the Interface/Implementation paradigm. An RWDBConnection instance is an interface to a reference-counted implementation; copy constructors and assignment operators produce additional references to a shared implementation. An RWDBConnection implementation is a base class from which a family of database-specific connection implementations is derived.

Synopsis
#include <rw/db/connect.h>
RWDBConnection connection = myDbase.connection();

DB XA Module

In an XA environment, connections are opened and managed by the transaction processing monitor, based on criteria defined during the set-up phase of the resource manager. An RWDBConnection in this environment does not have the responsibility of opening and closing a database connection directly with the server. Instead, it attempts to get a valid handle to a connection previously opened by the TPM. This handle is held, and used whenever it is needed.

Even though the TPM actually establishes connections, your programs will still request RWDBConnection objects from an RWDBDatabase.

The DB Interface Module restricts the number of RWDBConnection objects you can create. This is because the DB Interface Module makes use of connections opened by the TPM. Therefore, you should be careful when using default connections. If the number of connections requested is greater than one, the DB Interface Module returns a RWDBStatus::notSupported error (with the exception of Sybase).

Note
We recommend that you always use explicit connections.

Because the TPM actually opens and closes connections, the following RWDBConnection member functions should not be called when using the DB Interface Module: open() and close().

Transactions in an XA environment are controlled by the TPM. Therefore, the following methods should not be called in an XA connection: beginTransaction(), commitTransaction(), and rollbackTransaction().

Member Enumeration Documentation

These are the values used for the connectionType() methods:

Enumerator
Synchronous 

Connection type is set to Synchronous.

Asynchronous 

Connection type is set to Asynchronous.

These are the values used for the isolation() methods:

Enumerator
Unknown 

Isolation level is set to Unknown.

ANSILevel1 

Isolation level is set to ANSI Level 1.

ANSILevel2 

Isolation level is set to ANSI Level 2.

ANSILevel3 

Isolation level is set to ANSI Level 3.

Constructor & Destructor Documentation

RWDBConnection::RWDBConnection ( )

The default constructor creates an RWDBConnection with a status of RWDBStatus::notInitialized. This constructor is provided as a convenience, for example, for declaring an array of RWDBConnection instances. Usable RWDBConnection objects are obtained from an RWDBDatabase.

RWDBConnection::RWDBConnection ( const RWDBConnection connection)

Copy constructor. The created object shares an implementation with connection.

Member Function Documentation

void RWDBConnection::acquire ( void  ) const

Attempts to acquire the internal mutex lock. If the mutex is already locked by another thread, the function blocks until the mutex is released. This function can be called from a const object.

Note
In single-threaded builds, this function evaluates to a no-op.
RWDBStatus RWDBConnection::beginTransaction ( const RWCString name = RWCString())

Sends a begin transaction, or the vendor-specific equivalent, to the database server. Here name means the name of the transaction, if you choose to supply it and the vendor supports named transactions. This function can behave asynchronously if executed using an asynchronous connection.

DB XA Module: Returns a RWDBStatus::notSupported error.

RWDBStatus RWDBConnection::close ( )

Closes this connection. This method should only be used in exceptional circumstances, as it causes all references to this connection to become unusable. It is usually best to allow connections to close themselves as they go out of scope.

DB XA Module: Returns a RWDBStatus::notSupported error.

RWDBStatus RWDBConnection::commitTransaction ( const RWCString name = RWCString())

Sends a commit transaction, or the vendor-specific equivalent, to the database server. Here name means the name of the transaction, if you choose to supply it and the vendor supports named transactions. This function can behave asynchronously if executed using an asynchronous connection.

DB XA Module: Returns a RWDBStatus::notSupported error.

ConnectionType RWDBConnection::connectionType ( ) const

Returns the connection type of self. An RWDBConnection can be Synchronous or Asynchronous.

RWDBDatabase RWDBConnection::database ( ) const

Provides access to the RWDBDatabase that produced the connection.

RWDBStatus::ErrorHandler RWDBConnection::errorHandler ( ) const

Returns the error handler attached to self.

RWDBResult RWDBConnection::executeSql ( const RWCString sqlProgram) const

The SQL statement(s) in sqlProgram are submitted to the connection for execution. The DB Interface Module does not check the contents of sqlProgram for validity. The return value represents a sequence of zero or more RWDBTable objects. This function can behave asynchronously if executed using an asynchronous connection.

RWDBStatus RWDBConnection::isolation ( IsolationType  level)

Makes a database-specific call or calls to set self's isolation level to the specified level. The details vary widely among database vendor implementations. This function can behave asynchronously if executed using an asynchronous connection. See your DB Access Module guide and your database vendor's manual for exact details on isolation levels.

IsolationType RWDBConnection::isolation ( ) const

Returns self's isolation level, as defined by the most recent call to isolation(IsolationType). If isolation(IsolationType) has not been called, returns Unknown. In this case, self's isolation level is whatever the database vendor provides by default.

bool RWDBConnection::isReady ( ) const

This function returns true if the object is in ready state, indicating that accessing the object will not block. Accessing a nonready object may potentially block.

bool RWDBConnection::isValid ( ) const

Returns true if the status of this connection is RWDBStatus::ok, otherwise returns false.

RWDBStatus RWDBConnection::open ( )

Reopens this connection after closing or an error condition. If the connection cannot be opened for use, both the status returned and the connection will be invalid.

DB XA Module: Returns a RWDBStatus::notSupported error.

bool RWDBConnection::operator!= ( const RWDBConnection conn) const

Returns true if self and conn do not share the same implementation. Otherwise, returns false.

RWDBConnection& RWDBConnection::operator= ( const RWDBConnection connection)

Assignment operator. Self shares an implementation with connection.

bool RWDBConnection::operator== ( const RWDBConnection conn) const

Returns true if self and conn share the same implementation. Otherwise, returns false.

void RWDBConnection::release ( void  ) const

Releases a previously acquired mutex. This function can be called from a const object.

Note
In single-threaded builds, this function evaluates to a no-op.
RWDBStatus RWDBConnection::rollbackTransaction ( const RWCString name = RWCString())

Sends a rollback transaction, or the vendor-specific equivalent, to the database server. Here name means the name of the transaction, if you choose to supply it and the vendor supports named transactions. This function can behave asynchronously if executed using an asynchronous connection.

DB XA Module: Returns a RWDBStatus::notSupported error.

void RWDBConnection::setErrorHandler ( RWDBStatus::ErrorHandler  handler)

Installs handler as the error handler for self. The supplied handler is called whenever the status of self changes to anything except RWDBStatus::ok. By default, the RWDBStatus::ErrorHandler is inherited from the RWDBDatabase that produced self; this method overrides the default.

RWDBStatus RWDBConnection::setSavepoint ( const RWCString name)

Sends a savepoint name or the vendor-specific equivalent to the database server.

RWDBStatus RWDBConnection::status ( ) const

Provides the current status of this connection.

RWDBSystemHandle* RWDBConnection::systemHandle ( ) const

This member allows an application to gain access to the underlying vendor-specific database API. The result is a pointer to the base class RWDBSystemHandle, which must be cast by the application to a derived class representing a database-specific system handle. This can be done in a type-safe manner if your compiler supports runtime type identification. See your DB Access Module guide for details on the name and contents of the database-specific system handle variants.

DB XA Module: See the chapter on using the DB access modules with XA functionality in the DB XA Module User's Guide.

const RWZone& RWDBConnection::timeZone ( ) const

Returns a reference to the time zone set on self. If the time zone is not set, returns RWZone::local(). When self is produced, it inherits the time zone setting from the producer RWDBDatabase object. If the time zone was not set on the RWDBDatabase object at the time, the time zone is not set on self.

See also
timeZone(const RWZone*) for setting the time zone and information on how the time zone setting is used.
Note
This method does not fetch the time zone set on the database server or the database client.
const RWZone* RWDBConnection::timeZone ( const RWZone zone)

Sets the time zone used when extracting the date and time data from the RWDateTime objects sent to the database, and when creating the RWDateTime objects from the date and time data retrieved from the database. When the time zone is not set, RWZone::local() is used. When self is produced, it inherits the time zone setting from the producer RWDBDatabase object. If the time zone was not set on the RWDBDatabase object at the time, the time zone is not set on self.

Parameters
zoneThe time zone to be set on self. Pass NULL to unset the time zone setting.
Returns
The previous time zone set on self. Returns NULL when the time zone was not set.
Note
Self retains a reference to zone. It is the application's responsibility to ensure that zone has a lifetime greater than self or any object using self.
This method does not set the time zone on the database server or the database client.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.