Skip to content

Commit d68b730

Browse files
committed
ConnectionIdleTimeout connection string option
1 parent 739101d commit d68b730

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

docs/content/connection-options.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,28 @@ Connection pooling is enabled by default. These options are used to configure i
106106
<tr>
107107
<td>Connection Lifetime, ConnectionLifeTime</td>
108108
<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>
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) means pooled connections will never incur a ConnectionLifeTime timeout.</td>
110110
</tr>
111111
<tr>
112112
<td>Connection Reset, ConnectionReset </td>
113113
<td>false</td>
114114
<td>If true, the connection state is reset when it is retrieved from the pool. The default value of false avoids making an additional server round trip when obtaining a connection, but the connection state is not reset.</td>
115115
</tr>
116+
<tr>
117+
<td>Connection Idle Timeout, ConnectionIdleTimeout</td>
118+
<td>180</td>
119+
<td>The amount of time in seconds that a connection can remain idle in the pool. Any connection that is idle for longer is subject to being closed by a background task that runs every minute, unless there are only MinimumPoolSize connections left in the pool. A value of zero (0) means pooled connections will never incur a ConnectionIdleTimeout.</td>
120+
</tr>
116121
<tr>
117122
<td>Maximum Pool Size, Max Pool Size, MaximumPoolsize, maxpoolsize</td>
118123
<td>100</td>
119124
<td>The maximum number of connections allowed in the pool.</td>
120125
</tr>
126+
<tr>
127+
<td>Minimum Pool Size, Min Pool Size, MinimumPoolSize, minpoolsize</td>
128+
<td>0</td>
129+
<td>The minimum number of connections to leave in the pool if ConnectionIdleTimeout is reached.</td>
130+
</tr>
121131
</table>
122132

123133
Other Options

src/MySqlConnector/MySqlClient/MySqlConnectionStringBuilder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public bool ConnectionReset
8585
set => MySqlConnectionStringOption.ConnectionReset.SetValue(this, value);
8686
}
8787

88+
public uint ConnectionIdleTimeout
89+
{
90+
get => MySqlConnectionStringOption.ConnectionIdleTimeout.GetValue(this);
91+
set => MySqlConnectionStringOption.ConnectionIdleTimeout.SetValue(this, value);
92+
}
93+
8894
public uint MinimumPoolSize
8995
{
9096
get => MySqlConnectionStringOption.MinimumPoolSize.GetValue(this);
@@ -231,6 +237,7 @@ internal abstract class MySqlConnectionStringOption
231237
public static readonly MySqlConnectionStringOption<bool> Pooling;
232238
public static readonly MySqlConnectionStringOption<uint> ConnectionLifeTime;
233239
public static readonly MySqlConnectionStringOption<bool> ConnectionReset;
240+
public static readonly MySqlConnectionStringOption<uint> ConnectionIdleTimeout;
234241
public static readonly MySqlConnectionStringOption<uint> MinimumPoolSize;
235242
public static readonly MySqlConnectionStringOption<uint> MaximumPoolSize;
236243

@@ -321,6 +328,10 @@ static MySqlConnectionStringOption()
321328
keys: new[] { "Connection Reset", "ConnectionReset" },
322329
defaultValue: true));
323330

331+
AddOption(ConnectionIdleTimeout = new MySqlConnectionStringOption<uint>(
332+
keys: new[] { "Connection Idle Timeout", "ConnectionIdleTimeout" },
333+
defaultValue: 180));
334+
324335
AddOption(MinimumPoolSize = new MySqlConnectionStringOption<uint>(
325336
keys: new[] { "Minimum Pool Size", "Min Pool Size", "MinimumPoolSize", "minpoolsize" },
326337
defaultValue: 0));

src/MySqlConnector/Serialization/ConnectionSettings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public ConnectionSettings(MySqlConnectionStringBuilder csb)
3838
Pooling = csb.Pooling;
3939
ConnectionLifeTime = (int)csb.ConnectionLifeTime;
4040
ConnectionReset = csb.ConnectionReset;
41+
ConnectionIdleTimeout = (int)csb.ConnectionIdleTimeout;
4142
if (csb.MinimumPoolSize > csb.MaximumPoolSize)
4243
throw new MySqlException("MaximumPoolSize must be greater than or equal to MinimumPoolSize");
4344
MinimumPoolSize = (int)csb.MinimumPoolSize;
@@ -80,6 +81,7 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
8081
Pooling = other.Pooling;
8182
ConnectionLifeTime = other.ConnectionLifeTime;
8283
ConnectionReset = other.ConnectionReset;
84+
ConnectionIdleTimeout = other.ConnectionIdleTimeout;
8385
MinimumPoolSize = other.MinimumPoolSize;
8486
MaximumPoolSize = other.MaximumPoolSize;
8587

@@ -116,6 +118,7 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
116118
internal readonly bool Pooling;
117119
internal readonly int ConnectionLifeTime;
118120
internal readonly bool ConnectionReset;
121+
internal readonly int ConnectionIdleTimeout;
119122
internal readonly int MinimumPoolSize;
120123
internal readonly int MaximumPoolSize;
121124

@@ -134,4 +137,3 @@ private ConnectionSettings(ConnectionSettings other, bool? useCompression)
134137
}
135138

136139
}
137-

tests/MySqlConnector.Tests/MySqlConnectionStringBuilderTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void Defaults()
2525
Assert.Equal("", csb.Database);
2626
#if !BASELINE
2727
Assert.Equal(false, csb.BufferResultSets);
28+
Assert.Equal(180u, csb.ConnectionIdleTimeout);
2829
Assert.Equal(false, csb.ForceSynchronous);
2930
#endif
3031
Assert.Equal(0u, csb.Keepalive);
@@ -64,6 +65,7 @@ public void ParseConnectionString()
6465
"ConnectionReset=false;" +
6566
"Convert Zero Datetime=true;" +
6667
#if !BASELINE
68+
"connectionidletimeout=30;" +
6769
"bufferresultsets=true;" +
6870
"forcesynchronous=true;" +
6971
#endif
@@ -91,6 +93,7 @@ public void ParseConnectionString()
9193
Assert.Equal("schema_name", csb.Database);
9294
#if !BASELINE
9395
Assert.Equal(true, csb.BufferResultSets);
96+
Assert.Equal(30u, csb.ConnectionIdleTimeout);
9497
Assert.Equal(true, csb.ForceSynchronous);
9598
#endif
9699
Assert.Equal(90u, csb.Keepalive);

0 commit comments

Comments
 (0)