Suppose we need to do some long-time computing after loading some objects from SQLAlchemy session, we want to close the database connection during the computing, then reconnect to the database. How to do it in SQLAlchemy?
Or, is it necessary to close the session and re-create one every time, and merge/reload all related objects? But I found session.close() doesn't close the connection, even session.connection().close() or close the connection bound to the session doesn't close it either. So how to close the connection, after I used a session and before I quit the application?
Got the answer from the SQLAlchemy Google Group: use session.bind.dispose() after terminating the on-going transaction. It disposes the whole connection pool, so all database connections are closed.
And I can hold the session, e.g. commit or rollback the session instead of closing it. When I need to use it later, it'll automatically make necessary connections. This method saves me from recreating the session and merging objects.
Related
I'm creating a web application that uses SQLAlchemy to connect to a MySQL database. This may be a newbie question, but when is the proper time to call Engine.connect()? Should I be:
Calling Engine.connect() every time a web request is made, and closing the connection once the request is processed? OR
Calling Engine.connect() several times in the very beginning of my application to create several connections. Then every time I process a request, I use one of the connections, and never close them?
I'm also not exactly sure I understand the concept of a Connection Pool in general. Does the connection pool maintain a series of open connections to the database, and then hand a connection off whenever Engine.connect() is called? Or, does the pool establish a new connection every time Engine.connect() is called? What happens when Connection.close() is called?
I want to write a function for mysql connection to use it any where.
there is tow way in my mind
1)in top of function open connection and execute query and close connection. in this way each query open connection and close it but have easy usage because no need to handle open and close connection in separate function and call them in top and bottom of code
2)write a function for opening connection and another function for closing it and call them in top an bottom of code. in this way one connection open and use for multiple query then close
my question is:
is there any difference to open and close the db connection multiple times or it doesn't matter?
which way have a better performance(in second way connection may open some minutes)?
I use some programming language like java, php and ... .but my question is general
Each time you open and close a connection you are using resources. Imagine you have 100 requests per second. Each time you perform 2 operations. Open and close a connection. The response time increase.
Is better to use pool Connections. So, you have 5 open connections waiting for querys. When the connection is not in use, the connection returns to the pool and wait for another request.
Or you can try a persist connection. Both have pros and cons.
But never, never ever open and close connections for each request. Look this response
MySQL - Persistent connection vs connection pooling
i think there will be not so much difference but it will be better if you close your connection frequently so mysql connection limit will not be increase and if you keep connection open, So there will be chances to cross your connection limit.
Hi am confused with sql servers session. What does it actually mean? Does it keep track of the client like httpSession? I have read some documents on query life cycle. None talks about the sesion. Most of the documents say that after the query is recived by the server it gets parsed and then maintains a syntax tree and then execution plan and then executes the query and then a dispatch palan and then dispatches the resultset to the client who issued the query on the server. In the whole story where does the session on sql server like mysql server fits in and what actually it does? or There is no session concept on Mysql server(any sql server)? am i in wrong imagination?
A session in this context usually just refers to a single client connection.
The client connects to the DB server and authenticates; this is the start of the session.
When the client disconnects (gracefully or not) the session ends.
This is relevant for things like temporary tables or transactions: Un-committed transactions will be rolled back by the DBMS and all temporary tables created through this connection (=session) are discarded when the client disconnects, i.e. when the session ends.
Note that a client does not necessarily actively end a session or connection. The client may crash, or the network connection may break, or the server may shut down &c. Any of this implicitly terminates the session.
Problems may arise when a (client) application uses a connection pool keeping connections (and sessions) open and handing them out transparently to different application components. When not handled correctly, errors may occur because a given session may already be 'spoiled' by a previous operation. If, for example, one routine on the client creates a temporary table named 'X' and fails to explicitly drop it afterwards, the next routine that 'inherits' this session may encounter an error when trying to create another temporary table of that name, because it already exists in this specific session; which couldn't be the case if the connection/session was freshly created.
"Session" is mainly a generic term. You connect to a server (MySQL, Oracle, FTP, IRC... whatever), you do your stuff and finally disconnect when you're done. That has been a session.
HTTP is a particular case. It's a stateless protocol: if you spend an hour reading a web site, you don't remain connected for a whole hour. You make a quick connection, fetch an item at a time (an HTML document, a style sheet, a picture...) and close the connection. (Internals are actually more complex but that's the general idea.) When you ask for a second page, the server doesn't know who you are: that makes it impossible to keep track of your whole browsing session at protocol level. Thus HTTP sessions were invented: they're a way to emulate physical sessions.
The MySQL session starts when you open a connection to the server. A connection ID is assigned which can be read via the SELECT CONNECTION_ID() statement. The session is terminated when the connection is closed or, in case of persistent connections, after a certain timeout or when the server shuts down.
If I use node-mysql's connection pooling feature, wouldn't this pose a problem in my app because connections are re-used after connection.end()? Here's why I'm concerned: When you define a variable in SQL, or start a transaction, the variable or transaction is persistent until the connection is closed. Since the connection is never actually closed, but instead re-used, the variables and transactions can seep into another routine that doesn't expect the variable or expect the transaction to exist; it expects a fresh connection.
Variables could pose a big problem; a routine could never safely assume a variable to be undefined because the connection with wear and tear.
Transactions could pose an even bigger issue if one routine were to ever fail to rollback or commit a transaction before calling end(). If the next routine that were to use this connection doesn't deal with transactions, then all queries would be appended to the transaction and halted, never to be executed. I could just be careful when writing my app that such an error never occurs, but mistakes happen, and if it does happen, it'd be extremely difficult to debug (I wouldn't know which routine is mishandling connections, bad bad).
So these are some of my concerns when thinking about using pooling. Surely I'm not the only person to have thought of these issues, so please shed some light on how to properly handle pooled connections, if you'd be so kind. :)
Thank you very much.
All transactions will happen in the context of a single connection. After you end or terminate the connection, you can no longer operate on that transaction. The general pattern is to open, query, close and unless you have a long running transaction, you won't have any problems of queries or variables bleeding over into other connections.
In the event of a long running transaction, you will have to manage that connection object to make sure it exists and is only used by code that operates within that transaction.
I have program that constantly query a mysql server. Each time I access the server I made a connection then query it. I wonder if I can actually save time by reuse the same connection and only reconnect when the connection is closed. Given that I can fit many of my queries within the duration of connection timeout and my program has only one thread.
yes - this is a good idea.
remember to use the timeout so you don't leave connections open permanently.
also, remember to close it when the program exits. (even after exceptions)
Yes by all means, re-use the connection!
If you are also doing updates/delete/inserts through that connection make sure you commit (or rollback) you transactions properly so that once you are "done" with the connection, it is left in a clean state.
Another option would be to use a connection pooler.
Yes, you should reuse the connection, within reason. Don't leave a connection open indefinitely, but you can batch your queries together so that you get everything done, and then close it immediately afterwards.
Leaving a connection open too long means that under high traffic you might hit the maximum number of possible connections to your server.
Reconnecting often is just slow, causes a lot of unnecessary chatter, and is simply a waste.
Instead, you should look into using mysql_pconnect function which will create persistent connection to the database. You can read about it here:
http://php.net/manual/en/function.mysql-pconnect.php