简体   繁体   中英

.net core web api close database connection

I am trying to build a blog webapi with this web api core demo and microsoft doc . I use Npgsql connect to a PostgreSQL database, and add some methods, but the database return error message "too many clients connected" when I debugging.

So how to close the connections in webapi? I read some documents but can't find a way to do with it.

my repo here

The easiest solution is to use using block. This will take care of database connection closing automatically and you don't have to manually close the database connection.

eg.

using (SqlConnection connection = new SqlConnection(connectionString))
{
   // Your database code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM