修訂 | a9ac1a666fffd549e3ab015bdeebb01070e4d03e (tree) |
---|---|
時間 | 2022-11-16 00:35:49 |
作者 | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[MOD] コネクションファクトリ、コネクションファクトリマップ、データソース、トランザクションマネージャをコネクションラッパーに対応
@@ -11,32 +11,48 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
11 | 11 | { |
12 | 12 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
13 | 13 | |
14 | + protected readonly string name; | |
15 | + | |
14 | 16 | private readonly DbProviderFactory factory; |
15 | 17 | private readonly string connStr; |
18 | + | |
16 | 19 | private readonly ISet<IDbConnection> connSet; |
20 | + | |
17 | 21 | private bool disposedValue; |
18 | 22 | |
19 | 23 | public AbstractConnectionFactory(DbProviderFactory factory, string connStr) |
24 | + : this(factory, connStr, "default") | |
25 | + { | |
26 | + } | |
27 | + | |
28 | + public AbstractConnectionFactory(DbProviderFactory factory, string connStr, string name) | |
20 | 29 | { |
21 | 30 | this.factory = factory; |
31 | + | |
22 | 32 | this.connStr = connStr; |
33 | + this.name = name; | |
34 | + | |
23 | 35 | this.connSet = new HashSet<IDbConnection>(); |
24 | 36 | } |
25 | 37 | |
26 | - public virtual IDbConnection GetConnection() | |
38 | + public virtual IDbConnectionWrapper GetConnection() | |
27 | 39 | { |
28 | - IDbConnection conn = CreateConnection(); | |
40 | + IDbConnection conn = CreateRawConnection(); | |
29 | 41 | |
30 | - _ = connSet.Add(conn); | |
31 | - | |
32 | - return conn; | |
42 | + return CreateWrapper(conn, this.name); | |
33 | 43 | } |
34 | 44 | |
35 | - protected IDbConnection CreateConnection() | |
45 | + public abstract IDbConnectionWrapper CreateWrapper(IDbConnection conn, string name); | |
46 | + | |
47 | + protected IDbConnection CreateRawConnection() | |
36 | 48 | { |
37 | 49 | IDbConnection conn = factory.CreateConnection(); |
50 | + | |
38 | 51 | conn.ConnectionString = connStr; |
39 | 52 | conn.Open(); |
53 | + | |
54 | + _ = connSet.Add(conn); | |
55 | + | |
40 | 56 | return conn; |
41 | 57 | } |
42 | 58 |
@@ -1,6 +1,5 @@ | ||
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | -using System.Data; | |
4 | 3 | |
5 | 4 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
6 | 5 | { |
@@ -30,12 +29,12 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
30 | 29 | factoryMap.Add(key, factory); |
31 | 30 | } |
32 | 31 | |
33 | - public IDbConnection GetConnection() | |
32 | + public IDbConnectionWrapper GetConnection() | |
34 | 33 | { |
35 | 34 | return GetConnection(""); |
36 | 35 | } |
37 | 36 | |
38 | - public IDbConnection GetConnection(string name) | |
37 | + public IDbConnectionWrapper GetConnection(string name) | |
39 | 38 | { |
40 | 39 | if (name == null) |
41 | 40 | { |
@@ -1,5 +1,4 @@ | ||
1 | 1 | using System; |
2 | -using System.Data; | |
3 | 2 | |
4 | 3 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
5 | 4 | { |
@@ -14,12 +13,12 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
14 | 13 | this.factoryMap = factoryMap; |
15 | 14 | } |
16 | 15 | |
17 | - public IDbConnection GetConnection() | |
16 | + public IDbConnectionWrapper GetConnection() | |
18 | 17 | { |
19 | 18 | return factoryMap.GetConnection(); |
20 | 19 | } |
21 | 20 | |
22 | - public IDbConnection GetConnection(string name) | |
21 | + public IDbConnectionWrapper GetConnection(string name) | |
23 | 22 | { |
24 | 23 | return factoryMap.GetConnection(name); |
25 | 24 | } |
@@ -1,10 +1,9 @@ | ||
1 | 1 | using System; |
2 | -using System.Data; | |
3 | 2 | |
4 | 3 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
5 | 4 | { |
6 | 5 | public interface IConnectionFactory : IDisposable |
7 | 6 | { |
8 | - IDbConnection GetConnection(); | |
7 | + IDbConnectionWrapper GetConnection(); | |
9 | 8 | } |
10 | 9 | } |
@@ -1,12 +1,11 @@ | ||
1 | 1 | using System; |
2 | -using System.Data; | |
3 | 2 | |
4 | 3 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
5 | 4 | { |
6 | 5 | public interface IConnectionFactoryMap : IDisposable |
7 | 6 | { |
8 | - IDbConnection GetConnection(); | |
9 | - IDbConnection GetConnection(string name); | |
7 | + IDbConnectionWrapper GetConnection(); | |
8 | + IDbConnectionWrapper GetConnection(string name); | |
10 | 9 | |
11 | 10 | void Clear(); |
12 | 11 |
@@ -1,11 +1,10 @@ | ||
1 | 1 | using System; |
2 | -using System.Data; | |
3 | 2 | |
4 | 3 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
5 | 4 | { |
6 | 5 | public interface IDataSource : IDisposable |
7 | 6 | { |
8 | - IDbConnection GetConnection(); | |
9 | - IDbConnection GetConnection(string name); | |
7 | + IDbConnectionWrapper GetConnection(); | |
8 | + IDbConnectionWrapper GetConnection(string name); | |
10 | 9 | } |
11 | 10 | } |
\ No newline at end of file |
@@ -1,15 +1,11 @@ | ||
1 | 1 | using CleanAuLait48.Core.Log; |
2 | 2 | using NLog; |
3 | +using System; | |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Data; |
5 | 6 | |
6 | 7 | namespace CleanAuLait48.OuterEdge.Repository.Db.Tx |
7 | 8 | { |
8 | - /* | |
9 | - * TOOD: FactoryMapにアプリケーション設定を反映させるタイミングの検討 | |
10 | - * TODO: Dialectの注入方法の検討(現状は型引数だが... DIから生成しなければ現状のままで良い?) | |
11 | - */ | |
12 | - | |
13 | 9 | public class TransactionManager : ITransactionManager |
14 | 10 | { |
15 | 11 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
@@ -35,7 +31,7 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
35 | 31 | IDbConnectionWrapper conn = GetConnection(name); |
36 | 32 | IDbTransaction tx = conn.GetRawConnection().BeginTransaction(); |
37 | 33 | |
38 | - IDbTransactionWrapper txWrapper = new DbTransactionWrapper(this, conn,tx, name); | |
34 | + IDbTransactionWrapper txWrapper = new DbTransactionWrapper(this, conn, tx, name); | |
39 | 35 | |
40 | 36 | txMap.Add(name, txWrapper); |
41 | 37 |
@@ -83,6 +79,16 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
83 | 79 | { |
84 | 80 | if (connMap.TryGetValue(name, out IDbConnectionWrapper conn)) |
85 | 81 | { |
82 | + try | |
83 | + { | |
84 | + conn.TestConnection(); | |
85 | + } | |
86 | + catch (Exception e) | |
87 | + { | |
88 | + logger.Warn(e); | |
89 | + logger.Warn("既存DB接続 [{0}] {1} は疎通テストに失敗しました。", name, ToConnectionID(conn)); | |
90 | + } | |
91 | + | |
86 | 92 | logger.Trace("既存DB接続 [{0}] {1} の状態は [{2}] です.", name, ToConnectionID(conn), conn.State); |
87 | 93 | |
88 | 94 | if (conn.State == ConnectionState.Open) |
@@ -97,15 +103,13 @@ namespace CleanAuLait48.OuterEdge.Repository.Db.Tx | ||
97 | 103 | connMap.Remove(name); |
98 | 104 | } |
99 | 105 | |
100 | - IDbConnection rawConn = dataSource.GetConnection(name); | |
101 | - | |
102 | - conn = new DbConnectionWrapper(rawConn, name); | |
106 | + IDbConnectionWrapper wrapper = dataSource.GetConnection(name); | |
103 | 107 | |
104 | - connMap.Add(name, conn); | |
108 | + connMap.Add(name, wrapper); | |
105 | 109 | |
106 | - logger.Trace("新規DB接続 [{0}] {1} を取得しました.", name, ToConnectionID(conn)); | |
110 | + logger.Trace("新規DB接続 [{0}] {1} を取得しました.", name, ToConnectionID(wrapper)); | |
107 | 111 | |
108 | - return conn; | |
112 | + return wrapper; | |
109 | 113 | } |
110 | 114 | |
111 | 115 | public void ClearConnectionCacheMap() |