locking an XML file...will it create a queue?

D

darrel

We're having an odd problem with our home grown CMS once in a great while,
an XML file will become corrupt...either missing, or only half-written.

We think something is happening when two people happen to hit the 'write
XML' function at exactly the same time and both process vie to write the
file first.

As such, we need to fix that. William in this forum gave me a nice example:
Additionally, I believe you can lock (for writing by anyone else until you
close the file) the xml file you are writing to by using the
IO.FileShare.Read property in the sample code below

Dim fs As New System.IO.FileStream("YourXmlFile.xml", IO.FileMode.Open,
IO.FileAccess.Write, IO.FileShare.Read)
Dim xmltw As New System.Xml.XmlTextWriter(fs,
System.Text.Encoding.Default)
xmltw.WriteString("Your xml text")

My question, since we really can't figure out how to test this issue, is
what will happen if I do lock the file and two process do want to write the
file. Will they queue up?

Other question...do I need to 'release' the file when done? My XML writing
function simple ends with:

objXMLWriter.WriteEndElement() 'close menuItems node
objXMLWriter.Flush()
objXMLWriter.Close()

Is there anything else I need to do to release the file for the next process
to write to it?

-Darrel
 
W

William LaMartin

All I know is what the Visual Studio Help says about the topic
System.IO.FileShare enumeration. Check it out.
 
G

Guest

If a person tries to write to the file whilst it is locked it will probably
throw some kind of exception because the OS will generate an access denied
type error. When you close a file it will no longer be locked.
 
D

darrel

If a person tries to write to the file whilst it is locked it will
probably
throw some kind of exception because the OS will generate an access denied
type error. When you close a file it will no longer be locked.

In that case, any suggestions? We obviously don't want to have a corrupt
file due to two people trying to save at the exact same time. At the same
time, we don't really want one person to get an error if the other person is
writing to it either.

-Darrel
 
S

sirfunusa

Simply catch the exception, and display a friendly message to the user.
Or sleep for a bit, and try again.
 
G

Guest

Yeah as sirfunusa says, catch the exception and deal with it how you wish.
The best you can hope for really though is that exception is caught and dealt
with, then when the second users save goes ahead they will imidiately
overwrite all the changes the first user made.

To test catching the exception i would somehow keep the file open and locked
and then try and update it and see what it does, then deal with catching the
exception.
 
D

darrel

Simply catch the exception, and display a friendly message to the user.

Ah! Of course. Duh. ;o)
Or sleep for a bit, and try again.

Can't tell if 'sleep' is a .net technical term or just a toungue-in-cheek
remark. ;o)
 
D

darrel

Yeah as sirfunusa says, catch the exception and deal with it how you wish.
The best you can hope for really though is that exception is caught and
dealt
with, then when the second users save goes ahead they will imidiately
overwrite all the changes the first user made.

Hmm...is there any way via javascript to have a timed postback? I'm thinking
catch the error, and, if it errors out, just send a 'please wait' message
back to the browser with a script that will then try the postback again in
10 seconds or so.

-Darrel
 
D

darrel

Hmm...is there any way via javascript to have a timed postback?

It looks like there is:

<SCRIPT language=javascript>
<!--
setTimeout( "__doPostBack('PgTimer1','')", 5000);
// -->
</SCRIPT>

However, I don't know what the __doPostBack function is. I assume that's
something my .net page should be rendering to the page automatically? (It's
not, in my case)

-Darrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top