简体   繁体   中英

Read\write to a file: heavily used application

I had asked a question here: The process cannot access the file because it is being used by another process: Correct way to read\\write to a file: heavily used application-Part II .

We have a heavily used .Net 3.5 application that reads "expensive to create" data and caches it. However we are getting a lot of errors around both reading the cache file and writing to the cache file. I have a single process, multiple threads and I want the application to synchronize access to a resource. I was advised to use a simple locking mechanism like lock or ReaderWriterLockSlim (see: http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx ). This however seems to have made the problem much much worse in production.

EDIT After the change was implemented, a lot of the cache files have a ">" tag in the end. Due to this the files are no longer xml files.

Can someone look at the code and advise what could I be doing wrong?

Code before change:

private XmlDocument ReadFromFile()
{
    XmlDocument result=null;
    string fileSystemPath=FileSystemPath();
    try
    {
        result=new XmlDocument();
        using (StreamReader streamReader = new StreamReader(fileSystemPath))
        {
            result.Load(streamReader);
        }
    }
    catch (FileNotFoundException)
    {
        result=null;
    }
    return result;
}

private object thisObject= new object();
private void WriteToFile(string siteID, XmlDocument stuff)
{
    string fileSystemPath=FileSystemPath();
    lock(thisObject)
    {
        using (StreamWriter streamWriter = new StreamWriter(fileSystemPath))
        {
            stuff.Save(streamWriter);
        }
    }
}

Code after change:

private readonly ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim();
private XmlDocument ReadFromFile()
{
    XmlDocument result = null;
    var fileSystemPath = FileSystemPath();          
    readerWriterLockSlim.EnterReadLock();
    try
    {
        result = new XmlDocument();
        using (var fileStream = new FileStream(fileSystemPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (var streamReader = new StreamReader(fileStream))
        {
            result.Load(streamReader);
        }
    }
    catch (FileNotFoundException)
    {
        result = null;
    }
    finally
    {
        readerWriterLockSlim.ExitReadLock();
    }
    return result;
}


private void WriteToFile()
{
    var fileSystemPath = FileSystemPath();            
    readerWriterLockSlim.EnterWriteLock();
    try
    {
        using (var fileStream = new FileStream(fileSystemPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
        using (var streamWriter = new StreamWriter(fileStream))
        {
            stuff.Save(streamWriter);
        }
    }
    finally
    {
        readerWriterLockSlim.ExitWriteLock();
    }
}

This code with some little changes should work

private readonly ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim();
private XmlDocument ReadFromFile()
{
    XmlDocument result = null;
    var fileSystemPath = FileSystemPath();          
    readerWriterLockSlim.EnterReadLock();
    try
    {
        result = new XmlDocument();
        using (var streamReader = new StreamReader(fileStream))
        {
            result.Load(streamReader);
        }
    }
    catch (FileNotFoundException)
    {
        result = null;
    }
    finally
    {
        readerWriterLockSlim.ExitReadLock();
    }
    return result;
}


private void WriteToFile(XmlDocument stuff)
{
    var fileSystemPath = FileSystemPath();            
    readerWriterLockSlim.EnterWriteLock();
    try
    {
        using (var streamWriter = new StreamWriter(fileSystemPath))
        {
            stuff.Save(streamWriter);
        }
    }
    finally
    {
        readerWriterLockSlim.ExitWriteLock();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM