Browse Source

WebController内でトランザクションを管理するように修正

develop
sosuke.iwabuchi 2 years ago
parent
commit
e7eff640b7
5 changed files with 20 additions and 7 deletions
  1. +1
    -0
      app/Http/Controllers/Web/Auth/LoginController.php
  2. +1
    -0
      app/Http/Controllers/Web/Auth/LogoutController.php
  3. +1
    -3
      app/Http/Controllers/Web/Auth/MeController.php
  4. +11
    -1
      app/Http/Controllers/Web/WebController.php
  5. +6
    -3
      app/Util/DBUtil.php

+ 1
- 0
app/Http/Controllers/Web/Auth/LoginController.php View File

@@ -25,6 +25,7 @@ class LoginController extends WebController


public function __construct(protected LoginParam $param) public function __construct(protected LoginParam $param)
{ {
parent::__construct();
} }


protected function run(Request $request): JsonResponse protected function run(Request $request): JsonResponse


+ 1
- 0
app/Http/Controllers/Web/Auth/LogoutController.php View File

@@ -25,6 +25,7 @@ class LogoutController extends WebController


public function __construct(protected LogoutParam $param) public function __construct(protected LogoutParam $param)
{ {
parent::__construct();
} }


protected function run(Request $request): JsonResponse protected function run(Request $request): JsonResponse


+ 1
- 3
app/Http/Controllers/Web/Auth/MeController.php View File

@@ -2,12 +2,9 @@


namespace App\Http\Controllers\Web\Auth; namespace App\Http\Controllers\Web\Auth;


use App\Http\Controllers\Web\IParam;
use App\Http\Controllers\Web\WebController; use App\Http\Controllers\Web\WebController;
use App\Models\User;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class MeController extends WebController class MeController extends WebController
{ {
@@ -26,6 +23,7 @@ class MeController extends WebController


public function __construct(protected MeParam $param) public function __construct(protected MeParam $param)
{ {
parent::__construct();
} }






+ 11
- 1
app/Http/Controllers/Web/WebController.php View File

@@ -167,16 +167,23 @@ abstract class WebController extends BaseController


$this->authorize(); $this->authorize();


return $this->run($request);
$this->transaction->beginTransaction();
$ret = $this->run($request);
$this->transaction->commit();
return $ret;
} catch (GeneralErrorMessageException $e) { } catch (GeneralErrorMessageException $e) {
$this->transaction->rollBack();
return $this->failedResponse([], $e->getMessage()); return $this->failedResponse([], $e->getMessage());
} catch (AppCommonException $e) { } catch (AppCommonException $e) {
$this->transaction->rollBack();
logs()->error(sprintf("Appエラー:%s", $e->getMessage())); logs()->error(sprintf("Appエラー:%s", $e->getMessage()));
return $this->failedResponse(); return $this->failedResponse();
} catch (ExclusiveException $e) { } catch (ExclusiveException $e) {
$this->transaction->rollBack();
logs()->error(sprintf("排他エラー:%s", $e->getMessage())); logs()->error(sprintf("排他エラー:%s", $e->getMessage()));
return $this->exclusiveErrorResponse(); return $this->exclusiveErrorResponse();
} catch (LogicException $e) { } catch (LogicException $e) {
$this->transaction->rollBack();
logs()->error([ logs()->error([
sprintf("実装エラー:%s", $e->getMessage()), sprintf("実装エラー:%s", $e->getMessage()),
get_class($e), get_class($e),
@@ -189,13 +196,16 @@ abstract class WebController extends BaseController
}, ARRAY_FILTER_USE_BOTH)); }, ARRAY_FILTER_USE_BOTH));
return $this->failedResponse(); return $this->failedResponse();
} catch (ValidationException $e) { } catch (ValidationException $e) {
$this->transaction->rollBack();
return $this->validateErrorResponse($e); return $this->validateErrorResponse($e);
} catch (HttpException $e) { } catch (HttpException $e) {
$this->transaction->rollBack();
if ($e->getStatusCode() === 401) { if ($e->getStatusCode() === 401) {
return $this->unAuthorizedResponse(); return $this->unAuthorizedResponse();
} }
throw e; throw e;
} catch (Exception $e) { } catch (Exception $e) {
$this->transaction->rollBack();
logs()->error([ logs()->error([
sprintf("例外エラー:%s", $e->getMessage()), sprintf("例外エラー:%s", $e->getMessage()),
get_class($e), get_class($e),


+ 6
- 3
app/Util/DBUtil.php View File

@@ -22,7 +22,8 @@ class DBUtil
public function beginTransaction(): void public function beginTransaction(): void
{ {
if ($this->isBeginning) { if ($this->isBeginning) {
throw new LogicException("2重トランザクション開始検知");
// throw new LogicException("2重トランザクション開始検知");
return;
} }


DB::beginTransaction(); DB::beginTransaction();
@@ -32,7 +33,8 @@ class DBUtil
public function commit(): void public function commit(): void
{ {
if (!$this->isBeginning) { if (!$this->isBeginning) {
throw new LogicException("無効なコミット検知");
// throw new LogicException("無効なコミット検知");
return;
} }
DB::commit(); DB::commit();
$this->isBeginning = false; $this->isBeginning = false;
@@ -41,7 +43,8 @@ class DBUtil
public function rollBack(): void public function rollBack(): void
{ {
if (!$this->isBeginning) { if (!$this->isBeginning) {
throw new LogicException("無効なロールバック検知");
// throw new LogicException("無効なロールバック検知");
return;
} }


DB::rollBack(); DB::rollBack();


Loading…
Cancel
Save