locked
XmlReader invalid after connection closed RRS feed

  • Question

  • I want to return an XmlReader to a client app but when I call the close method on the underlying connection, the XML data is no longer accessible through the reader. How can I pass a reader to a client without leaving the connection open? Once the client has finished with the reader it goes out of scope but the connection is not released, so I am having issues with connection pooling.
    Monday, May 28, 2007 6:49 AM

Answers

  • Hi,

     

    To answer your last question first: The memory stream will get collected by garbage collection when the GC decides it needs to free up some memory. If you release all references to the reader and the memory stream yourself and your clients do, it will get collected, no memory leaks will happen.

     

    As for the best approach on this - I assume that you're getting the XmlReader from SQL connection right - you don't have any means of controlling th eunderlying stream. In that case you have two options, either all your users have to correctly Dispose the reader once they're done with it (XmlReader implements IDisposable, so correct usage of using keyword will do) - this will also release the underlying stream and the connection, so you should not have problems with too many connections (This is definitely the best way of doing this). If that is not feasible your approach is pretty good, except that it is probably too CPU intensive. instead you might want to read the XML into XPathDocument and then create a reader based on the XPathDocument (it is very similar approach but instead of writing the XML back to text and then parsing it back to memory, you store it in more XML-like format in memory).

     

    Hope this helps,

    Thursday, May 31, 2007 12:42 PM
    Moderator

All replies

  • I have resolved this problem. It may not be the best method, so comments welcome.

    I have created an XmlWriter on a memory stream, written the result of Command.ExecuteXmlReader to it using WriteNode, closed the connection, then created a new XmlReader from the XmlWrite using XmlReader.Create. This creates a disconnected XmlReader which can be passed back to the client without holding the connection open. If the XML from SQL can contain multiple root elements (mine can) then it is necessary to set the ConformanceLevel on the XmlReader to Fragment.

    Does anyone know if the memory stream is freed up after the XmlReader is closed, or do I now have a memory leak to deal with?
    Monday, May 28, 2007 7:20 AM
  • Hi,

     

    To answer your last question first: The memory stream will get collected by garbage collection when the GC decides it needs to free up some memory. If you release all references to the reader and the memory stream yourself and your clients do, it will get collected, no memory leaks will happen.

     

    As for the best approach on this - I assume that you're getting the XmlReader from SQL connection right - you don't have any means of controlling th eunderlying stream. In that case you have two options, either all your users have to correctly Dispose the reader once they're done with it (XmlReader implements IDisposable, so correct usage of using keyword will do) - this will also release the underlying stream and the connection, so you should not have problems with too many connections (This is definitely the best way of doing this). If that is not feasible your approach is pretty good, except that it is probably too CPU intensive. instead you might want to read the XML into XPathDocument and then create a reader based on the XPathDocument (it is very similar approach but instead of writing the XML back to text and then parsing it back to memory, you store it in more XML-like format in memory).

     

    Hope this helps,

    Thursday, May 31, 2007 12:42 PM
    Moderator