2010-01-11 6 views
1

FOR XML 쿼리를 실행하기 위해 저장된 proc를 호출하는 데이터 액세스 클래스에서 XMLReader를 반환하려고합니다. 문제는, 내가 xmlreader를 반환하거나 읽기가 종료됩니다 때 SQL 연결에서 close()를 호출 할 수 없다는 것입니다. xmlreader를 호출하는 클래스는 SQL 연결에 대해 아무것도 모르기 때문에 연결을 종료 할 수 없습니다. 어떻게 처리 할 수 ​​있습니까?Return XMLReader

답변

3

XmlDocument을 빌드하고 리턴하여 해당 데이터베이스 연결을 닫을 수 있습니다.

+0

문제는이 xml이 최대 200MB까지 될 수 있으므로 독자 접근 방식을 시도한 이유입니다. 나는 정말로 메모리에 200MB의 xmldocument를 갖고 싶습니까? – Bob

+1

이 경우 연결을 열어 둘 필요가 있거나 데이터 액세스 레이어를 일회용으로 만들어야하므로 '사용'으로 호출 할 수 있습니다. –

1

전화를 랩핑하려면 익명 메소드를 사용하십시오. 예를 들어

, 당신은 데이터 영역 클래스가 가정, 데이터 층에 유사한 방법을 추가

public delegate void DoSomethingInvoker(); 
class DataLayer 
{ 
    //myReader needs to be declared externally in other to access it from the doSomething delegate 
    public void MethodThatGetsAsXmlReader(XmlReader myReader, DoSomethingInvoker doSomething) 
    { 
     myReader = GetXmlReaderFromDB(); 
     doSomething(); 
     CloseDbConnection();  //close connections, do cleanup, and any other book keeping can be done after the doSomething() call 
    } 
} 

통화/사용, 당신은 단순히 당신의 높은 수준의 클래스에서이 작업을 수행

DataLayer dl = new DataLayer(); 
XmlReader myReader = null; //variables declared outside the scope of the anonymous method are still accessible inside it through the magic of closures 
dl.MethodThatGetsAsXmlReader(myReader, delegate() 
    { 
     //do all work that involves myReader here 
     myReader.read(); 
     Console.out.println(myReader.value); 
    }); 
//at this point myReader is closed and cannot be used 

기본적으로 실행하려는 코드를 데이터 계층에 전달하고, 데이터 계층은 xmlreader를 반입하고, 코드를 호출 한 다음 정리합니다. 여전히 조직 계층 유지하면서

나는, 내 코드

DataLayer dl = new DataLayer(); 
dl.Transaction(delegate() 
    { 
     dl.DbCall1(); 
     dl.DbCall2(); 
     dl.DbCall3(); 
    }); 

그것은 코드 좋은 읽을하게에 트랜잭션 논리를 포장하기위한 유사한 기술을 사용;

+0

cool! 감사합니다. – Bob