Poco::Data

class Transaction

File Information

Library: Data
Package: DataCore
Header: Poco/Data/Transaction.h

Description

Transaction helps with transactions in domain logic. When an Transaction object is created, it first checks whether a transaction is in progress. If not, a new transaction is created. When the Transaction is destroyed, and commit() has been called, nothing is done. Otherwise, the current transaction is rolled back. See Transaction for more details and purpose of this template.

Member Summary

Member Functions: commit, execute, getIsolation, hasIsolation, isActive, isIsolation, rollback, setIsolation, setLogger, transact

Constructors

Transaction

Transaction(
    Poco::Data::Session & session,
    Poco::Logger * pLogger = 0
);

Creates the Transaction and starts it, using the given database session and logger.

Transaction

Transaction(
    Poco::Data::Session & session,
    bool start
);

Creates the Transaction, using the given database session. If start is true, transaction is started, otherwise begin() must be called to start the transaction.

Transaction inline

template < typename T > Transaction(
    Poco::Data::Session & rSession,
    T & t,
    Poco::Logger * pLogger = 0
);

Creates the Transaction, using the given database session, transactor and logger. The transactor type must provide operator () overload taking non-const Session reference as an argument.

When transaction is created using this constructor, it is executed and commited automatically. If no error occurs, rollback is disabled and does not occur at destruction time. If an error occurs resulting in exception being thrown, the transaction is rolled back and exception propagated to calling code.

Example usage:

struct Transactor { void operator () (Session& session) const {

// do something ...

} };

Transactor tr; Transaction tn(session, tr);

Destructor

~Transaction

~Transaction();

Destroys the Transaction. Rolls back the current database transaction if it has not been commited (by calling commit()), or rolled back (by calling rollback()).

If an exception is thrown during rollback, the exception is logged and no further action is taken.

Member Functions

commit

void commit();

Commits the current transaction.

execute

void execute(
    const std::string & sql,
    bool doCommit = true
);

Executes and, if doCommit is true, commits the transaction. Passing true value for commit disables rollback during destruction of this Transaction object.

execute

bool execute(
    const std::vector < std::string > & sql
);

Executes all the SQL statements supplied in the vector and, after the last one is sucesfully executed, commits the transaction and returns true. If an error occurs during execution, transaction is rolled back and false is returned. Passing true value for commit disables rollback during destruction of this Transaction object.

execute

bool execute(
    const std::vector < std::string > & sql,
    std::string * info
);

Executes all the SQL statements supplied in the vector and, after the last one is sucesfully executed, commits the transaction and returns true. If an error occurs during execution, transaction is rolled back false is returned and info pointer is assigned to a std::string containing the error message. Passing true value for commit disables rollback during destruction of this Transaction object.

getIsolation inline

Poco::UInt32 getIsolation();

Returns the transaction isolation level.

hasIsolation inline

bool hasIsolation(
    Poco::UInt32 ti
);

Returns true iff the transaction isolation level corresponding to the supplied bitmask is supported.

isActive inline

bool isActive();

Returns false after the transaction has been committed or rolled back, true if the transaction is ongoing.

isIsolation inline

bool isIsolation(
    Poco::UInt32 ti
);

Returns true iff the transaction isolation level corresponds to the supplied bitmask.

rollback

void rollback();

Rolls back the current transaction.

setIsolation inline

void setIsolation(
    Poco::UInt32 ti
);

Sets the transaction isolation level.

setLogger inline

void setLogger(
    Poco::Logger * pLogger
);

Sets the logger for this transaction. Transaction does not take the ownership of the pointer.

transact inline

template < typename T > void transact(
    T & t
);

Executes the transactor and, unless transactor throws an exception, commits the transaction.