private async Task <bool> releaseAsync(IDbConnectionAsync conn, LockInfo lockInfo) { var state = lockInfo.AsImmutable(); if (!state.HasLock()) { return(true); //released already } var newLockRef = Math.Max(1, state.LockReference + 1); using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, RELEASE_LOCK)) { command.Parameters.Add(new SqlParameter("newLockRef", newLockRef)); command.Parameters.Add(new SqlParameter("resource", state.Resource)); command.Parameters.Add(new SqlParameter("lockReference", state.LockReference)); command.Parameters.Add(new SqlParameter("lockedByPID", _PID)); try { await command.ExecuteNonQueryAsync(); lockInfo.Update(null, TimeSpan.Zero, 0, FrozenTimeClock.Instance); return(true); } catch (Exception ex) { return(false); } } }
private async Task <LockReferenceValue> GetLatestReferenceAsync(IDbConnectionAsync conn, Guid resource) { using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, SELECT_REFERENCE)) { command.Parameters.Add(new SqlParameter("resource", resource)); using (var reader = (await command.ExecuteReaderAsync()) as SqlDataReader) { if (await reader.ReadAsync()) { var lockReference = ReadInt(reader, 0); var lockedTill = ReadDateTime(reader, 1); var utcNow = ReadDateTime(reader, 2); return(new LockReferenceValue() { LockReference = lockReference, IsFree = lockReference != null && (lockedTill == null || lockedTill < utcNow) }); } else { return(new LockReferenceValue() { LockReference = null, IsFree = false }); } } } }
private async Task <LockInfo> acquireLockAsync(IDbConnectionAsync conn, Guid resource, int lockReference, TimeSpan timeout) { var lockInfo = LockInfo.Empty(resource); var newLockRef = Math.Max(1, lockReference + 1); //only acquires lock if old lock reference number matches, and not locked yet. using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, ACQUIRE_lOCK_COMMANDS)) { command.Parameters.Add(new SqlParameter("newLockRef", newLockRef)); command.Parameters.Add(new SqlParameter("timeout", Convert.ToInt64(timeout.TotalMilliseconds))); command.Parameters.Add(new SqlParameter("lockedByMachine", _machineName)); command.Parameters.Add(new SqlParameter("lockedByPID", _PID)); command.Parameters.Add(new SqlParameter("resource", resource)); command.Parameters.Add(new SqlParameter("lockReference", lockReference)); try { var dbLockedTill = (DateTime?)await command.ExecuteScalarAsync(); if (dbLockedTill != null) { var clock = _clockProvider.GetClock(); lockInfo.Update(dbLockedTill, timeout, newLockRef, clock); } } catch (Exception ex) { ex.LogException(); } return(lockInfo); } }
public CachedWordpressRepository( IDbConnectionAsync dbConnection, ICacher cacher, Func <Type, CacherItemPolicy> policyFunc ) : base(dbConnection) { _cacher = cacher; _policyFunc = policyFunc; }
internal static async Task <IDbCommandAsync> CreateDbCommand(IDbConnectionAsync connection, string text) { if (connection.State == System.Data.ConnectionState.Closed) { await connection.OpenAsync(); } var command = connection.CreateCommand(); command.CommandText = text; command.CommandType = System.Data.CommandType.Text; return(command); }
private async Task <bool> insertEntryRecordAsync(IDbConnectionAsync conn, Guid resource) { using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, SQL_INSERT)) { command.Parameters.Add(new SqlParameter("resource", resource)); try { return(await command.ExecuteNonQueryAsync() > 0); } catch (Exception ex) { return(false); } } }
private async Task <int?> GetFreeReferenceOrNewAsync(IDbConnectionAsync conn, Guid resource) { var selectRef = await GetLatestReferenceAsync(conn, resource); if (selectRef.LockReference == null) { // Ensures an entry for the resource is persisted. if (await insertEntryRecordAsync(conn, resource)) { return(0); } } else if (selectRef.IsFree) { return(selectRef.LockReference); } return(null); }
private async Task <bool> renewLockAsync(IDbConnectionAsync conn, TimeSpan renewTimeout, LockInfo lockInfo) { var state = lockInfo.AsImmutable(); if (!state.HasLock()) { return(false); } var newLockRef = Math.Max(1, state.LockReference + 1); using (var command = await DbAccessHelperAsync.CreateDbCommand(conn, RENEW_LOCK_COMMANDS)) { command.Parameters.Add(new SqlParameter("newLockRef", newLockRef)); command.Parameters.Add(new SqlParameter("resource", state.Resource)); command.Parameters.Add(new SqlParameter("lockReference", state.LockReference)); command.Parameters.Add(new SqlParameter("timeout", Convert.ToInt64(renewTimeout.TotalMilliseconds))); command.Parameters.Add(new SqlParameter("lockedByPID", _PID)); try { var dbLockedTill = (DateTime?)await command.ExecuteScalarAsync(); if (dbLockedTill != null) { var clock = _clockProvider.GetClock(); lockInfo.Update(dbLockedTill, renewTimeout, newLockRef, clock); return(lockInfo.AsImmutable().HasLock()); } return(false); } catch (Exception ex) { return(false); } } }
public WordpressRepository( IDbConnectionAsync dbConnection ) { _dbConnection = dbConnection; }
public SchemaDataAdapter(IDbConnectionAsync connection) =>
//TODO unit test this. public static async Task <Result <IReadOnlyCollection <Table> > > GetTablesFromConnectionStringAsync(IDbConnectionAsync connection, Func <ISchemaDataAdapter> adapterFactory) { try { await connection.OpenAsync(); var adapter = adapterFactory(); var tablesNames = adapter.GetTableList(); var result = tablesNames .Select(x => GetTable(adapter, x)) .ToList(); return(Success <IReadOnlyCollection <Table> > .Succeed(result)); } catch (Exception e) { //var d = Failure<SqlError>.Fail(new SqlError()); //return (Result<IReadOnlyCollection<Table>>)d; return(Failure <IReadOnlyCollection <Table>, SqlError> .Fail(new SqlError { Message = e.Message })); } }