@@ -11,36 +11,43 @@ public class DatabaseFixture : IDisposable
11
11
{
12
12
public DatabaseFixture ( )
13
13
{
14
- // increase the number of worker threads to reduce number of spurious failures from threadpool starvation
14
+ lock ( s_lock )
15
+ {
16
+ if ( ! s_isInitialized )
17
+ {
18
+ // increase the number of worker threads to reduce number of spurious failures from threadpool starvation
15
19
#if NETCOREAPP1_1_2
16
- // from https://stackoverflow.com/a/42982698
17
- typeof ( ThreadPool ) . GetMethod ( "SetMinThreads" , BindingFlags . Public | BindingFlags . Static ) . Invoke ( null , new object [ ] { 64 , 64 } ) ;
20
+ // from https://stackoverflow.com/a/42982698
21
+ typeof ( ThreadPool ) . GetMethod ( "SetMinThreads" , BindingFlags . Public | BindingFlags . Static ) . Invoke ( null , new object [ ] { 64 , 64 } ) ;
18
22
#else
19
- ThreadPool . SetMinThreads ( 64 , 64 ) ;
23
+ ThreadPool . SetMinThreads ( 64 , 64 ) ;
20
24
#endif
21
25
22
- var csb = AppConfig . CreateConnectionStringBuilder ( ) ;
23
- var connectionString = csb . ConnectionString ;
24
- var database = csb . Database ;
25
- csb . Database = "" ;
26
- using ( var db = new MySqlConnection ( csb . ConnectionString ) )
27
- {
28
- db . Open ( ) ;
29
- using ( var cmd = db . CreateCommand ( ) )
30
- {
31
- cmd . CommandText = $ "create schema if not exists { database } ;";
32
- cmd . ExecuteNonQuery ( ) ;
33
-
34
- if ( ! string . IsNullOrEmpty ( AppConfig . SecondaryDatabase ) )
26
+ var csb = AppConfig . CreateConnectionStringBuilder ( ) ;
27
+ var database = csb . Database ;
28
+ csb . Database = "" ;
29
+ using ( var db = new MySqlConnection ( csb . ConnectionString ) )
35
30
{
36
- cmd . CommandText = $ "create schema if not exists { AppConfig . SecondaryDatabase } ;";
37
- cmd . ExecuteNonQuery ( ) ;
31
+ db . Open ( ) ;
32
+ using ( var cmd = db . CreateCommand ( ) )
33
+ {
34
+ cmd . CommandText = $ "create schema if not exists { database } ;";
35
+ cmd . ExecuteNonQuery ( ) ;
36
+
37
+ if ( ! string . IsNullOrEmpty ( AppConfig . SecondaryDatabase ) )
38
+ {
39
+ cmd . CommandText = $ "create schema if not exists { AppConfig . SecondaryDatabase } ;";
40
+ cmd . ExecuteNonQuery ( ) ;
41
+ }
42
+ }
43
+ db . Close ( ) ;
38
44
}
45
+
46
+ s_isInitialized = true ;
39
47
}
40
- db . Close ( ) ;
41
48
}
42
49
43
- Connection = new MySqlConnection ( connectionString ) ;
50
+ Connection = new MySqlConnection ( AppConfig . ConnectionString ) ;
44
51
}
45
52
46
53
public MySqlConnection Connection { get ; }
@@ -57,5 +64,8 @@ protected virtual void Dispose(bool disposing)
57
64
Connection . Dispose ( ) ;
58
65
}
59
66
}
67
+
68
+ static object s_lock = new object ( ) ;
69
+ static bool s_isInitialized ;
60
70
}
61
71
}
0 commit comments