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