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?

Tom
  • 1,387
  • 3
  • 19
  • 30
alp_the_developer
  • 615
  • 1
  • 9
  • 23
  • 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. – grepe Oct 28 '16 at 12:37
  • 2
    you should use a `using` statement for proper disposal. [This q/a should help you](http://stackoverflow.com/q/5567097/2679750) – Mark C. Oct 28 '16 at 12:47
  • @MarkC. you are right i should use using statement. – alp_the_developer Oct 28 '16 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). – Joel Coehoorn Oct 28 '16 at 13:52

1 Answers1

-2

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

struggleendlessly
  • 108
  • 2
  • 2
  • 8
  • No mention of a using statement or anything? Can you elaborate on your answer as to why OPs question is the best approach? – Mark C. Oct 28 '16 at 12:48