/// <summary> /// Initializes a new instance of the <see cref="DbQueryRunner"/> class. /// </summary> /// <param name="dbConnectionPool">The <see cref="IDbConnectionPool"/> that provided the <see cref="DbConnection"/>.</param> /// <param name="conn">The <see cref="DbConnection"/> to use.</param> /// <exception cref="ArgumentNullException"><paramref name="dbConnectionPool"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="conn"/> is null.</exception> public DbQueryRunner(IDbConnectionPool dbConnectionPool, DbConnection conn) { if (dbConnectionPool == null) { throw new ArgumentNullException("dbConnectionPool"); } if (conn == null) { throw new ArgumentNullException("conn"); } _dbConnectionPool = dbConnectionPool; _conn = conn; if (Connection.State == ConnectionState.Closed || Connection.State == ConnectionState.Broken) { Connection.Open(); } // Create and start the worker thread _workerThread = new Thread(WorkerThreadLoop) { Name = "DbQueryRunner worker thread", IsBackground = true }; _workerThread.Start(); }
/// <summary> /// Acquires a connection from the pool. Ensures it's open. Invokes the action. /// Ensures the connection is returned to the pool when the action is complete. /// Useful for single-line operations. /// </summary> /// <typeparam name="TConnection">The connection type.</typeparam> /// <param name="connectionPool">The connection pool to acquire connections from.</param> /// <param name="action">The action to execute.</param> /// <param name="cancellationToken">An optional cancellation token.</param> public static async ValueTask OpenAsync <TConnection>(this IDbConnectionPool <TConnection> connectionPool, Func <TConnection, ConnectionState, ValueTask> action, CancellationToken cancellationToken = default) where TConnection : IDbConnection { if (connectionPool is null) { throw new ArgumentNullException(nameof(connectionPool)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } Contract.EndContractBlock(); var conn = connectionPool.Take(); try { await action(conn, await conn.EnsureOpenAsync(cancellationToken)) .ConfigureAwait(false); } finally { connectionPool.Give(conn); } }
/// <param name="connectionPool">The pool to acquire connections from.</param> /// <param name="type">The command type.</param> /// <param name="command">The SQL command.</param> /// <param name="params">The list of params</param> public ExpressiveSqlCommand( IDbConnectionPool <SqlConnection> connectionPool, CommandType type, string command, IEnumerable <Param>? @params = null) : base(connectionPool, type, command, @params) { }
/// <param name="connectionPool">The pool to acquire connections from.</param> /// <param name="type">The command type.</param> /// <param name="command">The SQL command.</param> /// <param name="params">The list of params</param> public ExpressiveCommand( IDbConnectionPool connectionPool, CommandType type, string command, IEnumerable <Param>? @params = null) : base(connectionPool.AsGeneric(), type, command, @params) { }
public OracleContext(IDbConnectionPool connectionPool) : base(connectionPool) { if (connectionPool is OracleDbConnectionPool) { dbConnection = (OracleConnection)connectionPool.RequestConnection(); } else { throw new InvalidCastException("OracleRepository nur mit OracleDbConnectionPool verwenden"); } }
/// <summary> /// 为该数据库引擎对象配置并使用您自己实现的连接池. /// </summary> /// <param name="connectionPool">连接池的对象实例.</param> public void UseConnectionPool(IDbConnectionPool connectionPool) { if (connectionPool == null) { return; } if (DBA == null) { throw new Exception("The connection pool can't be configured when the DBA object is null (DBA 对象为空时无法配置连接池)."); } DBA.ConnectionPool = connectionPool; }
/// <summary> /// Initializes a new instance of the <see cref="DbQueryRunner"/> class. /// </summary> /// <param name="dbConnectionPool">The <see cref="IDbConnectionPool"/> that provided the <see cref="DbConnection"/>.</param> /// <param name="conn">The <see cref="DbConnection"/> to use.</param> /// <exception cref="ArgumentNullException"><paramref name="dbConnectionPool"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="conn"/> is null.</exception> public DbQueryRunner(IDbConnectionPool dbConnectionPool, DbConnection conn) { if (dbConnectionPool == null) throw new ArgumentNullException("dbConnectionPool"); if (conn == null) throw new ArgumentNullException("conn"); _dbConnectionPool = dbConnectionPool; _conn = conn; if (Connection.State == ConnectionState.Closed || Connection.State == ConnectionState.Broken) Connection.Open(); // Create and start the worker thread _workerThread = new Thread(WorkerThreadLoop) { Name = "DbQueryRunner worker thread", IsBackground = true }; _workerThread.Start(); }
/// <param name="connectionPool">The pool to acquire connections from.</param> /// <param name="type">The command type.</param> /// <param name="command">The SQL command.</param> /// <param name="params">The list of params</param> protected ExpressiveCommandBase( IDbConnectionPool <TConnection> connectionPool, CommandType type, string command, IEnumerable <Param>? @params) { ConnectionProvider = connectionPool ?? throw new ArgumentNullException(nameof(connectionPool)); Command = command ?? throw new ArgumentNullException(nameof(command)); if (string.IsNullOrWhiteSpace(command)) { throw new ArgumentException("Cannot be null or whitespace.", nameof(command)); } Contract.EndContractBlock(); Type = type; Params = @params?.ToList() ?? new List <Param>(); Timeout = CommandTimeout.DEFAULT_SECONDS; }
public TransientConnectionHolder(IDbConnectionPool pool) { _pool = pool; DbConnection = _pool.ConnectionPool?.Get(); if (DbConnection != null) { _objectShouldReturnToPool = true; } else { DbConnection = _pool.CreateDbConnection(); _objectShouldReturnToPool = false; } if (DbConnection != null && DbConnection.State == System.Data.ConnectionState.Closed) {//预先打开数据库,为的是确保在使用EF的场合(比如ASP.Net)不会频繁打开/关闭数据库而致使性能下降 DbConnection.Open(); } }
/// <summary> /// Acquires a connection from the pool, returning it after the action is complete. /// Useful for single-line operations. /// </summary> /// <param name="connectionPool">The connection pool to acquire connections from.</param> /// <param name="action">The action to execute.</param> public static async ValueTask UsingAsync(this IDbConnectionPool connectionPool, Func <IDbConnection, ValueTask> action) { if (connectionPool is null) { throw new ArgumentNullException(nameof(connectionPool)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } Contract.EndContractBlock(); var conn = connectionPool.Take(); try { await action(conn).ConfigureAwait(false); } finally { connectionPool.Give(conn); } }
/// <summary> /// Acquires a connection from the pool, returning it after the action is complete. /// Useful for single-line operations. /// </summary> /// <param name="connectionPool">The connection pool to acquire connections from.</param> /// <param name="action">The action to execute.</param> public static void Using(this IDbConnectionPool connectionPool, Action <IDbConnection> action) { if (connectionPool is null) { throw new ArgumentNullException(nameof(connectionPool)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } Contract.EndContractBlock(); var conn = connectionPool.Take(); try { action(conn); } finally { connectionPool.Give(conn); } }
/// <summary> /// Acquires a connection from the pool, returning it after the action is complete. /// Useful for single-line operations. /// </summary> /// <typeparam name="T">The type returned from the action.</typeparam> /// <param name="connectionPool">The connection pool to acquire connections from.</param> /// <param name="action">The action to execute.</param> /// <returns>The value from the action.</returns> public static T Using <T>(this IDbConnectionPool connectionPool, Func <IDbConnection, T> action) { if (connectionPool is null) { throw new ArgumentNullException(nameof(connectionPool)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } Contract.EndContractBlock(); var conn = connectionPool.Take(); try { return(action(conn)); } finally { connectionPool.Give(conn); } }
/// <summary> /// Acquires a connection from the pool, returning it after the action is complete. /// Useful for single-line operations. /// </summary> /// <typeparam name="TConn">The connection type.</typeparam> /// <typeparam name="T">The type returned from the action.</typeparam> /// <param name="connectionPool">The connection pool to acquire connections from.</param> /// <param name="action">The action to execute.</param> /// <returns>The value from the action.</returns> public static async ValueTask <T> UsingAsync <TConn, T>(this IDbConnectionPool <TConn> connectionPool, Func <TConn, ValueTask <T> > action) where TConn : IDbConnection { if (connectionPool is null) { throw new ArgumentNullException(nameof(connectionPool)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } Contract.EndContractBlock(); var conn = connectionPool.Take(); try { return(await action(conn).ConfigureAwait(false)); } finally { connectionPool.Give(conn); } }
/// <summary> /// Acquires a connection from the pool. Ensures it's open. Invokes the action. /// Ensures the connection is returned to the pool when the action is complete. /// Useful for single-line operations. /// </summary> /// <typeparam name="TConnection">The connection type.</typeparam> /// <param name="connectionPool">The connection pool to acquire connections from.</param> /// <param name="action">The action to execute.</param> public static void Open <TConnection>(this IDbConnectionPool <TConnection> connectionPool, Action <TConnection, ConnectionState> action) where TConnection : IDbConnection { if (connectionPool is null) { throw new ArgumentNullException(nameof(connectionPool)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } Contract.EndContractBlock(); var conn = connectionPool.Take(); try { action(conn, conn.EnsureOpen()); } finally { connectionPool.Give(conn); } }
/// <summary> /// Initializes a new instance of the <see cref="PooledDbConnection"/> class. /// </summary> /// <param name="pool">The <see cref="IDbConnectionPool"/> that this instance belongs to.</param> internal PooledDbConnection(IDbConnectionPool pool) { _pool = pool; }
/// <summary> /// Converts a non-generic connection factory to a generic one. /// </summary> /// <param name="connectionPool">The source connection factory.</param> /// <returns>The generic version of the source factory.</returns> public static IDbConnectionPool <IDbConnection> AsGeneric(this IDbConnectionPool connectionPool) => connectionPool is IDbConnectionPool <IDbConnection> p ? p : new GenericPool(connectionPool);
public GenericPool(IDbConnectionPool source) { _source = source ?? throw new ArgumentNullException(nameof(source)); }
public OracleRatingContext(IDbConnectionPool connectionPool) : base(connectionPool) { }
public OracleArticleContext(IDbConnectionPool connectionPool) : base(connectionPool) { }
/// <summary> /// Creates an ExpressiveSqlCommand for subsequent configuration and execution. /// </summary> /// <param name="connectionSource">The connection pool to take connections from.</param> /// <param name="command">The command text or stored procedure name to use.</param> /// <param name="type">The command type. Default = CommandType.Text.</param> /// <returns>The resultant ExpressiveSqlCommand.</returns> public static ExpressiveSqlCommand Command( this IDbConnectionPool <SqlConnection> connectionSource, string command, CommandType type = CommandType.Text) => new ExpressiveSqlCommand(connectionSource, type, command);
/// <summary> /// Creates an ExpressiveSqlCommand with command type set to StoredProcedure for subsequent configuration and execution. /// </summary> /// <param name="connectionSource">The connection pool to take connections from.</param> /// <param name="procedureName">The stored procedure name to use.</param> /// <returns>The resultant ExpressiveSqlCommand.</returns> public static ExpressiveSqlCommand StoredProcedure( this IDbConnectionPool <SqlConnection> connectionSource, string procedureName) => new ExpressiveSqlCommand(connectionSource, CommandType.StoredProcedure, procedureName);
/// <summary> /// Creates an ExpressiveDbCommand with command type set to StoredProcedure for subsequent configuration and execution. /// </summary> /// <typeparam name="TConnection">The connection type.</typeparam> /// <param name="connectionSource">The connection pool to take connections from.</param> /// <param name="procedureName">The stored procedure name to use.</param> /// <returns>The resultant ExpressiveDbCommand.</returns> public static ExpressiveDbCommand StoredProcedure <TConnection>( this IDbConnectionPool <TConnection> connectionSource, string procedureName) where TConnection : DbConnection => new ExpressiveDbCommand(connectionSource, CommandType.StoredProcedure, procedureName);
/// <summary> /// Creates an ExpressiveDbCommand for subsequent configuration and execution. /// </summary> /// <typeparam name="TConnection">The connection type.</typeparam> /// <param name="connectionSource">The connection pool to take connections from.</param> /// <param name="command">The command text or stored procedure name to use.</param> /// <param name="type">The command type. Default = CommandType.Text.</param> /// <returns>The resultant ExpressiveDbCommand.</returns> public static ExpressiveDbCommand Command <TConnection>( this IDbConnectionPool <TConnection> connectionSource, string command, CommandType type = CommandType.Text) where TConnection : DbConnection => new ExpressiveDbCommand(connectionSource, type, command);
public Context(IDbConnectionPool connectionPool) { dbConnectionPool = connectionPool; }
public OracleCommentContext(IDbConnectionPool connectionPool) : base(connectionPool) { }