Skip to content

Commit b1ad9ac

Browse files
committed
add ConectionLifeTime connection string option
1 parent 42fcae2 commit b1ad9ac

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

docs/content/connection-options.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ Connection pooling is enabled by default. These options are used to configure i
103103
<td>true</td>
104104
<td>When true, the MySqlConnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool. Recognized values are true, false, yes, and no.</td>
105105
</tr>
106+
<tr>
107+
<td>Connection Lifetime, ConnectionLifeTime</td>
108+
<td>0</td>
109+
<td>When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online. A value of zero (0) causes pooled connections to have the maximum connection timeout.</td>
110+
</tr>
106111
<tr>
107112
<td>Connection Reset, ConnectionReset </td>
108113
<td>false</td>

src/MySqlConnector/MySqlClient/MySqlConnectionStringBuilder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public bool Pooling
7373
set => MySqlConnectionStringOption.Pooling.SetValue(this, value);
7474
}
7575

76+
public uint ConnectionLifeTime
77+
{
78+
get => MySqlConnectionStringOption.ConnectionLifeTime.GetValue(this);
79+
set => MySqlConnectionStringOption.ConnectionLifeTime.SetValue(this, value);
80+
}
81+
7682
public bool ConnectionReset
7783
{
7884
get => MySqlConnectionStringOption.ConnectionReset.GetValue(this);
@@ -223,6 +229,7 @@ internal abstract class MySqlConnectionStringOption
223229

224230
// Connection Pooling Options
225231
public static readonly MySqlConnectionStringOption<bool> Pooling;
232+
public static readonly MySqlConnectionStringOption<uint> ConnectionLifeTime;
226233
public static readonly MySqlConnectionStringOption<bool> ConnectionReset;
227234
public static readonly MySqlConnectionStringOption<uint> MinimumPoolSize;
228235
public static readonly MySqlConnectionStringOption<uint> MaximumPoolSize;
@@ -306,6 +313,10 @@ static MySqlConnectionStringOption()
306313
keys: new[] { "Pooling" },
307314
defaultValue: true));
308315

316+
AddOption(ConnectionLifeTime = new MySqlConnectionStringOption<uint>(
317+
keys: new[] { "Connection Lifetime", "ConnectionLifeTime" },
318+
defaultValue: 0));
319+
309320
AddOption(ConnectionReset = new MySqlConnectionStringOption<bool>(
310321
keys: new[] { "Connection Reset", "ConnectionReset" },
311322
defaultValue: true));

src/MySqlConnector/Serialization/ConnectionSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public ConnectionSettings(MySqlConnectionStringBuilder csb)
3636

3737
// Connection Pooling Options
3838
Pooling = csb.Pooling;
39+
ConnectionLifeTime = (int)csb.ConnectionLifeTime;
3940
ConnectionReset = csb.ConnectionReset;
4041
if (csb.MinimumPoolSize > csb.MaximumPoolSize)
4142
throw new MySqlException("MaximumPoolSize must be greater than or equal to MinimumPoolSize");
@@ -77,6 +78,7 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
7778

7879
// Connection Pooling Options
7980
Pooling = other.Pooling;
81+
ConnectionLifeTime = other.ConnectionLifeTime;
8082
ConnectionReset = other.ConnectionReset;
8183
MinimumPoolSize = other.MinimumPoolSize;
8284
MaximumPoolSize = other.MaximumPoolSize;
@@ -112,6 +114,7 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
112114

113115
// Connection Pooling Options
114116
internal readonly bool Pooling;
117+
internal readonly int ConnectionLifeTime;
115118
internal readonly bool ConnectionReset;
116119
internal readonly int MinimumPoolSize;
117120
internal readonly int MaximumPoolSize;

tests/MySqlConnector.Tests/MySqlConnectionStringBuilderTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public void Defaults()
1717
#if BASELINE
1818
Assert.Equal(false, csb.ConnectionReset);
1919
#else
20+
Assert.Equal(0u, csb.ConnectionLifeTime);
2021
Assert.Equal(true, csb.ConnectionReset);
2122
#endif
2223
Assert.Equal(15u, csb.ConnectionTimeout);
@@ -59,6 +60,7 @@ public void ParseConnectionString()
5960
"Character Set=latin1;" +
6061
"Compress=true;" +
6162
"connect timeout=30;" +
63+
"connection lifetime=15;" +
6264
"ConnectionReset=false;" +
6365
"Convert Zero Datetime=true;" +
6466
#if !BASELINE
@@ -82,6 +84,7 @@ public void ParseConnectionString()
8284
Assert.Equal("file.pfx", csb.CertificateFile);
8385
Assert.Equal("Pass1234", csb.CertificatePassword);
8486
Assert.Equal("latin1", csb.CharacterSet);
87+
Assert.Equal(15u, csb.ConnectionLifeTime);
8588
Assert.Equal(false, csb.ConnectionReset);
8689
Assert.Equal(30u, csb.ConnectionTimeout);
8790
Assert.Equal(true, csb.ConvertZeroDateTime);

0 commit comments

Comments
 (0)