Revision | 3c3c83e6813b918ec2a0b7954cf0d384eedf8af9 (tree) |
---|---|
Zeit | 2022-05-26 12:19:03 |
Autor | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[ADD] CleanAuLait 本体からデータベース関連の機能を分離
@@ -1,58 +0,0 @@ | ||
1 | -using CleanAuLait.Adaptor.Boundary.Controller; | |
2 | -using CleanAuLait.Adaptor.Boundary.Controller.Handler; | |
3 | -using CleanAuLait.OuterEdge.Repository.Db.Tx; | |
4 | -using CleanAuLait.UseCase.Request; | |
5 | -using CleanAuLait.UseCase.Response; | |
6 | -using NLog; | |
7 | - | |
8 | -namespace CleanAuLait.Adaptor.Controller.Handler.tx | |
9 | -{ | |
10 | - public abstract class AbstractAsyncDbTransactionHandler : IAsyncRequestHandler | |
11 | - { | |
12 | - private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
13 | - | |
14 | - private readonly ITransactionManager txMan; | |
15 | - | |
16 | - private readonly string name; | |
17 | - | |
18 | - public AbstractAsyncDbTransactionHandler(ITransactionManager txMan) | |
19 | - : this(txMan, txMan.DEFAULT_NAME) | |
20 | - { | |
21 | - } | |
22 | - | |
23 | - public AbstractAsyncDbTransactionHandler(ITransactionManager txMan, string name) | |
24 | - { | |
25 | - this.txMan = txMan; | |
26 | - this.name = name; | |
27 | - } | |
28 | - | |
29 | - public async Task<UseCaseResponse> HandleAsync(UseCaseRequest req, IAsyncHandlerContext context) | |
30 | - { | |
31 | - try | |
32 | - { | |
33 | - logger.Trace("トランザクション境界 [{0}] を開始します。", name); | |
34 | - using var tx = txMan.BeginTransaction(name); | |
35 | - try | |
36 | - { | |
37 | - UseCaseResponse res = await context.HandleNextAsync(req, context); | |
38 | - | |
39 | - tx.Commit(false); | |
40 | - | |
41 | - return res; | |
42 | - } | |
43 | - catch (Exception) | |
44 | - { | |
45 | - tx.Rollback(false); | |
46 | - | |
47 | - throw; | |
48 | - } | |
49 | - } | |
50 | - finally | |
51 | - { | |
52 | - txMan.RemoveTransaction(name); | |
53 | - | |
54 | - logger.Trace("トランザクション境界 [{0}] を終了しました。", name); | |
55 | - } | |
56 | - } | |
57 | - } | |
58 | -} |
@@ -1,60 +0,0 @@ | ||
1 | -using CleanAuLait.Adaptor.Boundary.Controller; | |
2 | -using CleanAuLait.Adaptor.Boundary.Controller.Handler; | |
3 | -using CleanAuLait.OuterEdge.Repository.Db.Tx; | |
4 | -using CleanAuLait.UseCase.Request; | |
5 | -using CleanAuLait.UseCase.Response; | |
6 | -using NLog; | |
7 | - | |
8 | -namespace CleanAuLait.Adaptor.Controller.Handler.tx | |
9 | -{ | |
10 | - public abstract class AbstractDbTransactionHandler : IRequestHandler | |
11 | - { | |
12 | - private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
13 | - | |
14 | - private readonly ITransactionManager txMan; | |
15 | - | |
16 | - private readonly string name; | |
17 | - | |
18 | - public AbstractDbTransactionHandler(ITransactionManager txMan) | |
19 | - : this(txMan, txMan.DEFAULT_NAME) | |
20 | - { | |
21 | - } | |
22 | - | |
23 | - public AbstractDbTransactionHandler(ITransactionManager txMan, string name) | |
24 | - { | |
25 | - this.txMan = txMan; | |
26 | - this.name = name; | |
27 | - } | |
28 | - | |
29 | - public UseCaseResponse Handle(UseCaseRequest req, IHandlerContext context) | |
30 | - { | |
31 | - try | |
32 | - { | |
33 | - logger.Trace("トランザクション境界 [{0}] を開始します。", name); | |
34 | - using var tx = txMan.BeginTransaction(name); | |
35 | - try | |
36 | - { | |
37 | - UseCaseResponse res = context.HandleNext(req, context); | |
38 | - | |
39 | - tx.Commit(false); | |
40 | - | |
41 | - return res; | |
42 | - } | |
43 | - catch (Exception e) | |
44 | - { | |
45 | - logger.Error(e, "例外がスローされたためロールバックします"); | |
46 | - | |
47 | - tx.Rollback(false); | |
48 | - | |
49 | - throw; | |
50 | - } | |
51 | - } | |
52 | - finally | |
53 | - { | |
54 | - txMan.RemoveTransaction(name); | |
55 | - | |
56 | - logger.Trace("トランザクション境界 [{0}] を終了しました。", name); | |
57 | - } | |
58 | - } | |
59 | - } | |
60 | -} |
@@ -1,11 +0,0 @@ | ||
1 | -using CleanAuLait.OuterEdge.Repository.Db.Tx; | |
2 | - | |
3 | -namespace CleanAuLait.Adaptor.Controller.Handler.tx | |
4 | -{ | |
5 | - public class AsyncSimpleDbTransactionHandler : AbstractAsyncDbTransactionHandler | |
6 | - { | |
7 | - public AsyncSimpleDbTransactionHandler(ITransactionManager txMan) : base(txMan) | |
8 | - { | |
9 | - } | |
10 | - } | |
11 | -} |
@@ -1,12 +0,0 @@ | ||
1 | -using CleanAuLait.OuterEdge.Repository.Db.Tx; | |
2 | - | |
3 | - | |
4 | -namespace CleanAuLait.Adaptor.Controller.Handler.tx | |
5 | -{ | |
6 | - internal class SimpleDbTransactionHandler : AbstractDbTransactionHandler | |
7 | - { | |
8 | - public SimpleDbTransactionHandler(ITransactionManager txMan) : base(txMan) | |
9 | - { | |
10 | - } | |
11 | - } | |
12 | -} |
@@ -26,21 +26,7 @@ namespace CleanAuLait | ||
26 | 26 | |
27 | 27 | containerRegistry.RegisterSingleton<IHandlerContextFactory, HandlerContextFactory>(); |
28 | 28 | containerRegistry.RegisterSingleton<IAsyncHandlerContextFactory, AsyncHandlerContextFactory>(); |
29 | -#if false | |
30 | - // | |
31 | - // Transaction Manager | |
32 | - // | |
33 | - | |
34 | - containerRegistry.RegisterSingleton<ITransactionManager, TransactionManager>(); | |
35 | - | |
36 | - // | |
37 | - // Connection Factory | |
38 | - // | |
39 | - | |
40 | - containerRegistry.RegisterSingleton<IConnectionFactoryMap, ConnectoinFactoryMap>(); | |
41 | 29 | |
42 | - containerRegistry.RegisterSingleton<IDataSource, DataSource>(); | |
43 | -#endif | |
44 | 30 | /// |
45 | 31 | /// Resources |
46 | 32 | /// |
@@ -1,79 +0,0 @@ | ||
1 | -using CleanAuLait.Core.Log; | |
2 | -using NLog; | |
3 | -using System.Data; | |
4 | -using System.Data.Common; | |
5 | - | |
6 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
7 | -{ | |
8 | - public abstract class AbstractConnectionFactory : IConnectionFactory | |
9 | - { | |
10 | - private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
11 | - | |
12 | - private readonly DbProviderFactory factory; | |
13 | - private readonly string connStr; | |
14 | - private readonly ISet<IDbConnection> connSet; | |
15 | - private bool disposedValue; | |
16 | - | |
17 | - public AbstractConnectionFactory(DbProviderFactory factory, string connStr) | |
18 | - { | |
19 | - this.factory = factory; | |
20 | - this.connStr = connStr; | |
21 | - this.connSet = new HashSet<IDbConnection>(); | |
22 | - } | |
23 | - | |
24 | - public virtual IDbConnection GetConnection() | |
25 | - { | |
26 | - IDbConnection conn = CreateConnection(); | |
27 | - | |
28 | - _ = connSet.Add(conn); | |
29 | - | |
30 | - return conn; | |
31 | - } | |
32 | - | |
33 | - protected IDbConnection CreateConnection() | |
34 | - { | |
35 | - IDbConnection conn = factory.CreateConnection(); | |
36 | - conn.ConnectionString = connStr; | |
37 | - conn.Open(); | |
38 | - return conn; | |
39 | - } | |
40 | - | |
41 | - #region IDisposable | |
42 | - | |
43 | - protected virtual void Dispose(bool disposing) | |
44 | - { | |
45 | - if (!disposedValue) | |
46 | - { | |
47 | - if (disposing) | |
48 | - { | |
49 | - // TODO: マネージド状態を破棄します (マネージド オブジェクト) | |
50 | - foreach (IDbConnection conn in connSet) | |
51 | - { | |
52 | - conn.Dispose(); | |
53 | - logger.Trace("{0} disposed.", conn.ToHashString()); | |
54 | - } | |
55 | - } | |
56 | - | |
57 | - // TODO: アンマネージド リソース (アンマネージド オブジェクト) を解放し、ファイナライザーをオーバーライドします | |
58 | - // TODO: 大きなフィールドを null に設定します | |
59 | - disposedValue = true; | |
60 | - } | |
61 | - } | |
62 | - | |
63 | - // TODO: 'Dispose(bool disposing)' にアンマネージド リソースを解放するコードが含まれる場合にのみ、ファイナライザーをオーバーライドします | |
64 | - //~AbstractConnectionFactory() | |
65 | - //{ | |
66 | - // // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
67 | - // Dispose(disposing: false); | |
68 | - //} | |
69 | - | |
70 | - public void Dispose() | |
71 | - { | |
72 | - // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
73 | - Dispose(disposing: true); | |
74 | - GC.SuppressFinalize(this); | |
75 | - } | |
76 | - | |
77 | - #endregion | |
78 | - } | |
79 | -} |
@@ -1,77 +0,0 @@ | ||
1 | -using System.Data; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public class ConnectoinFactoryMap : IConnectionFactoryMap | |
6 | - { | |
7 | - public IDictionary<string, IConnectionFactory> factoryMap; | |
8 | - | |
9 | - private bool disposedValue; | |
10 | - | |
11 | - public ConnectoinFactoryMap() | |
12 | - { | |
13 | - factoryMap = new Dictionary<string, IConnectionFactory>(); | |
14 | - } | |
15 | - | |
16 | - public void Clear() | |
17 | - { | |
18 | - foreach (IConnectionFactory factory in factoryMap?.Values) | |
19 | - { | |
20 | - factory.Dispose(); | |
21 | - } | |
22 | - | |
23 | - factoryMap.Clear(); | |
24 | - } | |
25 | - | |
26 | - public void Add(string key, IConnectionFactory factory) | |
27 | - { | |
28 | - factoryMap.Add(key, factory); | |
29 | - } | |
30 | - | |
31 | - public IDbConnection GetConnection() | |
32 | - { | |
33 | - return GetConnection(""); | |
34 | - } | |
35 | - | |
36 | - public IDbConnection GetConnection(string name) | |
37 | - { | |
38 | - if (name == null) | |
39 | - { | |
40 | - throw new ArgumentNullException("name must not be null."); | |
41 | - } | |
42 | - | |
43 | - return factoryMap[name].GetConnection(); | |
44 | - } | |
45 | - | |
46 | - protected virtual void Dispose(bool disposing) | |
47 | - { | |
48 | - if (!disposedValue) | |
49 | - { | |
50 | - if (disposing) | |
51 | - { | |
52 | - // TODO: マネージド状態を破棄します (マネージド オブジェクト) | |
53 | - Clear(); | |
54 | - } | |
55 | - | |
56 | - // TODO: アンマネージド リソース (アンマネージド オブジェクト) を解放し、ファイナライザーをオーバーライドします | |
57 | - // TODO: 大きなフィールドを null に設定します | |
58 | - | |
59 | - disposedValue = true; | |
60 | - } | |
61 | - } | |
62 | - | |
63 | - // // TODO: 'Dispose(bool disposing)' にアンマネージド リソースを解放するコードが含まれる場合にのみ、ファイナライザーをオーバーライドします | |
64 | - // ~ConnectoinFactoryMap() | |
65 | - // { | |
66 | - // // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
67 | - // Dispose(disposing: false); | |
68 | - // } | |
69 | - | |
70 | - public void Dispose() | |
71 | - { | |
72 | - // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
73 | - Dispose(disposing: true); | |
74 | - GC.SuppressFinalize(this); | |
75 | - } | |
76 | - } | |
77 | -} |
@@ -1,62 +0,0 @@ | ||
1 | -using System.Data; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public class DataSource : IDataSource | |
6 | - { | |
7 | - private readonly IConnectionFactoryMap factoryMap; | |
8 | - | |
9 | - private bool disposedValue; | |
10 | - | |
11 | - public DataSource(IConnectionFactoryMap factoryMap) | |
12 | - { | |
13 | - this.factoryMap = factoryMap; | |
14 | - } | |
15 | - | |
16 | - public IDbConnection GetConnection() | |
17 | - { | |
18 | - return factoryMap.GetConnection(); | |
19 | - } | |
20 | - | |
21 | - public IDbConnection GetConnection(string name) | |
22 | - { | |
23 | - return factoryMap.GetConnection(name); | |
24 | - } | |
25 | - | |
26 | - #region IDisposable | |
27 | - | |
28 | - protected virtual void Dispose(bool disposing) | |
29 | - { | |
30 | - if (!disposedValue) | |
31 | - { | |
32 | - if (disposing) | |
33 | - { | |
34 | - // TODO: マネージド状態を破棄します (マネージド オブジェクト) | |
35 | - factoryMap.Dispose(); | |
36 | - } | |
37 | - | |
38 | - // TODO: アンマネージド リソース (アンマネージド オブジェクト) を解放し、ファイナライザーをオーバーライドします | |
39 | - // TODO: 大きなフィールドを null に設定します | |
40 | - | |
41 | - disposedValue = true; | |
42 | - } | |
43 | - } | |
44 | - | |
45 | - //// TODO: 'Dispose(bool disposing)' にアンマネージド リソースを解放するコードが含まれる場合にのみ、ファイナライザーをオーバーライドします | |
46 | - //~DataSource() | |
47 | - //{ | |
48 | - // // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
49 | - // Dispose(disposing: false); | |
50 | - //} | |
51 | - | |
52 | - public void Dispose() | |
53 | - { | |
54 | - // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
55 | - Dispose(disposing: true); | |
56 | - GC.SuppressFinalize(this); | |
57 | - } | |
58 | - | |
59 | - #endregion | |
60 | - } | |
61 | -} | |
62 | - |
@@ -1,101 +0,0 @@ | ||
1 | -using CleanAuLait.Core.Log; | |
2 | -using System.Data; | |
3 | -using System.Text; | |
4 | - | |
5 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
6 | -{ | |
7 | - public class DbConnectionWrapper : IDbConnectionWrapper | |
8 | - { | |
9 | - private const string MANAGED_ERROR = "The transaction is managed by the transaction manager. So, we can't "; | |
10 | - | |
11 | - private readonly IDbConnection conn; | |
12 | - private readonly string name; | |
13 | - | |
14 | - public string Database => this.conn.Database; | |
15 | - | |
16 | - public string ConnectionString { get => this.conn.ConnectionString; set => throw new NotSupportedException(); } | |
17 | - | |
18 | - public int ConnectionTimeout => this.conn.ConnectionTimeout; | |
19 | - | |
20 | - public ConnectionState State => this.conn.State; | |
21 | - | |
22 | - public IDbConnection GetRawConnection() => this.conn; | |
23 | - | |
24 | - public DbConnectionWrapper(IDbConnection conn, string name) | |
25 | - { | |
26 | - this.conn = conn; | |
27 | - this.name = name; | |
28 | - } | |
29 | - | |
30 | - public IDbTransaction BeginTransaction() | |
31 | - { | |
32 | - throw new InvalidOperationException(MANAGED_ERROR + "begin a nested transaction here."); | |
33 | - } | |
34 | - | |
35 | - public IDbTransaction BeginTransaction(IsolationLevel isolationLevel) | |
36 | - { | |
37 | - throw new InvalidOperationException(MANAGED_ERROR + "begin a nested transaction here."); | |
38 | - } | |
39 | - | |
40 | - public void ChangeDatabase(string databaseName) | |
41 | - { | |
42 | - throw new InvalidOperationException(MANAGED_ERROR + "change the database here."); | |
43 | - } | |
44 | - | |
45 | - public void Open() | |
46 | - { | |
47 | - throw new InvalidOperationException(MANAGED_ERROR + "open this connection here."); | |
48 | - } | |
49 | - | |
50 | - public void Close() | |
51 | - { | |
52 | - throw new InvalidOperationException(MANAGED_ERROR + "close this connectino here."); | |
53 | - } | |
54 | - | |
55 | - public IDbCommand CreateCommand() | |
56 | - { | |
57 | - return this.conn.CreateCommand(); | |
58 | - } | |
59 | - | |
60 | - public void Dispose() | |
61 | - { | |
62 | - // NOP | |
63 | - // The connection is managed by the transaction manager and does not need to be disposed here. | |
64 | - } | |
65 | - | |
66 | - public override string ToString() | |
67 | - { | |
68 | - StringBuilder buf = new StringBuilder(); | |
69 | - | |
70 | - buf.Append("DbConnectionWrapper["); | |
71 | - | |
72 | - buf.Append("name="); | |
73 | - buf.Append(name); | |
74 | - | |
75 | - buf.Append("; conn="); | |
76 | - buf.Append(LogHelper.ToObjectHashString(this.conn)); | |
77 | - | |
78 | - buf.Append("]"); | |
79 | - | |
80 | - return buf.ToString(); | |
81 | - } | |
82 | - | |
83 | - public static IDbConnection GetRawConnection(IDbConnection conn) | |
84 | - { | |
85 | - if (conn is IDbConnectionWrapper connWrapper) | |
86 | - { | |
87 | - conn = connWrapper.GetRawConnection(); | |
88 | - } | |
89 | - | |
90 | - return conn; | |
91 | - } | |
92 | - | |
93 | -#if false | |
94 | - public static IDatabase CreateDatabaseFromRawConnection(IDbConnection conn) | |
95 | - { | |
96 | - return new Database((DbConnection)GetRawConnection(conn)); | |
97 | - } | |
98 | -#endif | |
99 | - } | |
100 | -} | |
101 | - | |
\ No newline at end of file |
@@ -1,186 +0,0 @@ | ||
1 | -using CleanAuLait.Core.Log; | |
2 | -using NLog; | |
3 | -using System.Data; | |
4 | -using System.Diagnostics; | |
5 | - | |
6 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
7 | -{ | |
8 | - internal class DbTransactionWrapper : IDbTransactionWrapper | |
9 | - { | |
10 | - private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
11 | - | |
12 | - private readonly ITransactionManager txMan; | |
13 | - private IDbConnectionWrapper conn; | |
14 | - private IDbTransaction tx; | |
15 | - | |
16 | - private readonly string name; | |
17 | - | |
18 | - private bool terminatedValue; | |
19 | - private bool disposedValue; | |
20 | - | |
21 | - public IDbConnection Connection => this.tx.Connection; | |
22 | - | |
23 | - public IsolationLevel IsolationLevel => this.tx.IsolationLevel; | |
24 | - | |
25 | - public DbTransactionWrapper(ITransactionManager txMan, IDbConnectionWrapper conn, IDbTransaction tx, string name) | |
26 | - { | |
27 | - Debug.Assert(tx != null); | |
28 | - | |
29 | - this.txMan = txMan; | |
30 | - | |
31 | - this.conn = conn; | |
32 | - this.tx = tx; | |
33 | - this.name = name; | |
34 | - | |
35 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} のラップを開始しました", | |
36 | - name, ToRawConnID(), ToTransactionID()); | |
37 | - } | |
38 | - | |
39 | - private void SwitchTransaction(bool needNewTx) | |
40 | - { | |
41 | - if (this.tx != null) | |
42 | - { | |
43 | - this.tx.Dispose(); | |
44 | - | |
45 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} を破棄しました", | |
46 | - name, ToRawConnID(), ToTransactionID()); | |
47 | - } | |
48 | - | |
49 | - if (!needNewTx) | |
50 | - { | |
51 | - this.tx = null; | |
52 | - this.terminatedValue = true; | |
53 | - logger.Trace("DB接続 [{0}] {1}: トランザクションを終端しました", | |
54 | - name, ToRawConnID()); | |
55 | - return; | |
56 | - } | |
57 | - | |
58 | - // 新しいトランザクションの開始にあたって、トランザクションマネージャからコネクションを取り直すべきか? | |
59 | - // ※トランザクションマネージャ側で名前に紐付くコネクションが更新されるとラッパー側と整合性が取れなくなるため | |
60 | - // →リクエストスコープのトランザクション中ではコネクションをアプリ側で更新するのはマナー違反なのでこのままでよい? | |
61 | - | |
62 | - IDbConnectionWrapper currentTxConn = txMan.GetConnection(name); | |
63 | - | |
64 | - if (this.conn.GetRawConnection() != currentTxConn.GetRawConnection()) | |
65 | - { | |
66 | - logger.Warn("DB接続 [{0}] {1}: カレントのDB接続が {2} に変更されました", | |
67 | - name, ToRawConnID(), ToRawConnID(currentTxConn)); | |
68 | - this.conn = currentTxConn; | |
69 | - } | |
70 | - | |
71 | - this.tx = this.conn.GetRawConnection().BeginTransaction(); | |
72 | - | |
73 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} を開始しました", | |
74 | - name, ToRawConnID(), ToTransactionID()); | |
75 | - } | |
76 | - | |
77 | - public void Commit() | |
78 | - { | |
79 | - Commit(true); | |
80 | - } | |
81 | - | |
82 | - public void Commit(bool needNewTx) | |
83 | - { | |
84 | - if (this.terminatedValue) | |
85 | - { | |
86 | - logger.Trace("DB接続 [{0}]: トランザクションは終端されているため、コミットできません", name); | |
87 | - return; | |
88 | - } | |
89 | - | |
90 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} をコミットします (新規Tx生成 = {3})", | |
91 | - name, ToRawConnID(), ToTransactionID(), needNewTx); | |
92 | - | |
93 | - this.tx.Commit(); | |
94 | - | |
95 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} をコミットしました", | |
96 | - name, ToRawConnID(), ToTransactionID()); | |
97 | - | |
98 | - SwitchTransaction(needNewTx); | |
99 | - } | |
100 | - | |
101 | - public void Rollback() | |
102 | - { | |
103 | - Rollback(true); | |
104 | - } | |
105 | - | |
106 | - public void Rollback(bool needNewTx) | |
107 | - { | |
108 | - if (this.terminatedValue) | |
109 | - { | |
110 | - logger.Trace("DB接続 [{0}]: トランザクションは終端されているため、ロールバックできません", name); | |
111 | - return; | |
112 | - } | |
113 | - | |
114 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} をロールバックします (新規Tx生成 = {3})", | |
115 | - name, ToRawConnID(), ToTransactionID(), needNewTx); | |
116 | - | |
117 | - this.tx.Rollback(); | |
118 | - | |
119 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} をロールバックしました", | |
120 | - name, ToRawConnID(), ToTransactionID()); | |
121 | - | |
122 | - SwitchTransaction(needNewTx); | |
123 | - } | |
124 | - | |
125 | - protected virtual void Dispose(bool disposing) | |
126 | - { | |
127 | - if (!disposedValue) | |
128 | - { | |
129 | - if (disposing) | |
130 | - { | |
131 | - // TODO: マネージド状態を破棄します (マネージド オブジェクト) | |
132 | - | |
133 | - if (!this.terminatedValue) | |
134 | - { | |
135 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} の破棄が開始されました", | |
136 | - name, ToRawConnID(), ToTransactionID()); | |
137 | - | |
138 | - Rollback(false); | |
139 | - } | |
140 | - | |
141 | - if (this.tx != null) | |
142 | - { | |
143 | - logger.Trace("DB接続 [{0}] {1}: トランザクション {2} を破棄しました", | |
144 | - name, ToRawConnID(), ToTransactionID()); | |
145 | - | |
146 | - this.tx.Dispose(); | |
147 | - } | |
148 | - } | |
149 | - | |
150 | - // TODO: アンマネージド リソース (アンマネージド オブジェクト) を解放し、ファイナライザーをオーバーライドします | |
151 | - // TODO: 大きなフィールドを null に設定します | |
152 | - disposedValue = true; | |
153 | - } | |
154 | - } | |
155 | - | |
156 | - // // TODO: 'Dispose(bool disposing)' にアンマネージド リソースを解放するコードが含まれる場合にのみ、ファイナライザーをオーバーライドします | |
157 | - // ~DbTransactionWrapper() | |
158 | - // { | |
159 | - // // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
160 | - // Dispose(disposing: false); | |
161 | - // } | |
162 | - | |
163 | - public void Dispose() | |
164 | - { | |
165 | - // このコードを変更しないでください。クリーンアップ コードを 'Dispose(bool disposing)' メソッドに記述します | |
166 | - Dispose(disposing: true); | |
167 | - System.GC.SuppressFinalize(this); | |
168 | - } | |
169 | - | |
170 | - private string ToRawConnID() | |
171 | - { | |
172 | - return ToRawConnID(this.conn); | |
173 | - } | |
174 | - | |
175 | - private static string ToRawConnID(IDbConnectionWrapper conn) | |
176 | - { | |
177 | - return conn.GetRawConnection().ToHashString(); | |
178 | - } | |
179 | - | |
180 | - private string ToTransactionID() | |
181 | - { | |
182 | - return this.tx.ToHashString(); | |
183 | - } | |
184 | - | |
185 | - } | |
186 | -} |
@@ -1,9 +0,0 @@ | ||
1 | -using System.Data; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public interface IConnectionFactory : IDisposable | |
6 | - { | |
7 | - IDbConnection GetConnection(); | |
8 | - } | |
9 | -} |
@@ -1,14 +0,0 @@ | ||
1 | -using System.Data; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public interface IConnectionFactoryMap : IDisposable | |
6 | - { | |
7 | - IDbConnection GetConnection(); | |
8 | - IDbConnection GetConnection(string name); | |
9 | - | |
10 | - void Clear(); | |
11 | - | |
12 | - void Add(string key, IConnectionFactory factory); | |
13 | - } | |
14 | -} |
@@ -1,10 +0,0 @@ | ||
1 | -using System.Data; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public interface IDataSource : IDisposable | |
6 | - { | |
7 | - IDbConnection GetConnection(); | |
8 | - IDbConnection GetConnection(string name); | |
9 | - } | |
10 | -} | |
\ No newline at end of file |
@@ -1,9 +0,0 @@ | ||
1 | -using System.Data; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public interface IDbConnectionWrapper : IDbConnection | |
6 | - { | |
7 | - IDbConnection GetRawConnection(); | |
8 | - } | |
9 | -} | |
\ No newline at end of file |
@@ -1,10 +0,0 @@ | ||
1 | -using System.Data; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public interface IDbTransactionWrapper : IDbTransaction | |
6 | - { | |
7 | - void Commit(bool needNewTx); | |
8 | - void Rollback(bool needNewTx); | |
9 | - } | |
10 | -} | |
\ No newline at end of file |
@@ -1,17 +0,0 @@ | ||
1 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
2 | -{ | |
3 | - public interface ITransactionManager | |
4 | - { | |
5 | - string DEFAULT_NAME { get; set; } | |
6 | - IDbTransactionWrapper BeginTransaction(); | |
7 | - IDbTransactionWrapper BeginTransaction(string name); | |
8 | - IDbTransactionWrapper GetTransaction(); | |
9 | - IDbTransactionWrapper GetTransaction(string name); | |
10 | - void RemoveTransaction(string name); | |
11 | - | |
12 | - IDbConnectionWrapper GetConnection(); | |
13 | - IDbConnectionWrapper GetConnection(string name); | |
14 | - | |
15 | - void ClearConnectionCacheMap(); | |
16 | - } | |
17 | -} | |
\ No newline at end of file |
@@ -1,11 +0,0 @@ | ||
1 | -using MySql.Data.MySqlClient; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public class MysqlConnectionFactory : AbstractConnectionFactory | |
6 | - { | |
7 | - public MysqlConnectionFactory(string connStr) : base(MySqlClientFactory.Instance, connStr) | |
8 | - { | |
9 | - } | |
10 | - } | |
11 | -} |
@@ -1,11 +0,0 @@ | ||
1 | -using Npgsql; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public class NpgsqlConnectionFactory : AbstractConnectionFactory | |
6 | - { | |
7 | - public NpgsqlConnectionFactory(string connStr) : base(NpgsqlFactory.Instance, connStr) | |
8 | - { | |
9 | - } | |
10 | - } | |
11 | -} |
@@ -1,11 +0,0 @@ | ||
1 | -using System.Data.SQLite; | |
2 | - | |
3 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
4 | -{ | |
5 | - public class SQLiteConnectionFactory : AbstractConnectionFactory | |
6 | - { | |
7 | - public SQLiteConnectionFactory(string connStr) : base(SQLiteFactory.Instance, connStr) | |
8 | - { | |
9 | - } | |
10 | - } | |
11 | -} |
@@ -1,122 +0,0 @@ | ||
1 | -using CleanAuLait.Core.Log; | |
2 | -using NLog; | |
3 | -using System.Data; | |
4 | - | |
5 | -namespace CleanAuLait.OuterEdge.Repository.Db.Tx | |
6 | -{ | |
7 | - /* | |
8 | - * TOOD: FactoryMapにアプリケーション設定を反映させるタイミングの検討 | |
9 | - * TODO: Dialectの注入方法の検討(現状は型引数だが... DIから生成しなければ現状のままで良い?) | |
10 | - */ | |
11 | - | |
12 | - public class TransactionManager : ITransactionManager | |
13 | - { | |
14 | - private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
15 | - | |
16 | - public string DEFAULT_NAME { get; set; } = string.Empty; | |
17 | - | |
18 | - private readonly IDataSource dataSource; | |
19 | - private readonly IDictionary<string, IDbConnectionWrapper> connMap = new Dictionary<string, IDbConnectionWrapper>(); | |
20 | - private readonly IDictionary<string, IDbTransactionWrapper> txMap = new Dictionary<string, IDbTransactionWrapper>(); | |
21 | - | |
22 | - public TransactionManager(IDataSource dataSource) | |
23 | - { | |
24 | - this.dataSource = dataSource; | |
25 | - } | |
26 | - | |
27 | - public IDbTransactionWrapper BeginTransaction() | |
28 | - { | |
29 | - return BeginTransaction(DEFAULT_NAME); | |
30 | - } | |
31 | - | |
32 | - public IDbTransactionWrapper BeginTransaction(string name) | |
33 | - { | |
34 | - IDbConnectionWrapper conn = GetConnection(name); | |
35 | - IDbTransaction tx = conn.GetRawConnection().BeginTransaction(); | |
36 | - | |
37 | - IDbTransactionWrapper txWrapper = new DbTransactionWrapper(this, conn,tx, name); | |
38 | - | |
39 | - txMap.Add(name, txWrapper); | |
40 | - | |
41 | - return txWrapper; | |
42 | - } | |
43 | - | |
44 | - public IDbTransactionWrapper BeginTransaction(IsolationLevel isolationLevel) | |
45 | - { | |
46 | - return BeginTransaction(DEFAULT_NAME, isolationLevel); | |
47 | - } | |
48 | - | |
49 | - public IDbTransactionWrapper BeginTransaction(string name, IsolationLevel isolationLevel) | |
50 | - { | |
51 | - IDbConnectionWrapper conn = GetConnection(name); | |
52 | - IDbTransaction tx = conn.GetRawConnection().BeginTransaction(isolationLevel); | |
53 | - | |
54 | - IDbTransactionWrapper txWrapper = new DbTransactionWrapper(this, conn, tx, name); | |
55 | - | |
56 | - txMap.Add(name, txWrapper); | |
57 | - | |
58 | - return txWrapper; | |
59 | - } | |
60 | - | |
61 | - public IDbTransactionWrapper GetTransaction() | |
62 | - { | |
63 | - return GetTransaction(DEFAULT_NAME); | |
64 | - } | |
65 | - | |
66 | - public IDbTransactionWrapper GetTransaction(string name) | |
67 | - { | |
68 | - return txMap[name]; | |
69 | - } | |
70 | - | |
71 | - public void RemoveTransaction(string name) | |
72 | - { | |
73 | - txMap.Remove(name); | |
74 | - } | |
75 | - | |
76 | - public IDbConnectionWrapper GetConnection() | |
77 | - { | |
78 | - return GetConnection(DEFAULT_NAME); | |
79 | - } | |
80 | - | |
81 | - public IDbConnectionWrapper GetConnection(string name) | |
82 | - { | |
83 | - if (connMap.TryGetValue(name, out IDbConnectionWrapper conn)) | |
84 | - { | |
85 | - logger.Trace("既存DB接続 [{0}] {1} の状態は [{2}] です.", name, ToConnectionID(conn), conn.State); | |
86 | - | |
87 | - if (conn.State == ConnectionState.Open) | |
88 | - { | |
89 | - logger.Trace("既存DB接続 [{0}] {1} を取得しました.", name, ToConnectionID(conn)); | |
90 | - return conn; | |
91 | - } | |
92 | - | |
93 | - logger.Trace("OpenでないDB接続 [{0}] {1} を破棄しました.", name, ToConnectionID(conn)); | |
94 | - | |
95 | - connMap[name].GetRawConnection().Dispose(); | |
96 | - connMap.Remove(name); | |
97 | - } | |
98 | - | |
99 | - IDbConnection rawConn = dataSource.GetConnection(name); | |
100 | - | |
101 | - conn = new DbConnectionWrapper(rawConn, name); | |
102 | - | |
103 | - connMap.Add(name, conn); | |
104 | - | |
105 | - logger.Trace("新規DB接続 [{0}] {1} を取得しました.", name, ToConnectionID(conn)); | |
106 | - | |
107 | - return conn; | |
108 | - } | |
109 | - | |
110 | - public void ClearConnectionCacheMap() | |
111 | - { | |
112 | - // Ensure the raw connections must be already disposed. | |
113 | - this.connMap.Clear(); | |
114 | - } | |
115 | - | |
116 | - private static string ToConnectionID(IDbConnectionWrapper conn) | |
117 | - { | |
118 | - return conn.GetRawConnection().ToHashString(); | |
119 | - } | |
120 | - | |
121 | - } | |
122 | -} |