-3

I've got an error in my C# application. I'm not sure if it's my program or my website. It's a gaming emulator and it says after 1-2 hours running 'Too many connections'. It also says it on my website.

The line this code is erroring on is below, and it errors and highlights the words connection.Open(); when it crashes. I think it has something to do with not closing the connections.

//C# Coding (In VB)

private static SqlDatabaseClient CreateClient(int Id)
{
    MySqlConnection connection = new MySqlConnection(GenerateConnectionString());
    connection.Open();
    return new SqlDatabaseClient(Id, connection);
}

//Application error

[04:51] Exception - Session -> To many connection[]MySqlData.MySqlClient.MySqlPacket ReadPacket<> @ at MysqlData.MySqlClient.MySqlStream.Readpacket<>
at MySql.Data.MySqlClient.NativeDriver.Open<>
at MySql.Data.MySqlClient.Driver.Open<>
at MySql.Data.MySqlClient.Driver.Create
at MySql.Data.MySqlClient.MySqlPool.GtPooledConnection<>
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver<>
at MySql.Data.MySqlClient.MySqlPool.GetConnection<>
at MySql.Data.MySqlClient.MySqlConnection.Open<>
at Reality.Storage.SqlDatabaeManager.CreateClient in C:\iRP\SqlDatabaseClient.cs:line 24d
Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • 4
    Do you close your db connections anywhere? – I4V Aug 27 '13 at 13:25
  • Are you closing those open Connections once you are through with them? There is a limited amount of Connections you can have open. – Bearcat9425 Aug 27 '13 at 13:25
  • I dont think there is anything that closes them no, i got this gaing cnsle application off a friend who coded it so i wouldnt know much about it – Asan Smith Aug 27 '13 at 13:26
  • 1
    You need to close the connections once you are through otherwise they just stay open and when new ones are created your connection limits start to be reached till at some point you receive this error. – Bearcat9425 Aug 27 '13 at 13:27
  • 1
    Next time please include the code and error yourself. There is absolutely no reason why you shouldn't be able to do that. – Bart Aug 27 '13 at 13:27
  • is there any coding i could add to close it? im begginer with c# – Asan Smith Aug 27 '13 at 13:29
  • http://stackoverflow.com/questions/18309362/c-sharp-emulator-to-many-connections or http://stackoverflow.com/questions/18318458/c-sharp-emulator-error-says-to-many-connections – I4V Aug 27 '13 at 13:32

2 Answers2

0

It's good practice to place the code accessing your database within a using clause. By doing this you will ensure that your connections are disposed of when it is not used anymore.

Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96
  • is there any advice you can give to do this? – Asan Smith Aug 27 '13 at 13:32
  • I would suggest the following – Andre Lombaard Aug 27 '13 at 13:37
  • As they say there is 9 ways to skin a cat, I'm not sure of the layout and design of your project. I prefer to create a separate data access layer project using entity framework and the repository pattern to handle data access to the database. Within your individual repository classes you can place all your database code within using statements. If you post an example of the classes containing the code accessing the database we can perhaps help you with an example. – Andre Lombaard Aug 27 '13 at 13:42
0

C# Too many connections in MySQL

Take a look at that link. You need to use the 'using' statement so the connections are opened and closed properly.

Community
  • 1
  • 1
Jon La Marr
  • 1,358
  • 11
  • 14