简体   繁体   中英

Huge amount of data retrieved from database causes System.AccessViolationException

We have a code which is extracting data from Oracle database. The data returned from the database is in XML format and it gets returned as ref cursor as it may contains multiple XML's. Each xml file size is about 5-7 MB, but when the file size goes above 25 MB we get exception thrown from the reader.

The exception thrown is - "System.AccessViolationException: Attempted to read or write protected memory."

The code on the C# side is a simple one - we extract data from the database as ref cursor and read the ref cursor using OracleReader. When we try to extract the xml into xmldocument using get reader this is the place where we get the System.AccessViolationException while trying to read the huge amount of data.

using (var cur= (OracleRefCursor)cmd.Parameters["cur_xml"].Value)
            {

                if (!cur.IsNull)
                {                  
                    OracleDataReader rdr= cur.GetDataReader();
                    while (rdr.Read())
                    {
                        XmlDocument x = new XmlDocument();                       
                        x.LoadXml(rdr.GetString(0));//this line above throws the System.AccessViolationException             
                       
                    }
                }
}

Any suggestion to fix this for large data.

Thanks to all who replied with suggestions to resolve the issue.

However, with some research here and there I was able to resolve the issue myself. Since we are using Oracle DB, we were referring Oracle.DataAccess in the DAL layer.

I switched the reference from Oracle.DataAccess to Oracle.ManagedDataAccess and with that I was able to resolve the issue and no matter how big the XML we retrieve from the DB(extracted about 35-40 MB XML file) it is not throwing the "System.AccessViolationException: Attempted to read or write protected memory." issue(for now).

I don't know if this solution will work for everyone as they need to first find out the root cause for the error and take action accordingly. For me whenever I tried to extract the details from reader using reader.GetString(0), it threw the exception.

Hope this will be helpful for anyone facing similar issue like me.

Thanks!

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