On Feb 21, 10:31 am, "Arnie" wrote:
> "Milind" wrote in message
>
>
>
>
>
> > Hi There,
>
> > I have developed the ODBC app which can work with Oracle as well as
> > MSSQL server databases. I need to use transactions at some level. By
> > setting AUTOCOMMIT OFF and using SET TRANSACTION and BEGIN WORK,
> > COMMIT WORK everything works fine with Oracle. But with MSSQL server
> > does not. Do I need to code something special for MSSQL? Does anyone
> > has example on how to implement transactions with MSSQL server using
> > ODBC?
>
> > Any help with appreciated. Thanks in advance.
>
> > Milind
>
> The following C++ code works with Oracle, SQL Server 2000 and DB2.
>
> - Arnie
>
> /////////////////////////////////////////////////////////////////////////////
>
> //
>
> // Auto commit is ON by default. We turn it OFF for an explicit transaction.
>
> //
>
> void VRMSDatabase::StartTransaction( void )
>
> {
>
> SQLRETURN rc = SQLSetConnectAttr( m_hDbc, SQL_ATTR_AUTOCOMMIT,
>
> SQL_AUTOCOMMIT_OFF, 0 );
>
> CHECK_DBC( m_hDbc, rc );
>
> m_InTransaction = true;
>
> }
>
> /////////////////////////////////////////////////////////////////////////////
>
> //
>
> // Commit the transaction and turn auto commit back on
>
> //
>
> void VRMSDatabase::Commit( void )
>
> {
>
> if ( m_InTransaction )
>
> {
>
> SQLRETURN rc = SQLEndTran( SQL_HANDLE_DBC, m_hDbc, SQL_COMMIT );
>
> CHECK_DBC( m_hDbc, rc );
>
> m_InTransaction = false;
>
> rc = SQLSetConnectAttr( m_hDbc, SQL_ATTR_AUTOCOMMIT,
>
> (SQLPOINTER) SQL_AUTOCOMMIT_ON, 0 );
>
> }
> }
>
> /////////////////////////////////////////////////////////////////////////////
>
> //
>
> // Rollback the transaction and turn auto commit back on
>
> //
>
> void VRMSDatabase::Rollback( void )
>
> {
>
> if ( m_InTransaction )
>
> {
>
> SQLRETURN rc = SQLEndTran( SQL_HANDLE_DBC, m_hDbc, SQL_ROLLBACK );
>
> CHECK_DBC( m_hDbc, rc );
>
> m_InTransaction = false;
>
> rc = SQLSetConnectAttr( m_hDbc, SQL_ATTR_AUTOCOMMIT,
>
> (SQLPOINTER) SQL_AUTOCOMMIT_ON, 0 );
>
>
>
> }
> }- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
Hi Arnie,
Thanks for help.
I guess your suggestion will help me where I have to use 'BEGIN WORK'
and 'COMMIT WORK'. I do set AUTO_COMMIT OFF as soon as I make the
connection. So I also need see how my existing code will behave when I
do not set the AUTO_COMMIT OFF.
Also I have a problem with 'SET TRANSACTION READ ONLY' where I want
to read the same table data multiple times in one transaction and when
I read second or third time it should not do the dirty reads (the same
data should be read all the SELECTs till the TRANSACTION does not
end). Do you know how can I achive this?
Thanks,
Milind
>> Stay informed about: MSSQL ODBC And Transactions