PC Review


Reply
Thread Tools Rate Thread

C# File + DB Transaction

 
 
Chris Fink
Guest
Posts: n/a
 
      29th Oct 2008
I have the following requirements and am looking to make this into a c#
transaction.

1. Read from a file into a memory stream
2. Insert the stream into a DB
3. Delete the file

Is it possible to perform a transaction on both a File action and a DB
insert?

A transaction will ensure that if an error occurs in step 3, the insert will
not commit, etc. All three steps must succeed, otherwise the transaction
should rollback and notify of an error.

Any assistance is appreciated.

Thanks

 
Reply With Quote
 
 
 
 
*Lysander*
Guest
Posts: n/a
 
      29th Oct 2008
Chris Fink schrieb:

> 1. Read from a file into a memory stream
> 2. Insert the stream into a DB
> 3. Delete the file


If you want to have all of that in ONE transaction...
You can use the transaction-object provided by your database provider
and wrap the commit()/rollback() into a try/catch.

With the Firebird-Provider it would look similar to this:

(Conn01 is a valid and open connection...)

FBTransaction Transaction1 = Conn01.BeginTransaction();

try
{
... read file into memory stream
... Insert the stream into DB
... Delete the file
Transaction1.Commit()
}
catch
{
Transaction1.Rollback()
... show errormessages
}


I would consider, though, to put at least the "read file into stream"-
part outside the database transaction. Why bother the database with a
transaction if you probably can't read or even find the file.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      30th Oct 2008
Chris Fink wrote:
> I have the following requirements and am looking to make this into a c#
> transaction.
>
> 1. Read from a file into a memory stream
> 2. Insert the stream into a DB
> 3. Delete the file
>
> Is it possible to perform a transaction on both a File action and a DB
> insert?
>
> A transaction will ensure that if an error occurs in step 3, the insert will
> not commit, etc. All three steps must succeed, otherwise the transaction
> should rollback and notify of an error.


BEGIN
INSERT
if delete file ok then
COMMIT
else
ROLLBACK
end if

may be good enough if you are sufficient sure that the DB transaction
will commit.

But if you really need a single transaction, then you will need:
- a file system that supports XA transactions
- a transaction manager to do 2PC

I would seriously consider the third option of having logic that
checks whether the file has already been inserted before doing
INSERT and then just do your 3 steps after that.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error message 3003 couldn't start transaction, to many transaction =?Utf-8?B?am9rb2Jl?= Microsoft Access Form Coding 4 24th Aug 2006 09:14 AM
Detect if Distributed Transaction is used or just SQL 2005 internal transaction? John Lee Microsoft C# .NET 2 10th May 2005 04:25 AM
"Distributed transaction completed. Either enlist this session in a new transaction or the NULL transaction." Ollie Riches Microsoft C# .NET 3 11th Mar 2005 05:23 PM
"Distributed transaction completed. Either enlist this session in a new transaction or the NULL transaction." Ollie Riches Microsoft ADO .NET 3 11th Mar 2005 05:23 PM
The root transaction wanted to commit, but transaction aborted Grober Microsoft ADO .NET 1 24th Nov 2003 02:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:37 AM.