473,792 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

We're having an odd problem with our home grown CMS once in a great while,
an XML file will become corrupt...eithe r 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.Re ad property in the sample code below

Dim fs As New System.IO.FileS tream("YourXmlF ile.xml", IO.FileMode.Ope n,
IO.FileAccess.W rite, IO.FileShare.Re ad)
Dim xmltw As New System.Xml.XmlT extWriter(fs,
System.Text.Enc oding.Default)
xmltw.WriteStri ng("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.Wr iteEndElement() 'close menuItems node
objXMLWriter.Fl ush()
objXMLWriter.Cl ose()

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

-Darrel
Apr 5 '06 #1
8 1379
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.

"darrel" wrote:
We're having an odd problem with our home grown CMS once in a great while,
an XML file will become corrupt...eithe r 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.Re ad property in the sample code below

Dim fs As New System.IO.FileS tream("YourXmlF ile.xml", IO.FileMode.Ope n,
IO.FileAccess.W rite, IO.FileShare.Re ad)
Dim xmltw As New System.Xml.XmlT extWriter(fs,
System.Text.Enc oding.Default)
xmltw.WriteStri ng("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.Wr iteEndElement() 'close menuItems node
objXMLWriter.Fl ush()
objXMLWriter.Cl ose()

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

-Darrel

Apr 5 '06 #2
All I know is what the Visual Studio Help says about the topic
System.IO.FileS hare enumeration. Check it out.

"darrel" <no*****@nowher e.com> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
We're having an odd problem with our home grown CMS once in a great while,
an XML file will become corrupt...eithe r 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.Re ad property in the sample code below

Dim fs As New System.IO.FileS tream("YourXmlF ile.xml", IO.FileMode.Ope n,
IO.FileAccess.W rite, IO.FileShare.Re ad)
Dim xmltw As New System.Xml.XmlT extWriter(fs,
System.Text.Enc oding.Default)
xmltw.WriteStri ng("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.Wr iteEndElement() 'close menuItems node
objXMLWriter.Fl ush()
objXMLWriter.Cl ose()

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

-Darrel

Apr 5 '06 #3
> 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
Apr 5 '06 #4
Simply catch the exception, and display a friendly message to the user.
Or sleep for a bit, and try again.

Apr 5 '06 #5
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.

"darrel" wrote:
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

Apr 5 '06 #6
> 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)
Apr 5 '06 #7
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
Apr 5 '06 #8
Hmm...is there any way via javascript to have a timed postback?


It looks like there is:

<SCRIPT language=javasc ript>
<!--
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
Apr 5 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2498
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can perform the task within "Write". 1-10 different threads may call "Write" simultaneously and continuously, some in a greedy manner. That is to say that some of the threads calling "Write" will take all they can get, while other threads may only call...
9
1712
by: Adam Monsen | last post by:
I kindly request a code review. If this is not an appropriate place for my request, where might be? Specific questions are in the QUESTIONS section of the code. ========================================================================== #include <stdio.h> #include <stdlib.h> #include <string.h>
9
1215
by: Shane | last post by:
I have a question about locks because I am seeing behavior that does not match what I though was the correct behavior. I thought when you put a lock around code the first instance that ran it would of course get access and then each access after that (while the first instance is still executing the code) would be essentially queued up on that lock waiting for the first to exit. But what I am seeing is that the last one there is the first...
8
2936
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm working with cifs (shares). there is another solution to copy files than file.copy in .net?
13
1679
by: Jeff Davis | last post by:
Right now performance isn't a problem, but this question has me curious: Let's say I have a shopping cart system where there is a "products" table that contains all possible products, and an "cart_items" table that stores how many of each product are in each cart. The obvious (or the first thing that came to my mind) would look something like this: create table products (
7
2869
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets send the messagetype via async NetworkStream ns = client.GetStream(); //assume client globally accessible
0
2307
by: shakahshakah | last post by:
Just started investigating InnoDB after having used MyISAM tables in the 4.0.12 version, almost immediately ran into a locking issue with INSERTs, DELETEs, and transactions. Given the following table: CREATE TABLE test1 ( f1 varchar(32) default NULL ) and a steady stream of INSERTs from a command-line shell:
7
7260
by: Boki | last post by:
Multi-thread read/write to a single file. I have two processing threads, thread A and thread B; and I called my queue as Q. Thread A will feed data into Q by user input, the timing is random. Thread B will read data from Q to process; this processing will communicate with a website. I think the best Q architecture is ring buffer as large as possible.
9
2604
by: zmickle | last post by:
Experts and books all say that you can share an Access back end on a shared drive with the front end running on each host computer. I have a simple database that tracks student data and it is shared between 4 staff memebers. Each staff computer has a copy of the front-end (linked tables, forms, and queries). They basically only use one form. The form works like this
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.