source

2명의 사용자가 MySQL을 사용하여 웹 응용 프로그램에서 동일한 데이터를 편집하지 못하도록 하는 방법(가능한 경우 CodeIgniter 사용)

lovecheck 2023. 8. 10. 18:55
반응형

2명의 사용자가 MySQL을 사용하여 웹 응용 프로그램에서 동일한 데이터를 편집하지 못하도록 하는 방법(가능한 경우 CodeIgniter 사용)

개발 환경에서 CodeIgniter 3 및 MariaDB 5.0을 사용하지만 Prod 환경에서는 MySQL 5.6을 사용합니다.

저는 웹 앱을 만들고 사용자 요청을 수락하는 검증 체인을 구현했습니다.

예:

User A asked for something
The manager validate
The deputy validate
The commission validate
The accounting department validate
The request is accepted

이러한 유효성 검사는 데이터베이스의 여러 테이블에 등록됩니다.

SQL 트랜잭션을 사용하여 CodeIgniter의 함수와 CRUD 모델로 데이터를 올바르게 업데이트/삽입합니다. 예:

$this->db->trans_start();
$this->my_model->create($data);
$another_data = $this->second_model->read($second_data);
$this->another_model->create($another_data);
$this->db->trans_complete();

하지만 이제 총지배인에 대한 새로운 규칙을 시행해야 합니다.그는 언제든지 검증할 수 있으며, 이 검증은 모든 사용자가 검증한 것처럼 중요하기 때문에 검증 체인이 끊어집니다.

이 사례는 다음과 같은 예가 될 수:

User A asked for something
The manager validate
The general manager validate
The request is accepted

두 사용자(예: 총지배인과 대리인)가 동일한 요청을 참조할 수 있고, 한 사용자는 거부하고, 다른 사용자는 유효성을 확인하며, 데이터가 더 이상 일관성이 없다는 것을 깨달았기 때문에 이를 어떻게 구현해야 할지 모르겠습니다.

거래 내용이 충분합니까? 예:

// User A asked for something
// The manager validate
// The deputy consult the request
// The manager consult the request
// The deputy validate:

    $this->db->trans_start();

    // check if already validated by the general manager
    if ($this->model->read($data)) {
        $this->db->trans_complete();
        // redirection to a warning page
    }
    else {
        $this->my_model->create($my_data);
    }
    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE)
        // redirection to another warning page

// The general manager wants to refuse at the same time but the deputy's transaction started first, so he's redirected to a warning page

    $this->db->trans_start();

    // check if already validated by anyone
    if ($this->model->read($data)) {
        $this->db->trans_complete();
        // redirection to a warning page
    }
    else {
        $this->my_model->create($my_data);
    }
    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE)
        // redirection to another warning page

// The validation chain continues after the deputy's validation

LOCK TABLE은 다른 사용자가 동일한 요청을 참조하는 동안 사용자가 유효성 확인 페이지에 접근하는 것을 금지하기 위해 생각했습니다.TRANSACTION그리고.LOCK TABLE암묵적인 커밋을 유발할 수 있기 때문에 구현하기가 어렵고, 저는 이 일을 제대로 할 만큼 충분히 사용되지 않습니다.

에 대해 읽었습니다.START TRANSACTION READ WRITE그것이 더 나은 해결책이 될 수 있지만, 저는 모든 것을 이해하지 못했고, 어떻게 이것을 올바르게 구현해야 할지 모르겠습니다.

좋은 전략을 구현하고, 가능하다면 이러한 SQL 기능을 사용하는 방법을 누가 도와줄 수 있습니까?

해결책을 찾았고, 저는 그것을 사용했습니다.FOR UPDATE질문 직후, 다음과 같습니다.

// the query for my results
private model_function_for_query() {
    $this->db->select('my_select');
    $this->db->from('my_table');
    $this->db->where('my_where_clause');

    return $this->db->get_compiled_select();
}

// the function to apply the FOR UPDATE on the query
public model_function() {
    $query = $this->model_function_for_query();
    $result = $this->db->query($query.' FOR UPDATE');

    return $result->row();
}

그리고 내 컨트롤러에서:

// start the transaction
$this->db->trans_start();

// do the query, locking tables
$data = $this->my_model->model_function();

// check if the data has not changed
...

// if not, do the data treatment, else redirect to the warning page
...

// close the transaction
$this->db->trans_complete();

// check if the transaction failed, and do the stuff
if ($this->db->trans_status() === FALSE)
    ...
else
    ...

언급URL : https://stackoverflow.com/questions/55283071/how-to-forbid-2-users-to-edit-the-same-data-in-a-web-application-using-mysql-wi

반응형