• Question: Should I have a constant SQL connection, or only connect, query, disconnect(Optimization)?
    9 replies, posted
Its just a simple question, would it be easier if I made the SQL(Mysqli) in an include file, and "keep" it open, or should I simply connect when its needed, query, and disconnect? Which one would be better?
Opening and disconnecting might be more reliable and easier to do. You can have also concurrent (multiple) connections at same time AFAIK.
If I can recall correctly, .net maintains a persistent connection(s). Then passes a connection from a pool to the requesting code as needed. Don't quote me on that, but keeping a persistent connection seems fine. from php.net mysqli documentation: [quote=http://php.net/manual/en/mysqli.persistconns.php] The idea behind persistent connections is that a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times. This reduces the overhead of creating fresh connections every time one is required, as unused connections are cached and ready to be reused. [/quote]
Huh, now my other question is: Wont it spam like, connection to the database server? since it 'only' can have 200 open connections, what if (just as example) over 200 users or so connect?
I doubt it's max 200 connections. Also persistent has less overhead.
[QUOTE=Fourier;49905646]I doubt it's max 200 connections. Also persistent has less overhead.[/QUOTE] tbh, on first mysql-server install on the vps, it was 10, I had to put it to 200.
[QUOTE=Topgamer7;49903760]If I can recall correctly, .net maintains a persistent connection(s). Then passes a connection from a pool to the requesting code as needed. Don't quote me on that, but keeping a persistent connection seems fine. from php.net mysqli documentation:[/QUOTE] OP is not talking about mysqli's persistent connections - which persist until the PHP process is exited or a script (or server) manually closes the connection. OP is asking whether you should open and close a connection whenever you need to execute a query in one of your application's scripts, or have a globally included script open one (and keep it until the end of the script). I'm pretty sure most PHP apps do the latter, and it seems like the right decision to me. You can also open the connection lazily. Write a wrapper function for the query function in which you check whether you already have a database connection, and open one if not. That way you'll only ever establish a database connection when you actually need to query the database, but it'll persist throughout the rest of the script's runtime.
how would I end the connection manually then? lets say I include the mysql shit, run some scripts in the INCLUDING file, run a query, and then close it? works it that way or..? Sorry, I never really worked with SQL in PHP, but gLua.
[QUOTE=Topgamer7;49903760]If I can recall correctly, .net maintains a persistent connection(s). Then passes a connection from a pool to the requesting code as needed. Don't quote me on that, but keeping a persistent connection seems fine. from php.net mysqli documentation:[/QUOTE] Persistent connections is an HTTP spec thing, not a language/framework specific thing.
Sorry, you need to Log In to post a reply to this thread.