tags:

views:

1344

answers:

6

I have this code

    protected void btnUpdateAddress_Click(object sender, EventArgs e)
    {
        sdsAddressComparison.Update();
    }

that I'm using to update an oracle database. When I run the update sql code in SQL Navigator I have to type "Commit" or hit the commit button.

Do I have to code in a "Commit" somewhere in ASP.NET? and if so how and where do i do it?

+3  A: 

Normally, No you do not need the commit.

However you can write the code to use a transaction, and at the completion of the transaction you can call commit.

E.G. (Regular):

try {
        // Open connection
        dbConn.Open();
        //DB Update Code Here
    }
    catch (Exception ex) {
        throw;
    }
    finally {
        // Close database connection
        dbConn.Close();
    }

E.G. (As Transaction):

try {
        // Open connection & begin transaction
        dbConn.Open();
        dbTran = dbConn.BeginTransaction();
        
        //DB Update Code Here
        // Commit transaction
        dbTran.Commit();
    }
    catch (Exception ex) {
        // Rollback transaction
        dbTran.Rollback();
        throw;
    }
    finally {
        // Close database connection
        dbConn.Close();
    }
Brian Schmitt
A: 

Just for clarification....I'm not talking about SQL Server transactions ....I'm talking about Oracle updates which usually require a commit command when I use either SQL navigator or SQL Plus

The reason i'm posting this is because I can update this data in SQL Navigator but it doesn't update when I use ASP.NET.

Yep, you can do transactions in Oracle too.
Brian Schmitt
I know i can do transactions.....but I'm not asking about Transactions. I'm asking about how you update an Oracle table....if a commit is required and if so how do you do it.
A transaction is a general concept, not a feature specific to SQL Server (or any other RDBMS). As is any DML (select/insert/update/delete) statement.
BQ
+3  A: 

By default, your ASP.Net code, and most other client API's for databases (ODBC, OLE DB, JDBC, etc), run in auto-commit mode. That is, any time a statement is executed successfully, the result is committed. If you are running in that sort of a default mode, there is no need to explicitly commit your update.

On the other hand, there is generally a great deal to be said for putting your updates in explicit transactions-- if you ever have to issue multiple updates in order to make one logical business change, the default auto-commit mode is a very poor one. The classic example here is that if you update account A to withdraw $50 and then update account B to deposit $50 and you end up having two different transactions because of auto-commit being enabled, it is possible that the first transaction would succeed while the other transaction fails and the system loses track of $50.

So you generally want to write code similar to what Brian has demonstrated where you use transactions and issue the explicit commit. But by default, you don't have to and your updates will auto-commit.

Justin Cave
+1: Yes, and I think that autocommit is a Very Bad Thing. If the associated problems are not already obvious, then let me add that it also harms performance on Oracle.
David Aldridge
A: 

How do I check if autocommit is turned on?

That depends on the application you're using to connect to the database. Look in SQL Navigator's options dialog.
BQ
A: 

The behavior you're seeing in SQL Navigator is probably determined by an options setting.

I haven't used SQL Navigator, but I do use TOAD which is also by Quest Software. In the options dialog there, it's under View->Toad Options..., then the Oracle->Transactions node.

There's the following relevant settings:

  • [ ] Commit after every statement (checkbox)

  • When closing connections: (radio selection)

    • ( ) Commit
    • ( ) Rollback
    • ( ) Prompt for commit/rollback when changes detected, or detection is not possible due to lack of privileges on dmbs_transaction.

So you could change the setting so you don't need to hit the commit button (or type "commit"), but it's generally a bad practice since a commit is something that you should explicitly be doing (or explicitly rolling back).

BQ
A: 

Autocommit is also available in SQL*Plus.

SET AUTOCOMMIT ON
SET AUTOCOMMIT OFF

or

SET AUTOCOMMIT 100

Use SHOW AUTOCOMMIT to see the current setting.

But ... I hate this setting. You commit at the end of a meaningful unit of work, not part way through.

David Aldridge