How to handle too many SQL Connections? - mysql

I’m trying to develop a server for a mobile game which should be able to withstand 1000 simultaneous connections. The Server is connected to a MySQL Database and each time I am looking to access the database I enclose my SQLConnection Cmd and Reader with a using statement so that they are all opened briefly and closed afterwards:
using (MySqlConnection SQLToUse = CreateSQLConnection()) {
MySqlCommand cmd = new MySqlCommand(query, SQLToUse);
...
}
I had problems with readers which is why I had to do this.
Theoretically, if say 1000 users were to open SQL Connections simultaneously, the maximum amount of SQL connections (151 to my knowledge) would stop me from creating any more connections and throw an error. This hasn’t happened yet because the highest number of users the server ever had at once was 10, but i am asking how to prevent it.
Thank you for reading this all the way through and for your help in advance

What you need to do is to create a service part that keep the connection(s) open and then the other part of the game make requests to this part. (sort of how you use a library)
I would also suggest you add a stack and each time a query is done it can check the stack for another query to run. Each time something is added to the stack, check if there is any connections available, if there are activate a connection that will pick up the new line in the stack.
Very hard to describe the code with no information on what languages is used but this should get you in the right direction.

Related

Best technique to make node mysql run fastest?

I am using this
var mysql = require('mysql');
in my node.js app. I am interested to make my app perform the fastest. I have many functions that connect to SQL. There is 2 approaches I am familiar with
For every request, I make a new connection and then execute the query and the close the connection.
Open the connection and make it a global variable, and then never close it. Then for every request that comes in, it just uses the opened connection saved globally.
Which is generally better to use? Also for number 2, if the server closes unexpectedly, then the sql connection doesn't close. Is that bad?
Thanks
Approach 2 is faster, but to avoid the potential problem of connections dropping without unexpectedly, you'll have to implement testing mechanism for every segment that queries the database (ex: count the number of returned rows).
To take this approach further, you can define connections bank or pool. Where you can deal with connection testing and distributions. The basic idea is to have many connections to the database and just inject healthy connections to consumers (functions, or objects that query the database). As Andrew mentions in the comments You can check this question: node.js + mysql connection pooling
Since the database is an essential asset to a project, if this is not a homework or learning project, it might not be a bad idea to explore 3rd party libraries, where a lot of the connections and security details is covered and automated.

How to implement a connection pool?

I'm using civetweb as a (websocket) server. I have some handlers for when I receive data, that will query mysql. These will spawn as a thread, every time there is a request.
Until now I was only using one mysql connection with the database, which I setup on the start off the program, in combination with the mongoose library. But with the threaded requests, it's causing me headaches, since mysql isn't thread-safe from the time you do mysql_select() until mysql_store_result(). I have tried putting a mutex around these mysql functions, but then perfomce drops a tenfold (from ~750 requests/second to ~75 requests/second).
What is the correct way to handle this? I've heard about a 'connection pool', but it's hard to find some simple examples with google (or wrap my head around a sane implementation).
It seems unlikely I'm the first person to encounter such a problem :).
I'm not sure if this will help you. I'd put it in a comment but I don't have enough reputation yet.
I had the same problem in VB.NET when I added multi threading to my application. To correct it there I made sure to call connection.open before all of my queries and added "pooling=true;" to the end of my mysql Connection String. Mysql will determine if it needs to open a connection or using existing one.

Connecting and disconnecting from Mysql continuously with Excel

Newbie question....sorry
I have a simple mysql database running in our Intranet (windows server) which >20 people connect to for searching/inserting records, etc
This is done with a simple Excel GUI.
Process is:
Search Strings are typed in excel cells
VBA opens connection to Mysql and query is run
Results retrieved are put on excel Connection to
mysql closed with VBA
The above process takes in general 0-2 seconds. Records retrieved <100.
Everthing runs fine so far.
In order to be able to connect more people in future, I would like to have some feedback on whether it is ok to continously connect and disconnect from mysql in the way I am doing.
Can it cause some type of crash/memory leaks, etc ??
Is there some better way to do this?
I am hoping to get <2000 users, but I understand the more users connected, worse it is.
By disconnecting after each search/insert, I am hoping to keep the number of live connections as low as possible.
thanks for your input
This constant connecting and disconnecting is an expensive process.
A better way would be to use server-side scripting to manage your connections. This way you would have a single persistent connection to each server, and the users will execute their queries through the single connection. You will also need to implement some sort of job queue for execution.

Constant MySQL Connection or Connect when needed

I am building a little daemon which periodically (every 30 seconds) checks for new data and enters it in a local MySQL Database.
I was just wondering whether it was better to create a connection to the database when the application launches and always use that connection throughout the application until it is closed, or if it should only open a connection when there is new data, close it after the data has been added and then repeat this when there is new data 30 seconds later?
Thank you.
I would recommend that you do whatever you find easiest to code. Don't waste any time trying to solve what will most likely be a non-problem.
If it turns out there is any difficulty with contention, connection limits or other such things you can fix it later.
That depends.
In your case the performance won't matter, since you won't be performing thousands of queries/logins per second and the new connection/login overhead is in (tens of) milliseconds.
If you use a single connection, you have to make sure your daemon handles sudden disconnections from the MySQL side and is able to recover from there. Also if you ever move your application so your application would be on a different server than the MySQL, then many firewalls can drop prolonged connections every now and then.
If you create a new connection every time and then disconnect when finished, things like firewalls cleaning up old connections won't bite you so easily.

NHibernate mysql connection not closed?

I am using NHibernate with mySQL 5 and I am a bit unsure whether NHibernate really closes the connection to mySQL.
This is the code I am using in order to save a Player entity to the database:
using (ISession session = SessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(player);
transaction.Commit();
}
}
After running this piece of code, I select the current connection count from the mySQL server using
show status like 'Conn%' ;
and the value is increased every time by 1!
So, inserting the player 10 times (=starting the program 10 times) increases the connection count by 10. But the session.IsClosed property of the NHibernate Session is false.
Do I have to do anything else in order to release the NHibernate resources and close the connection? Are the connections pooled / timed out?
It would be great if anyone could give me a hint.
I'm pretty sure the Connection is being closed. The dispose method on the ISession does just that. So it's unlikely it remains open when exiting the using block.
Also , from what I know the connection is usually opened only when writing/reading to the database . The session may be open, but that doesn't mean the connection is also open.
That, plus the fact that in the mysql documentation, it says that the keyword Connections means :
The number of connection attempts (successful or not) to the MySQL server.
and doesn't say anything about the current state of the connection (open or not), makes me believe that the connection is closed.
PS: you should be using Threads_connected to see open connections.
There is a bug in the MySQL.Data dll in certain versions that was causing this same problem for me. Make sure you update your Connector/NET to the most recent. This solved the problem for me.