|
| 1 | +#if NET7_0_OR_GREATER |
| 2 | +using MySqlConnector.Core; |
| 3 | +using MySqlConnector.Logging; |
| 4 | +using MySqlConnector.Protocol.Serialization; |
| 5 | + |
| 6 | +namespace MySqlConnector; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// <see cref="MySqlDataSource"/> implements a MySQL data source which can be used to obtain open connections, and against which commands can be executed directly. |
| 10 | +/// </summary> |
| 11 | +public sealed class MySqlDataSource : DbDataSource |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Initializes a new instance of the <see cref="MySqlDataSource"/> class. |
| 15 | + /// </summary> |
| 16 | + /// <param name="connectionString">The connection string for the MySQL Server. This parameter is required.</param> |
| 17 | + /// <exception cref="ArgumentNullException">Thrown if <paramref name="connectionString"/> is <c>null</c>.</exception> |
| 18 | + public MySqlDataSource(string connectionString) |
| 19 | + { |
| 20 | + m_connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); |
| 21 | + Pool = ConnectionPool.CreatePool(m_connectionString); |
| 22 | + m_id = Interlocked.Increment(ref s_lastId); |
| 23 | + m_logArguments = new object?[2] { m_id, null }; |
| 24 | + if (Pool is not null) |
| 25 | + { |
| 26 | + m_logArguments[1] = Pool.Id; |
| 27 | + Log.Info("DataSource{0} created with Pool {1}", m_logArguments); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + Log.Info("DataSource{0} created with no pool", m_logArguments); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Creates a new <see cref="MySqlConnection"/> that can connect to the database represented by this <see cref="MySqlDataSource"/>. |
| 37 | + /// </summary> |
| 38 | + /// <remarks> |
| 39 | + /// <para>The connection must be opened before it can be used.</para> |
| 40 | + /// <para>It is the responsibility of the caller to properly dispose the connection returned by this method. Failure to do so may result in a connection leak.</para> |
| 41 | + /// </remarks> |
| 42 | + public new MySqlConnection CreateConnection() => (MySqlConnection) base.CreateConnection(); |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Returns a new, open <see cref="MySqlConnection"/> to the database represented by this <see cref="MySqlDataSource"/>. |
| 46 | + /// </summary> |
| 47 | + /// <remarks> |
| 48 | + /// <para>The returned connection is already open, and is ready for immediate use.</para> |
| 49 | + /// <para>It is the responsibility of the caller to properly dispose the connection returned by this method. Failure to do so may result in a connection leak.</para> |
| 50 | + /// </remarks> |
| 51 | + public new MySqlConnection OpenConnection() => (MySqlConnection) base.OpenConnection(); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Asynchronously returns a new, open <see cref="MySqlConnection"/> to the database represented by this <see cref="MySqlDataSource"/>. |
| 55 | + /// </summary> |
| 56 | + /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param> |
| 57 | + /// <remarks> |
| 58 | + /// <para>The returned connection is already open, and is ready for immediate use.</para> |
| 59 | + /// <para>It is the responsibility of the caller to properly dispose the connection returned by this method. Failure to do so may result in a connection leak.</para> |
| 60 | + /// </remarks> |
| 61 | + public new async ValueTask<MySqlConnection> OpenConnectionAsync(CancellationToken cancellationToken = default) => |
| 62 | + (MySqlConnection) await base.OpenConnectionAsync(cancellationToken).ConfigureAwait(false); |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets the connection string of the database represented by this <see cref="MySqlDataSource"/>. |
| 66 | + /// </summary> |
| 67 | + public override string ConnectionString => m_connectionString; |
| 68 | + |
| 69 | + protected override DbConnection CreateDbConnection() |
| 70 | + { |
| 71 | + if (m_isDisposed) |
| 72 | + throw new ObjectDisposedException(nameof(MySqlDataSource)); |
| 73 | + return new MySqlConnection(this); |
| 74 | + } |
| 75 | + |
| 76 | + protected override void Dispose(bool disposing) |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + if (disposing) |
| 81 | + DisposeAsync(IOBehavior.Synchronous).GetAwaiter().GetResult(); |
| 82 | + } |
| 83 | + finally |
| 84 | + { |
| 85 | + base.Dispose(disposing); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + protected override ValueTask DisposeAsyncCore() => DisposeAsync(IOBehavior.Asynchronous); |
| 90 | + |
| 91 | + private async ValueTask DisposeAsync(IOBehavior ioBehavior) |
| 92 | + { |
| 93 | + if (Pool is not null) |
| 94 | + await Pool.ClearAsync(ioBehavior, default).ConfigureAwait(false); |
| 95 | + m_isDisposed = true; |
| 96 | + } |
| 97 | + |
| 98 | + internal ConnectionPool? Pool { get; } |
| 99 | + |
| 100 | + private static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(MySqlDataSource)); |
| 101 | + private static int s_lastId; |
| 102 | + |
| 103 | + private readonly int m_id; |
| 104 | + private readonly object?[] m_logArguments; |
| 105 | + private readonly string m_connectionString; |
| 106 | + private bool m_isDisposed; |
| 107 | +} |
| 108 | +#endif |
0 commit comments