xml로 serialize하고 Sql Server 행에 저장해야하는 개체 그래프가 있습니다. xml 데이터 형식의 테이블이 있고 그것을 memorystream 제공하는 SqlXml 데이터 형식을 사용하고 있습니다.개체 그래프를 SQL Server에 XML로 효율적으로 스트리밍하는 방법
이 부분은 대부분 잘 작동합니다. 그러나, 내 개체 그래프가 특히 큰 경우 (~ 200 메가 크기), 나는 OutOfMemoryException을 얻습니다. 개체 그래프를 XML로 serialize하고 SQL Server로 스트리밍하는 가장 효율적인 방법은 무엇입니까? HybridStream :
using (MemoryStream ms = new MemoryStream())
{
using (SqlConnection connection = new SqlConnection(this.Settings.GetConnectionString()))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT_MESSAGE";
command.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter param = command.CreateParameter();
param.ParameterName = "@PARTITION_ID";
param.Direction = System.Data.ParameterDirection.Input;
param.DbType = System.Data.DbType.Int32;
param.Value = this.PartitionId;
command.Parameters.Add(param);
param = command.CreateParameter();
param.ParameterName = "@MESSAGE";
param.Direction = System.Data.ParameterDirection.Input;
param.DbType = System.Data.DbType.Xml;
XmlSerializer xs = new XmlSerializer(typeof(MessageContext));
xs.Serialize(ms, message);
param.Value = new System.Data.SqlTypes.SqlXml(ms);
command.Parameters.Add(param);
param = command.CreateParameter();
param.ParameterName = "@RETURN_VALUE";
param.Direction = System.Data.ParameterDirection.ReturnValue;
param.DbType = System.Data.DbType.Int32;
command.Parameters.Add(param);
command.ExecuteNonQuery();
if ((int)command.Parameters["@RETURN_VALUE"].Value == 1)
throw new IntegrationSystemException("Unknown error encountered while selecting the record count");
else
{
if (log.IsInfoEnabled)
log.InfoFormat("Saved message [{0}]", message.MessageId);
}
}
}
여기처럼 나는 "hybridstream을"사용을 고려하고있다 : 여기 지금 내 코드입니다. 이것은 기본적으로 임의의 한도에 MemoryStream을 할당합니다. 한도에 도달하면 잉여에 대한 FileStream을 만듭니다.
최적화 도움말에 감사드립니다.
HybridStream 접근 방식을 사용합니다. 저는 작은 파일 (전체 트랜잭션의 90 %)에 대해 높은 성능의 메모리 스트림을 제공하지만 큰 파일 스트림에 대해서는 FileStream의 안전성을 보장한다고 생각합니다. – Gavin