リビジョン | a62016050ef8f95981a88a15ef4b6eba1d5cccab (tree) |
---|---|
日時 | 2022-05-27 23:07:55 |
作者 | yoshy <yoshy.org.bitbucket@gz.j...> |
コミッター | yoshy |
[ADD] 手動でトランザクションを終端できる機能を追加
@@ -39,13 +39,27 @@ namespace CleanAuLait48.Adaptor.Controller.Handler.tx | ||
39 | 39 | { |
40 | 40 | UseCaseResponse res = await context.HandleNextAsync(req, context); |
41 | 41 | |
42 | - tx.Commit(false); | |
42 | + if (!tx.IsTerminated) | |
43 | + { | |
44 | + tx.Commit(false); | |
45 | + } | |
46 | + else | |
47 | + { | |
48 | + logger.Trace("トランザクション境界 [{0}] は手動で終端されているためコミットをスキップします。", name); | |
49 | + } | |
43 | 50 | |
44 | 51 | return res; |
45 | 52 | } |
46 | 53 | catch (Exception) |
47 | 54 | { |
48 | - tx.Rollback(false); | |
55 | + if (!tx.IsTerminated) | |
56 | + { | |
57 | + tx.Rollback(false); | |
58 | + } | |
59 | + else | |
60 | + { | |
61 | + logger.Trace("トランザクション境界 [{0}] は手動で終端されているためロールバックをスキップします。", name); | |
62 | + } | |
49 | 63 | |
50 | 64 | throw; |
51 | 65 | } |
@@ -38,15 +38,27 @@ namespace CleanAuLait48.Adaptor.Controller.Handler.tx | ||
38 | 38 | { |
39 | 39 | UseCaseResponse res = context.HandleNext(req, context); |
40 | 40 | |
41 | - tx.Commit(false); | |
41 | + if (!tx.IsTerminated) | |
42 | + { | |
43 | + tx.Commit(false); | |
44 | + } | |
45 | + else | |
46 | + { | |
47 | + logger.Trace("トランザクション境界 [{0}] は手動で終端されているためコミットをスキップします。", name); | |
48 | + } | |
42 | 49 | |
43 | 50 | return res; |
44 | 51 | } |
45 | - catch (Exception e) | |
52 | + catch (Exception) | |
46 | 53 | { |
47 | - logger.Error(e, "例外がスローされたためロールバックします"); | |
48 | - | |
49 | - tx.Rollback(false); | |
54 | + if (!tx.IsTerminated) | |
55 | + { | |
56 | + tx.Rollback(false); | |
57 | + } | |
58 | + else | |
59 | + { | |
60 | + logger.Trace("トランザクション境界 [{0}] は手動で終端されているためロールバックをスキップします。", name); | |
61 | + } | |
50 | 62 | |
51 | 63 | throw; |
52 | 64 | } |
@@ -9,13 +9,14 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
9 | 9 | { |
10 | 10 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
11 | 11 | |
12 | + public bool IsTerminated { get; private set; } | |
13 | + | |
14 | + private readonly string name; | |
15 | + | |
12 | 16 | private readonly ITransactionManager txMan; |
13 | 17 | private IDbConnectionWrapper conn; |
14 | 18 | private IDbTransaction tx; |
15 | 19 | |
16 | - private readonly string name; | |
17 | - | |
18 | - private bool terminatedValue; | |
19 | 20 | private bool disposedValue; |
20 | 21 | |
21 | 22 | public IDbConnection Connection => this.tx.Connection; |
@@ -36,7 +37,15 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
36 | 37 | name, ToRawConnID(), ToTransactionID()); |
37 | 38 | } |
38 | 39 | |
39 | - private void SwitchTransaction(bool needNewTx) | |
40 | + public void TerminateTransaction() | |
41 | + { | |
42 | + logger.Trace("DB接続 [{0}] {1}: トランザクションを手動で終端します", | |
43 | + name, ToRawConnID()); | |
44 | + | |
45 | + TerminateTransaction(false); | |
46 | + } | |
47 | + | |
48 | + private void TerminateTransaction(bool needNewTx) | |
40 | 49 | { |
41 | 50 | if (this.tx != null) |
42 | 51 | { |
@@ -49,9 +58,18 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
49 | 58 | if (!needNewTx) |
50 | 59 | { |
51 | 60 | this.tx = null; |
52 | - this.terminatedValue = true; | |
61 | + this.IsTerminated = true; | |
53 | 62 | logger.Trace("DB接続 [{0}] {1}: トランザクションを終端しました", |
54 | 63 | name, ToRawConnID()); |
64 | + } | |
65 | + } | |
66 | + | |
67 | + private void SwitchTransaction(bool needNewTx) | |
68 | + { | |
69 | + TerminateTransaction(needNewTx); | |
70 | + | |
71 | + if (!needNewTx) | |
72 | + { | |
55 | 73 | return; |
56 | 74 | } |
57 | 75 |
@@ -81,7 +99,7 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
81 | 99 | |
82 | 100 | public void Commit(bool needNewTx) |
83 | 101 | { |
84 | - if (this.terminatedValue) | |
102 | + if (this.IsTerminated) | |
85 | 103 | { |
86 | 104 | logger.Trace("DB接続 [{0}]: トランザクションは終端されているため、コミットできません", name); |
87 | 105 | return; |
@@ -105,7 +123,7 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
105 | 123 | |
106 | 124 | public void Rollback(bool needNewTx) |
107 | 125 | { |
108 | - if (this.terminatedValue) | |
126 | + if (this.IsTerminated) | |
109 | 127 | { |
110 | 128 | logger.Trace("DB接続 [{0}]: トランザクションは終端されているため、ロールバックできません", name); |
111 | 129 | return; |
@@ -130,7 +148,7 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
130 | 148 | { |
131 | 149 | // TODO: マネージド状態を破棄します (マネージド オブジェクト) |
132 | 150 | |
133 | - if (!this.terminatedValue) | |
151 | + if (!this.IsTerminated) | |
134 | 152 | { |
135 | 153 | logger.Trace("DB接続 [{0}] {1}: トランザクション {2} の破棄が開始されました", |
136 | 154 | name, ToRawConnID(), ToTransactionID()); |
@@ -4,6 +4,8 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
4 | 4 | { |
5 | 5 | public interface IDbTransactionWrapper : IDbTransaction |
6 | 6 | { |
7 | + bool IsTerminated { get; } | |
8 | + void TerminateTransaction(); | |
7 | 9 | void Commit(bool needNewTx); |
8 | 10 | void Rollback(bool needNewTx); |
9 | 11 | } |