Link to home
Start Free TrialLog in
Avatar of udir
udir

asked on

Active Records + transaction...

Hi,
I need to build a transaction using AR, I wrote :
        public int DeleteFunc()
        {
             TransactionScope DelTran = new TransactionScope();
            int res = 0;
            try
            {
                 //INSERT THE ROW INTO USERS
                 string DataGridInsertCommand = "INSERT INTO USERS SELECT * "
                                                + "FROM USERS "
                                                + "WHERE USERS .USERS_ID = 1" ;
                Database DataGriddb = DatabaseFactory.CreateDatabase("connection");
                DataGriddb.ExecuteNonQuery(CommandType.Text, DataGridInsertCommand);

                //DELETE THE ROW FROM USERS
                string DataGridDeleteCommand = "DELETE FROM USERS "
                                             + "WHERE USERS.USERS _ID = 1" ;

                Database DataGriddb1 = DatabaseFactory.CreateDatabase("connection");
                res = DataGriddb1.ExecuteNonQuery(CommandType.Text, DataGridDeleteCommand);

                DelTran.VoteCommit();
            }
            catch (Exception ex)
            {
                DelTran.VoteRollBack();
            }
            return res;
        }

But the rollBack doesn't works.
Whats wrong?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

1) Is the TransactionScope necessary?

2) Are you distributing transactions across different system types?

3) Seems like all you need is an IDbTransaction.

Bob
Avatar of udir
udir

ASKER

Hi,
It is new for me (AR + transaction)
Can u give an example of how to use IDbTransaction?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of udir

ASKER

Hi Bob,
What i didn't found is how to implement the :
IDbTransaction transaction = connection.BeginTransaction();

as i wrote, i use :
Database DataGriddb = DatabaseFactory.CreateDatabase("connection");
and the "connection" define (at the global.asax) like that :
try
        {
            configPath = ConfigurationManager.AppSettings["configPath"];
            source = new XmlConfigurationSource(configPath);
            ActiveRecordStarter.Initialize(typeof(USERS).Assembly, source);
        }

So, how can i implement that connection at the  
IDbTransaction transaction = connection.BeginTransaction();

Thanks