dbmsv3 init...1

This commit is contained in:
choi.jh 2025-10-29 14:17:01 +09:00
parent 7298defef6
commit a17fade55d

View File

@ -5,8 +5,9 @@ namespace App\Services;
use App\Helpers\CommonHelper;
use App\Models\CommonModel;
use App\Services\Customer\ClientService;
use App\Services\UserService;
use App\Services\MyLogService;
use App\Services\UserService;
use CodeIgniter\Database\Exceptions\DatabaseException;
use RuntimeException;
abstract class CommonService
@ -307,22 +308,25 @@ abstract class CommonService
public function create(array $formDatas): mixed
{
$db = \Config\Database::connect();
$db->transStart();
try {
//트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
$db->transException(true)->transStart();
$entity = $this->create_process($formDatas);
$db->transComplete();
if ($db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패: DB 상태 문제');
}
return $entity;
} catch (\Throwable $e) {
$db->transRollback(); // 예외 발생 시 수동으로 롤백
} catch (DatabaseException $e) {
// DatabaseException을 포착하면 자동으로 롤백 처리됨
// 이 예외 객체($e)에 실패한 쿼리 정보가 포함됨
// 예외 메시지에 쿼리 정보 포함시키기
throw new RuntimeException(sprintf(
"\n------%s-----\n%s\n%s\n------------------------------\n",
"\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
__METHOD__,
$this->getModel()->getLastQuery(),
$e->getMessage()
), 0, $e);
), $e->getCode(), $e);
} catch (\Throwable $e) {
$db->transRollback(); // 예외 발생 시 수동으로 롤백
throw new RuntimeException($e->getMessage(), 0, $e);
}
}
//수정
@ -333,22 +337,22 @@ abstract class CommonService
public function modify(mixed $entity, array $formDatas): mixed
{
$db = \Config\Database::connect();
$db->transStart();
try {
//트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
$db->transException(true)->transStart();
$entity = $this->modify_process($entity, $formDatas);
$db->transComplete();
if ($db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패: DB 상태 문제');
}
return $entity;
} catch (\Throwable $e) {
$db->transRollback(); // 예외 발생 시 수동으로 롤백
} catch (DatabaseException $e) { //DB 오류시 발생
throw new RuntimeException(sprintf(
"\n------%s-----\n%s\n%s\n------------------------------\n",
"\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
__METHOD__,
$this->getModel()->getLastQuery(),
$e->getMessage()
), 0, $e);
), $e->getCode(), $e);
} catch (\Throwable $e) { // 그 외 다른 종류의 예외 처리
$db->transRollback(); // 예외 발생 시 수동으로 롤백
throw new RuntimeException($e->getMessage(), 0, $e);
}
}
//단일작업
@ -359,22 +363,22 @@ abstract class CommonService
public function toggle(mixed $entity, array $formDatas): mixed
{
$db = \Config\Database::connect();
$db->transStart();
try {
//트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
$db->transException(true)->transStart();
$entity = $this->toggle_process($entity, $formDatas);
$db->transComplete();
if ($db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패: DB 상태 문제');
}
return $entity;
} catch (\Throwable $e) {
$db->transRollback(); // 예외 발생 시 수동으로 롤백
} catch (DatabaseException $e) { //DB 오류시 발생
throw new RuntimeException(sprintf(
"\n------%s-----\n%s\n%s\n------------------------------\n",
"\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
__METHOD__,
$this->getModel()->getLastQuery(),
$e->getMessage()
), 0, $e);
), $e->getCode(), $e);
} catch (\Throwable $e) { // 그 외 다른 종류의 예외 처리
$db->transRollback(); // 예외 발생 시 수동으로 롤백
throw new RuntimeException($e->getMessage(), 0, $e);
}
}
//일괄처리작업
@ -385,50 +389,53 @@ abstract class CommonService
public function batchjob(mixed $entity, array $formDatas): mixed
{
$db = \Config\Database::connect();
$db->transStart();
try {
//트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
$db->transException(true)->transStart();
$entity = $this->batchjob_process($entity, $formDatas);
$db->transComplete();
if ($db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패: DB 상태 문제');
}
return $entity;
} catch (\Throwable $e) {
$db->transRollback(); // 예외 발생 시 수동으로 롤백
} catch (DatabaseException $e) { //DB 오류시 발생
throw new RuntimeException(sprintf(
"\n------%s-----\n%s\n%s\n------------------------------\n",
"\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
__METHOD__,
$this->getModel()->getLastQuery(),
$e->getMessage()
), 0, $e);
), $e->getCode(), $e);
} catch (\Throwable $e) { // 그 외 다른 종류의 예외 처리
$db->transRollback(); // 예외 발생 시 수동으로 롤백
throw new RuntimeException($e->getMessage(), 0, $e);
}
}
//삭제
protected function delete_process(string $uid): bool
protected function delete_process(string $uid): void
{
return $this->getModel()->delete($uid);
if (!$this->getModel()->delete($uid)) {
// delete() 메서드 실패 시 모델의 errors()를 통해 상세 정보 확인
$errors = $this->getModel()->errors();
throw new RuntimeException("모델 삭제 실패: " . var_export($errors, true));
}
}
public function delete(mixed $entity): mixed
{
$db = \Config\Database::connect();
$db->transStart();
try {
if (!$this->delete_process($entity->getPK())) {
throw new RuntimeException(var_export($this->getModel()->errors(), true));
}
//트랜잭션 도중 DB 오류가 발생하면 DatabaseException을 던지도록 설정
$db->transException(true)->transStart();
$this->delete_process($entity->getPK());
$db->transComplete();
if ($db->transStatus() === false) {
throw new RuntimeException('트랜잭션 실패: DB 상태 문제');
}
return $entity;
} catch (\Throwable $e) {
$db->transRollback(); // 예외 발생 시 수동으로 롤백
} catch (DatabaseException $e) { //DB 오류시 발생
throw new RuntimeException(sprintf(
"\n------%s-----\n%s\n%s\n------------------------------\n",
"\n----[%s]에서 트랜잭션 실패: DB 오류----\n%s\n%s\n------------------------------\n",
__METHOD__,
$this->getModel()->getLastQuery(),
$e->getMessage()
), 0, $e);
), $e->getCode(), $e);
} catch (\Throwable $e) { // 그 외 다른 종류의 예외 처리
$db->transRollback(); // 예외 발생 시 수동으로 롤백
throw new RuntimeException($e->getMessage(), 0, $e);
}
}
////Index 검색용