NHibernate ISession Flush : 언제 어디서 사용해야하며 왜 그런가?
나를 완전히 혼란스럽게 만드는 것 중 하나는 , 및 session.Flush을 함께 사용하는 것입니다 .session.Commitsession.Close
때로는 session.Close작동합니다. 예를 들어 필요한 모든 변경 사항을 커밋합니다. 트랜잭션이있을 때 커밋을 사용해야하거나 오류가 발생하는 경우 롤백하도록 선택할 수 있도록 여러 개의 작성 / 업데이트 / 삭제 작업 단위가 필요하다는 것을 알고 있습니다.
그러나 때때로 나는 그 뒤에있는 논리에 의해 정말로 당황하게됩니다 session.Flush. session.SaveOrUpdate()플러시 가 뒤 따르는 예제를 보았지만 플러시를 제거하면 어쨌든 잘 작동합니다. 때로는 세션이 시간 초과되었다고 말하는 Flush 문에 오류가 발생하여 제거하면 해당 오류가 발생하지 않습니다.
플러시를 언제 어디서 사용하는지에 대한 좋은 지침이 있습니까? 이것에 대한 NHibernate 문서를 확인했지만 여전히 간단한 답변을 찾을 수 없습니다.
간단히:
- 항상 거래를 사용하십시오
- 사용하지 말고
Close(), 대신 명세서ISession내부로 전화를 감싸using거나 다른 곳에서 ISession의 수명주기를 관리하십시오 .
에서 문서 :
때때로
ISessionADO.NET 연결 상태를 메모리에 보유 된 객체의 상태와 동기화하는 데 필요한 SQL 문을 실행합니다. 이 프로세스 플러시는 기본적으로 다음 지점에서 발생합니다.
Find()또는 일부 호출에서Enumerable()- ...에서
NHibernate.ITransaction.Commit()- ...에서
ISession.Flush()SQL 문은 다음 순서로 발행됩니다.
- 모든 개체 삽입 (해당 개체를 사용하여 해당 개체를 저장 한 순서와 동일)
ISession.Save()- 모든 엔티티 업데이트
- 모든 컬렉션 삭제
- 모든 컬렉션 요소 삭제, 업데이트 및 삽입
- 모든 컬렉션 삽입
- 모든 개체 삭제 (해당 개체를 사용하여 해당 개체를 삭제 한 순서와 동일)
ISession.Delete()(단, 고유 ID 생성을 사용하는 객체는 저장할 때 삽입됩니다.)
명시적인 경우를 제외하고
Flush(), 세션이 ADO.NET 호출을 언제 실행하는지에 대한 보장은 없으며, 실행 순서 만 보장 합니다. 그러나 NHibernate는ISession.Find(..)메소드가 오래된 데이터를 리턴하지 않을 것이라고 보장한다 . 또한 잘못된 데이터를 반환하지도 않습니다.플러시가 덜 자주 발생하도록 기본 동작을 변경할 수 있습니다. 이
FlushMode클래스는 커밋시에만 플러시 (및 NHibernateITransactionAPI가 사용되는 경우에만 ), 설명 된 루틴을 사용하여 자동으로 플러시 또는Flush()명시 적으로 호출 되지 않는 한 플러시하지 않는 세 가지 모드를 정의합니다 . 마지막 모드는 장시간 실행중인 작업 단위에 유용합니다ISession.
...
이 섹션 도 참조하십시오 .
세션을 종료하는 데는 4 가지 단계가 있습니다.
- 세션을 플러시
- 거래를 저 지르다
- 세션을 닫다
- 예외 처리
세션 플러시
ITransactionAPI를 사용하는 경우이 단계에 대해 걱정할 필요가 없습니다. 트랜잭션이 커밋되면 암시 적으로 수행됩니다. 그렇지 않으면ISession.Flush()모든 변경 사항이 데이터베이스와 동기화되도록 호출해야 합니다.데이터베이스 트랜잭션 커밋
NHibernate ITransaction API를 사용하는 경우 다음과 같습니다.
tx.Commit(); // flush the session and commit the transactionADO.NET 트랜잭션을 직접 관리하는 경우 ADO.NET 트랜잭션을 수동으로
Commit()수행 해야 합니다.sess.Flush(); currentTransaction.Commit();변경 사항을 커밋하지 않기로 결정한 경우 :
tx.Rollback(); // rollback the transaction또는:
currentTransaction.Rollback();트랜잭션을 롤백하면 NHibernate의 내부 상태가 일관성을 유지하도록 현재 세션을 즉시 닫고 버려야합니다.
문제 종결
A call to
ISession.Close()marks the end of a session. The main implication of Close() is that the ADO.NET connection will be relinquished by the session.tx.Commit(); sess.Close(); sess.Flush(); currentTransaction.Commit(); sess.Close();If you provided your own connection,
Close()returns a reference to it, so you can manually close it or return it to the pool. OtherwiseClose()returns it to the pool.
Starting in NHibernate 2.0, transactions are required for DB operations. Therefore, the ITransaction.Commit() call will handle any necessary flushing. If for some reason you aren't using NHibernate transactions, then there will be no auto-flushing of the session.
From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory.
And always use
using (var transaction = session.BeginTransaction())
{
transaction.Commit();
}
after the changes are committed than this changes to save into database we use transaction.Commit();
Here are two examples of my code where it would fail without session.Flush():
http://www.lucidcoding.blogspot.co.uk/2012/05/changing-type-of-entity-persistence.html
at the end of this, you can see a section of code where I set identity insert on, save the entity then flush, then set identity insert off. Without this flush it seemed to be setting identity insert on and off then saving the entity.
The use of Flush() gave me more control over what was going on.
Here is another example:
Sending NServiceBus message inside TransactionScope
I don't fully understand why on this one, but Flush() prevented my error from happening.
참고URL : https://stackoverflow.com/questions/43320/nhibernate-isession-flush-where-and-when-to-use-it-and-why
'IT' 카테고리의 다른 글
| AngularJS에서 컨트롤러를 다시로드하지 않고 경로를 변경할 수 있습니까? (0) | 2020.05.16 |
|---|---|
| C ++ 11에서 비 멤버 시작 및 종료 함수를 사용하는 이유는 무엇입니까? (0) | 2020.05.16 |
| 정규식은 대문자를 소문자로 바꿉니다. (0) | 2020.05.15 |
| UITableViewCell은 흰색 배경을 표시하며 iOS7에서 수정할 수 없습니다 (0) | 2020.05.15 |
| scikit-learn에서 여러 열의 레이블 인코딩 (0) | 2020.05.15 |