| Version 3 (modified by , 3 days ago) ( diff ) |
|---|
Advanced Application Development
Transactions
Pooling
Since the backend of our application is developed using ASP.NET Core with Entity Framework Core and PostgreSQL, database connections are managed through Npgsql.
In our project, we use the following dependency:
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.8" />
The data source is created in Program.cs using NpgsqlDataSourceBuilder:
var dataSourceBuilder = new NpgsqlDataSourceBuilder(
builder.Configuration.GetConnectionString("KernelRecords"));
var dataSource = dataSourceBuilder.Build();
builder.Services.AddDbContext<KernelRecordsContext>(options =>
options.UseNpgsql(dataSource));
Npgsql has built-in connection pooling, and pooling is enabled by default. This means that the application does not need to establish a completely new physical connection to the PostgreSQL database every time a database operation is performed.
When the application needs access to the database, Npgsql takes an available connection from the connection pool. After the operation is finished, the connection is returned to the pool and can be reused by another request.
In our project, we do not manually change the pooling configuration, so the default Npgsql settings are used. Some of the important default values are:
Pooling = true Minimum Pool Size = 0 Maximum Pool Size = 100 Connection Idle Lifetime = 300 seconds Timeout = 15 seconds
If needed, these values can be changed through the connection string.
Connection pooling improves application performance and reduces resource usage because existing database connections can be reused instead of opening a new connection for every request.
