0

I'm developing an API for my android app.

I'm using the API for accessing MySql from android via Asp.Net.

Like this,

[Route("api/v1/getMethodExample")]
[HttpGet]
public int GetMethodExample(string parametre)
{   
    mySqlConnection = new MySqlConnection("");
    mySqlConnection.Open();
    ...
    //Do something with mysql
    int value = mySqlConnection.get("myTable","4");//in example 
    mySqlConnection.Close();
    return value;
}

In short, for every web API call, I'm connecting MySql, getting data and closing MySql.

Is this right way?

4
  • 1
    generally, i don't see anything wrong with it and to my knowledge e.g. flask app will do the same. you just should not connect more than once per your API call, if you need to query db multiple times to return one result. if you have many thousands calls per second, you can think about something else. Commented Oct 28, 2016 at 12:37
  • 2
    you should use a using statement for proper disposal. This q/a should help you Commented Oct 28, 2016 at 12:47
  • @MarkC. you are right i should use using statement. Commented Oct 28, 2016 at 12:55
  • Sort of. It is correct to open and close new connections. ADO.Net providers using something called connection pooling to keep this efficient. However, the pattern you posted to demonstrate this has some deficiencies (namely the "using" issue mentioned in other comments). Commented Oct 28, 2016 at 13:52

1 Answer 1

-2

Yes. It's default behavior. In other way you will block context for other requests

Sign up to request clarification or add additional context in comments.

1 Comment

No mention of a using statement or anything? Can you elaborate on your answer as to why OPs question is the best approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.