0

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.

MaxPayne999
  • 184
  • 1
  • 2
  • 10
  • See answer for following : https://stackoverflow.com/questions/48916769/dbdatareader-causing-outofmemoryexception?force_isolation=true – jdweng Jul 28 '22 at 10:06
  • @jdweng the link you gave is for System.OutOfMemoryException issue, I get System.AccessViolationException. Tried the solution from the link you gave, i still get System.AccessViolationException – MaxPayne999 Jul 28 '22 at 10:20
  • Try capturing the call stack in the exception handler to get more info : https://learn.microsoft.com/en-us/dotnet/api/system.data.oracleclient.oracledatareader?force_isolation=true&view=netframework-4.8 – jdweng Jul 28 '22 at 12:06
  • Check how to debug AccessViolationException from official documentation here: https://learn.microsoft.com/en-us/dotnet/api/system.accessviolationexception?view=net-6.0#troubleshooting-accessviolationexception-exceptions – Wael Moughrbel Jul 28 '22 at 13:03
  • This also may be helpful: https://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception – Wael Moughrbel Jul 28 '22 at 13:04

1 Answers1

0

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!

MaxPayne999
  • 184
  • 1
  • 2
  • 10