Results 1 to 6 of 6

Thread: Transactions

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Transactions

    I have been using transactions for a while in .net but have hit a snag.

    How can I implement a transaction when 1 of my processes is not database driven ?

    I basically want to insert a record (fine here) and delete a file from the documents folder.

    How do I wrap these up in a transaction ?

    Parksie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Transactions

    You can wrap the database operation in two nested transactions. You commit the first then, if that succeeds, you perform the IO operation. If that succeeds you then commit the second transaction. This is not 100% safe as there is a miniscule possibility that the second commit might fail, but the only reason I can see for that would be if the connection to the database is lost for some reason.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Transactions

    Here's example code without the error handling.
    VB Code:
    1. Dim outerTransaction As OleDbTransaction = connection.BeginTransaction()
    2.         Dim innerTransaction As OleDbTransaction = outerTransaction.Begin()
    3.  
    4.         command.Transaction = innerTransaction
    5.  
    6.         command.ExecuteNonQuery()
    7.         innerTransaction.Commit()
    8.  
    9.         'Perform other operation here.
    10.  
    11.         outerTransaction.Commit()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Transactions

    I'm not sure how an OLEDbTransaction fits inb to the process of deleting a file.

    It's not a database operation.

    Parksie

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Transactions

    It's not really possible to have a SQL DB transaction rely on a file operation outside the database. As JM said, the TRANSACTION would rollback automatically if the DB connection got lost during the "file delete" operation.

    If this is MS SQL Server, you could write an EXTENDED STORED PROCEDURE that does the file delete - I've never done anything like this, but it should be possible.

    Have you considered adding a DB flag field and changing that DB flag value to indicate that the "delete" transaction was initiated, then delete the file, then set the DB flag to indicate that the delete was successful?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Transactions

    Quote Originally Posted by venerable bede
    I'm not sure how an OLEDbTransaction fits inb to the process of deleting a file.

    It's not a database operation.
    The OleDbTransaction can't control anything outside of database operations. That's the whole point of using two nested transactions. You commit the inner transaction first. If that succeeds then you know that all your database operations will succeed, but the data is not fully committed until you commit the outer transaction. That means that you can go ahead and perform whatever other operation you want to perform, in your case deleting the file. If that fails then you roll back the outer transaction and no data will be committed to the database. If it succeeds you commit the outer transaction and your database operations become final.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width