none
What's happening behind scene in SqlFileStream under transaction? RRS feed

  • Question

  • Hello,


    I am using SQLFileStream to read data from FILESTREAM enabled database.

    Here is a brief code..

    SqlTransaction tran = connection.BeginTransaction....
    command.Transaction = tran;

    SqlFileStream sfs = new SqlFileStream(path, transactionContext,
                                System.IO.FileAccess.Read);

    //Read Stream


    tran.Commit();

    My question is that what exactly means about the transaction Complete/or roll-back under the transaction of SQL "Read" File stream?

    I meant, for example, if there was Update T-Sql under a transaction, and it was rolled-back under that transaction, then it would be obvious that data wouldn't be updated.

    Well, obviously, the file resource would be locked until Complete or RollBack, but, other than resource locking/releasing, anything would affect to the SQL Read file stream??..

    I meant let's say..we have something like this code..

    SqlFileStream sfs = new SqlFileStream(path, transactionContext,
                                System.IO.FileAccess.Read);

    byte[] buffer = new byte[...];

    while ( read = sfs.Read(buffer...) > 0) {

           stream.Write(buffer, 0, read)...

    }

    let's say that we read about 500 bytes, and then...some reason. the transaction was roll-back by trans.Rollback()..

    In this case, the byte array we've read up to 500 bytes before rolling back...would be dropped/zeroed because the transaction was rolled-back????? I think the byte array we've read up to will be still available regardless of Roll-back or not...Right?

    Thanks,

    Friday, February 28, 2014 11:02 PM

Answers

  • Hello,

    >> I think the byte array we've read up to will be still available regardless of Roll-back or not...Right?

    Yes, you can check this link:

    http://msdn.microsoft.com/en-us/library/cc716724(v=vs.110).aspx

    It shows an example using SqlFileStream. We can see that it will use SqlReader to read the file context to local:

    byte[] transactionContext = reader.GetSqlBytes(1).Buffer;

    If there is any error occuring at this time, then it will rollback. Or it will load the specific file context to local into the transactionContext. So actually this line “SqlFileStream sfs = new SqlFileStream(path, transactionContext,System.IO.FileAccess.Read);” does not associaton with the transaction, it just initializes an instance of SqlFileStream.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 3, 2014 7:32 AM
    Moderator