2014-03-06 3 views
5

거래를 사용 중지하는 이유는 무엇입니까? http://ellislab.com/codeigniter/user-guide/database/transactions.htmlActiveRecord CodeIgniter에서 트랜잭션을 비활성화하는 것은 무엇입니까?

$this->db->trans_off() 

내가 사용을 볼 수 없습니다. 테스트 용입니까?

"트랜잭션을 사용할 수없는 경우 쿼리는 트랜잭션없이 쿼리를 실행할 때와 마찬가지로 자동 커밋됩니다."

user 테이블에 nameOfUser이라는 열이있는 테이블이 있습니다. nameOfUser2 - 열 않습니다 가 존재합니다.

TEST1 이 코드는 정상 거래와 2 개 삽입을 수행하려고 할 것입니다 :

$this->db->trans_start(); 
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL')); 
$this->db->insert('user', array('nameOfUser2' => 'test2')); 
$this->db->trans_complete();  

하지만 열 nameOfUser2 A 행이 아닌 존재 때문에 그것은 (아무것도 삽입되지 않은) 롤백 두 번째 삽입물.

TEST2 이 코드는 두 번째에 오류가있는 경우에도 - 테이블 usertestCVVCOOL 문자열을 삽입합니다

$this->db->trans_off(); 
$this->db->trans_start(); 
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL')); 
$this->db->insert('user', array('nameOfUser2' => 'test2')); 
$this->db->trans_complete();  

위를 비활성화 트랜잭션이 개 삽입을 수행하려고 할 것 삽입 ($this->db->insert('user', array('nameOfUser2' => 'test2'));)

이런 식으로 트랜잭션을 비활성화해야하는시기는 언제입니까?

+0

나는 잘 모르겠다.하지만 트랜잭션 상태가 다시 실행되도록 남겨 두었다면 trans_start()를 수행 한 후에 트랜잭션 모드를 off로 재설정해야 할 수도 있으므로 트랜잭션 상태에 대해 좀더 쉬워야한다고 생각한다. 끝났다. –

답변

1

언제 이러한 방식으로 트랜잭션을 비활성화해야합니까?

문제가있는 경우 트랜잭션이 동일한 스레드에서 더 많은 상호 작용을 허용하지 않습니다 (첫 번째 예와 같이). 따라서 커밋 전에 롤백하기 전에 db에서 다른 작업을 수행하려면 작업을 수행 할 수 없습니다.

의사 예 :

//will not work 
$this->db->trans_start(); 
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL')); 
$this->db->insert('user', array('nameOfUser2' => 'test2')); 
//more likely this would be a condition that makes sense 
//perhaps using a query result from earlier in function 
if(1===1){ 
    $this->db->query('INSERT INTO errors (db) values (1)');//nogo 
} 
$this->db->trans_complete(); 

-

그것은 완전히 우리가 거래를 사용하는 시나리오에 따라 달라집니다
//will work 
$this->db->trans_off(); 
$this->db->trans_start(); 
$this->db->insert('user', array('nameOfUser' => 'testCVVCOOL')); 
$this->db->insert('user', array('nameOfUser2' => 'test2')); 
//more likely this would be a condition that makes sense 
//perhaps using a query result from earlier in function 
if(1===1){ 
    $this->db->query('INSERT INTO errors (db) values (1)');//OK 
} 
$this->db->trans_complete(); 
+0

로깅 오류를 (기본적으로) 의미 할 수 있습니다. – bestprogrammerintheworld

+0

그 중 하나만 사용할 수 있습니다. 나는 그것이 융통성있는 것이라고 생각한다. 어떤 이유로 든 (오류, 강제 작성 등) 기능이 필요하다면 기능성이 필요하다. – stormdrain

+0

하지만 그건 이상한 방법으로 오류를 처리하는 것 같아? (예를 들어). docs (http://ellislab.com/codeigniter/user-guide/database/transactions.html)에서 $ this-> db-> trans_status()를 사용하여 트랜잭션의 상태를 얻은 다음 일부 로깅을 수행 할 수 있다고합니다. . 이것이 내 요점이다. 더 나은 대안이 있기 때문에 트랜잭션을 사용하는 좋은 이유를 알아낼 수 있습니까? – bestprogrammerintheworld

0

-

트랜잭션이 필요

:

전체에서 일련의 작업이 필요합니다. 즉, 온라인으로 제품을 주문하고 주문 확인에 몇 가지 단계가있는 경우 모든 단계가 실패하면 전체 주문 거래를 되돌릴 수 있습니다.

는 거래 오프가 필요한 경우 : 나는 정보의 모든 유형이 나에게 가치가 몇 가지 물건을 시도하고있는 경우

. 그런 다음 거래 해제 기능을 사용할 수 있습니다. 어떤 오류가 발생하더라도 조건을 충족하는 데이터를 저장하십시오.

+0

나는 정말로 그것을 얻지 못합니다. 몇 가지 예를 들어 설명해 주시겠습니까? – bestprogrammerintheworld

관련 문제