Forum Moderators: open
I have a file that is being written to just before a customer goes to my payment processing company to pay for their order.
The file logs the order details and items that were in the customer's shopping cart in case something happens in the Payment Notification routine that is called after the customer pays for their order. The shopping cart is stored as a session variable so this is a way to find the shopping cart details if something goes wrong.
My question is about simultaneous writes to the file and what I should do to prevent problems when two people checkout at the same time. This is my code - I'm using ASP.NET (I'm not too familiar with writing to files so any help would be appreciated)...
Try
'the following filestream will open the file if it exists or create a new file
'if it doesn't exist
Dim fs As New FileStream(strPath, FileMode.OpenOrCreate)
Dim sw As New StreamWriter(fs)
'the next line of code is needed in order for the streamwriter to append the
'lines written to the end of the file
sw.BaseStream.Seek(0, SeekOrigin.End)
'write the pending order information to the file
sw.WriteLine(str)
'flush and close the file
sw.Flush()
sw.Close()
Catch DefaultExc As Exception
Err.Clear()
End Try
korkus2000,
If i recall, you are on asp.net, if that is so,
then everytime you want to write to the file, grab
an application object lock. That will effectively
serialize all writes to the file. Any request to
write to the file just queues up, so that page will
take longer to be returned to the client but will
eventually succeed. For reading, just don't bother
grabbing the lock.
Make sure you open the file in write/shared read mode.
(see docs)
+++