public async Task DoNotGoBackToPoolAfterBroken() { var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Pooling = true; await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); } await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); try { await using (var cmd = conn.CreateCommand()) { cmd.CommandText = "select * from mon$statements union all select * from mon$statements"; await using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { } } } } catch (FbException) { } } }
public async Task UseWireCrypt(FbWireCrypt wireCrypt) { if (!EnsureServerVersion(new Version(3, 0, 0, 0))) { return; } if (!EnsureServerType(FbServerType.Default)) { return; } var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.WireCrypt = wireCrypt; await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); const string Pattern = ":[^:]*C[^:]*$"; if (wireCrypt == FbWireCrypt.Enabled || wireCrypt == FbWireCrypt.Required) { StringAssert.IsMatch(Pattern, conn.ServerVersion); } else { StringAssert.DoesNotMatch(Pattern, conn.ServerVersion); } } }
public async Task ConnectionPoolingMinPoolSizeTest() { var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Pooling = true; csb.ConnectionLifeTime = 5; csb.MinPoolSize = 3; var cs = csb.ToString(); var active = await GetActiveConnections(); var connections = new List <FbConnection>(); try { for (var i = 0; i < csb.MinPoolSize * 2; i++) { var connection = new FbConnection(cs); connections.Add(connection); Assert.DoesNotThrowAsync(() => connection.OpenAsync()); } } finally { foreach (var c in connections) { await c.DisposeAsync(); } } Thread.Sleep(TimeSpan.FromSeconds(csb.ConnectionLifeTime * 2)); Assert.AreEqual(active + csb.MinPoolSize, await GetActiveConnections()); }
public async Task UseCompression(bool compression) { if (!EnsureServerVersion(new Version(3, 0, 0, 0))) { return; } if (!EnsureServerType(FbServerType.Default)) { return; } var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Compression = compression; await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); const string Pattern = ":[^:]*Z[^:]*$"; if (compression) { StringAssert.IsMatch(Pattern, conn.ServerVersion); } else { StringAssert.DoesNotMatch(Pattern, conn.ServerVersion); } } }
public async Task DatabaseTriggersTest() { var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Pooling = false; int rows; csb.NoDatabaseTriggers = false; await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); rows = await GetLogRowsCount(conn); } csb.NoDatabaseTriggers = true; await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); Assert.AreEqual(rows, await GetLogRowsCount(conn)); } csb.NoDatabaseTriggers = false; await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); Assert.AreEqual(rows + 1, await GetLogRowsCount(conn)); } }
public async Task ConnectionPoolingLifetimeTest() { FbConnection.ClearAllPools(); var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Pooling = true; csb.ConnectionLifeTime = 5; var cs = csb.ToString(); var active = await GetActiveConnections(); await using (FbConnection myConnection1 = new FbConnection(cs), myConnection2 = new FbConnection(cs)) { await myConnection1.OpenAsync(); await myConnection2.OpenAsync(); Assert.AreEqual(active + 2, await GetActiveConnections()); } Thread.Sleep(TimeSpan.FromSeconds(csb.ConnectionLifeTime * 2)); Assert.AreEqual(active, await GetActiveConnections()); }
public async Task InvalidCredentialsGiveProperError() { if (!EnsureServerType(FbServerType.Default)) { return; } var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Password = string.Empty; await using (var conn = new FbConnection(csb.ToString())) { try { await conn.OpenAsync(); Assert.Fail(); } catch (FbException ex) when(ex.ErrorCode == 335544472) { Assert.Pass(); return; } } }
public async Task InsertTest() { var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Enlist = true; using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await using (var c = new FbConnection(csb.ToString())) { await c.OpenAsync(); var sql = "insert into TEST (int_field, date_field) values (1002, @date)"; await using (var command = new FbCommand(sql, c)) { command.Parameters.Add("@date", FbDbType.Date).Value = DateTime.Now.ToString(); var ra = await command.ExecuteNonQueryAsync(); Assert.AreEqual(ra, 1); } } scope.Complete(); } }
public async Task OpenAsync() { if (connection.State != System.Data.ConnectionState.Open) { await connection.OpenAsync(); } }
public async Task UserIDCorrectlyPassedToServer() { await using (var conn = new FbConnection(BuildConnectionString(ServerType, Compression, WireCrypt))) { await conn.OpenAsync(); await using (var command = conn.CreateCommand()) { command.CommandText = "select CURRENT_USER from RDB$DATABASE"; var loggedUser = (string)await command.ExecuteScalarAsync(); Assert.AreEqual(FbTestsSetup.UserID, loggedUser); } } }
public async Task ConnectionPoolingMaxPoolSizeTest() { FbConnection.ClearAllPools(); var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Pooling = true; csb.ConnectionLifeTime = 120; csb.MaxPoolSize = 10; var cs = csb.ToString(); var connections = new List <FbConnection>(); try { for (var i = 0; i <= csb.MaxPoolSize; i++) { var connection = new FbConnection(cs); connections.Add(connection); if (i == csb.MaxPoolSize) { Assert.ThrowsAsync <InvalidOperationException>(() => connection.OpenAsync()); } else { Assert.DoesNotThrowAsync(() => connection.OpenAsync()); } } } finally { foreach (var c in connections) { await c.DisposeAsync(); } } }
public virtual async Task SetUp() { await FbTestsSetup.SetUp(ServerType, Compression, WireCrypt); var cs = BuildConnectionString(ServerType, Compression, WireCrypt); if (_insertTestData) { await InsertTestData(cs); } Connection = new FbConnection(cs); await Connection.OpenAsync(); ServerVersion = FbServerProperties.ParseServerVersion(Connection.ServerVersion); }
private static async ValueTask OpenConnectionFast(FbConnection connection, SqlDriverConfiguration configuration, bool isAsync, CancellationToken cancellationToken = default) { if (!isAsync) { connection.Open(); SqlHelper.ExecuteInitializationSql(connection, configuration); } else { await connection.OpenAsync().ConfigureAwait(false); await SqlHelper.ExecuteInitializationSqlAsync(connection, configuration, cancellationToken).ConfigureAwait(false); } }
private async void button1_Click(object sender, EventArgs e) { try { FbConnection conexao = new FbConnection(@$ "User={txtUsuario.Text}; Password={txtSenha.Text}; Database={txtDatabase.Text}; DataSource={txtHost.Text}; Port=3050; Dialect=3; Charset=NONE; Role=; Connection lifetime=15; Pooling=true; MinPoolSize=0; MaxPoolSize=50; Packet Size=8192; ServerType=0"); var command = conexao.CreateCommand(); await conexao.OpenAsync(); Interaction.MsgBox("Conexão testada com sucesso", MsgBoxStyle.Information); } catch (Exception erro) { Interaction.MsgBox($@"Message: {erro.Message} InnerException: {erro.InnerException}", MsgBoxStyle.Critical, "Erro"); } }
private static async ValueTask OpenConnectionWithNotification(FbConnection connection, SqlDriverConfiguration configuration, bool isAsync, CancellationToken cancellationToken = default) { var accessors = configuration.DbConnectionAccessors; if (!isAsync) { SqlHelper.NotifyConnectionOpening(accessors, connection); try { connection.Open(); if (!string.IsNullOrEmpty(configuration.ConnectionInitializationSql)) { SqlHelper.NotifyConnectionInitializing(accessors, connection, configuration.ConnectionInitializationSql); } SqlHelper.ExecuteInitializationSql(connection, configuration); SqlHelper.NotifyConnectionOpened(accessors, connection); } catch (Exception ex) { SqlHelper.NotifyConnectionOpeningFailed(accessors, connection, ex); throw; } } else { await SqlHelper.NotifyConnectionOpeningAsync(accessors, connection, false, cancellationToken).ConfigureAwait(false); try { await connection.OpenAsync(cancellationToken).ConfigureAwait(false); if (!string.IsNullOrEmpty(configuration.ConnectionInitializationSql)) { await SqlHelper.NotifyConnectionInitializingAsync(accessors, connection, configuration.ConnectionInitializationSql, false, cancellationToken) .ConfigureAwait(false); } await SqlHelper.ExecuteInitializationSqlAsync(connection, configuration, cancellationToken).ConfigureAwait(false); await SqlHelper.NotifyConnectionOpenedAsync(accessors, connection, false, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { await SqlHelper.NotifyConnectionOpeningFailedAsync(accessors, connection, ex, false, cancellationToken).ConfigureAwait(false); throw; } } }
public void PassCryptKey() { var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt); csb.Database = "enc.fdb"; async Task Test() { await using (var conn = new FbConnection(csb.ToString())) { await conn.OpenAsync(); } } Assert.ThrowsAsync <FbException>(Test); csb.CryptKey = Encoding.ASCII.GetBytes("1234567890123456"); Assert.DoesNotThrowAsync(Test); }