Base solution for your next web application
Open Closed

SetInterval sql connection problem #9181


User avatar
0
nenjamio created

Hi team, we have a problem on setInterval() implementation. We use this function from the frontend in order to update a number every 30 seconds. This makes that when the core connects to database, the sql connections were taken and not released at time.

I think it could be the async function, because the sql connection is opened and closed correctly. Its a very simple function but we canĀ“t detect why this is happenning.

Could you help me about this?

Example: FRONTEND TS

this.balanceTimer = setInterval(() => {
            this._serviceProxy.updateNumber(this.appSession.user.id).subscribe((aux) =>
            {
            }); 
        }, 30000); 

CORE

Task<decimal> UpdateNumber(int userId);

public async Task<decimal> UpdateNumber(int userId)
        {
            decimal aux = 0;
            using (var con = new SqlConnection(_connString))
            using (var cmd = new SqlCommand("UpdateAux", con)) {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
                con.Open();

                using (var rd = await cmd.ExecuteReaderAsync()) {
                    while (rd.Read()) {
                        aux = Convert.ToDecimal(rd["AuxNumber"]);
                    }
                }
                con.Close();
            }
            return aux;
        }

Regards


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Could you change your method like below;

    public async Task<decimal> UpdateNumber(int userId)
    {
    	decimal aux = 0;
    	using (var con = new SqlConnection(""))
    	using (var cmd = new SqlCommand("UpdateAux", con))
    	{
    		cmd.CommandType = CommandType.StoredProcedure;
    		cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
    		await con.OpenAsync();
    
    		using (var rd = await cmd.ExecuteReaderAsync())
    		{
    			while (await rd.ReadAsync())
    			{
    				aux = Convert.ToDecimal(rd["AuxNumber"]);
    			}
    		}
    
    		await con.CloseAsync();
    	}
    
    	return aux;
    }