在.NET AWS Lambda中使用MySql.Data和依赖注入(D.I)。

huangapple go评论100阅读模式
英文:

Using MySql.Data in .NET AWS Lambda with D.I

问题

  1. 我看到旧的代码库在创建新的 MySqlConnection 时使用了 using 语句,但在 MySQL 文档中没有看到这种用法。我在创建新的 MySqlConnection 时是否需要使用 "using" 语句?

  2. 我应该将 MySqlConnection 注册为 Transient 并使用依赖注入,还是简单地在使用它的类内部创建一个新的 MySqlConnection 实例?

  3. 数据库连接的依赖注入是否会导致大量未关闭的连接?或者,另一种极端情况是,单个连接是否会因频繁使用而减慢 Lambda 函数的速度?

谢谢!

英文:

I'm refactoring a .NET AWS Lambda project with MySql.Data in it for connecting to AWS Aurora. I implemented the DI process and registered MySqlConnection into DI container as Transient and injected it into the DataAccess service.

The registration of MySqlConnection and the DataAccess go something like this:

Startup:

services.AddTransient<MySqlConnection>(provider => 
{
    //Get the necessary parameters and build a connection string.

    return new MySqlConnection(connectionString);
});

services.AddSingleton<IDataAccess, DataAccess>();

DataAccess:

private readonly MySqlConnection _connection;

// inject the MySqlConnection.

async function() 
{
  await _connection.OpenAsync();
  await _connection.DoStuff();
  await _connection.CloseAsync();
}

My boss concerned that all of the DB interaction will be going through a single connection that is injected into the DataAccess class and she doesn't want the db to crash because I'm not cleaning up the connections properly.

I have a few questions:

  1. I see the old code base has the using statement when it create a new MySqlConnection but I don't see one on the MySql documentation. Do I need the "using" statement when I create a new MySqlConnection?
  2. Should I register MySqlConnection as Transient and use DI or simply create a new MySqlConnection instance inside the class that use it?
  3. Is the DI of the DB connection going to leave a lot of open connections? Or, on the other extreme, that a single connection is going to slow down the lambda because it used to make numerous connections?

Thank you!

答案1

得分: 2

In my opinion, instead of using DI, you can go with using statements. SQL connections will be picked from the pool that .NET maintains at the application level, and will be returned once the connection is closed. Refer to this answer for more details.

However, connection pooling won't help much in the case of AWS Lambda, as the entire Lambda compute is destroyed after the idle timeout. So it is recommended to use RDS proxy to avoid connection-related issues.

Using RDS Proxy, you can handle unpredictable surges in database traffic. Otherwise, these surges might cause issues due to oversubscribing connections or creating new connections at a fast rate. RDS Proxy establishes a database connection pool and reuses connections in this pool. This approach avoids the memory and CPU overhead of opening a new database connection each time. To protect the database against oversubscription, you can control the number of database connections that are created.

Also, you can check out RDS Proxy for SQL Server with AWS Lambda and .NET 6.

英文:

IMO, instead of using DI, you can go with using statements. SQL connections will be picked from pool that .NET maintain at application level, and will be returned once connection is closed. Refer this answer for more detail.

However, connection pooling won't help much in case of AWS Lambda, as entire Lambda compute is destroyed after idle timeout. So it is recommended to use RDS proxy to avoid connection related issues.

> Using RDS Proxy, you can handle unpredictable surges in database traffic. Otherwise, these surges might cause issues due to oversubscribing connections or creating new connections at a fast rate. RDS Proxy establishes a database connection pool and reuses connections in this pool. This approach avoids the memory and CPU overhead of opening a new database connection each time. To protect the database against oversubscription, you can control the number of database connections that are created.

Also, you can checkout RDS Proxy for SQL Server with AWS Lambda and .NET 6

huangapple
  • 本文由 发表于 2023年5月26日 12:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定