How to close database connection of a session?

9,292 views
Skip to first unread message

Gary Shi

unread,
Apr 5, 2011, 1:47:42 AM4/5/11
to sqlalchemy
I found session.close() doesn't close the database connection (by
calling session.close and sleep, then check the network connections),
neither session.connection().close(). How to close the connection
without quit my application?

León

unread,
Apr 5, 2011, 8:14:09 AM4/5/11
to sqlalchemy
Hi,
I'm using "session.bind.dispose()"

León

Gary Shi

unread,
Apr 5, 2011, 8:45:20 AM4/5/11
to León Domingo, sqlal...@googlegroups.com
Wow this is cool, it not only works, but also tells me some inner logic.

Because I found it's no use dispose the bind's connection pool before I closed the session,
but it really works after the session is closed. This implies to me the connection is still in use,
so I tried to commit or rollback the session before dispose, it also worked.

Then I found I could hold the session, and use that later. This saves me recreate the session
and merge those objects, seems very useful to me. Thank you!

On Tue, Apr 5, 2011 at 8:12 PM, León Domingo <leon.d...@ender.es> wrote:
Hi,
I use "session.bind.dispose()"

León Domingo

ender
LA FACTORÍA DE SOFTWARE

Av. Cerro del Águila 7, 2ª planta - S23
28703 S.S. de los Reyes
Madrid

tlf. y fax: 902 01 44 01
www.ender.es




--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.





--
regards,
Gary Shi

Michael Bayer

unread,
Apr 5, 2011, 9:25:47 AM4/5/11
to sqlal...@googlegroups.com
if you don't want connections to be pooled, instead closed immediately, send "poolclass=NullPool" to create_engine().

docs: http://www.sqlalchemy.org/docs/core/pooling.html#switching-pool-implementations

Bozhidar Cherkezov

unread,
May 17, 2013, 7:59:29 AM5/17/13
to sqlal...@googlegroups.com, tengou...@gmail.com
Is session.bind.dispose() the same as Engine.dispose()?

I'm asking that because the docs says "A new connection pool is created immediately after the old one has been disposed", so you are destroying all connections in the pool to destroy one? 
I'm asking that because I need to close the current session connection after a current requests end, and destroying the pool for every request doesn't look right.

Michael Bayer

unread,
May 19, 2013, 8:27:46 PM5/19/13
to sqlal...@googlegroups.com, tengou...@gmail.com
On May 17, 2013, at 7:59 AM, Bozhidar Cherkezov <bozhi...@gmail.com> wrote:

Is session.bind.dispose() the same as Engine.dispose()?

I'm asking that because the docs says "A new connection pool is created immediately after the old one has been disposed", so you are destroying all connections in the pool to destroy one? 
I'm asking that because I need to close the current session connection after a current requests end, and destroying the pool for every request doesn't look right.

If you have a Session, it will release all the connection resources it has when you say session.close().   Where "release" means, it will tell it's connection to "close", which may mean that the connection is just returned to the connection pool for reuse, or may mean that the actual network connection is closed, depending on configuration.   See http://docs.sqlalchemy.org/en/rel_0_8/glossary.html#term-release.

If you want the actual network connection to the database to be gone when the Session is closed, it indicates you don't want to do any connection pooling.  So you'd use NullPool for that, engine = create_engine("...", poolclass=NullPool), see http://docs.sqlalchemy.org/en/rel_0_8/core/pooling.html#switching-pool-implementations .

The dispose() method of Engine is generally used to get rid the pool used by an Engine entirely, and isn't something that is called unless some special event has occurred, like the database was restarted, or within some testing scenarios. 



On Tuesday, April 5, 2011 3:14:09 PM UTC+3, León wrote:
Hi,
I'm using "session.bind.dispose()"

León

On 5 abr, 07:47, Gary Shi <gary...@gmail.com> wrote:
> I found session.close() doesn't close the database connection (by
> calling session.close and sleep, then check the network connections),
> neither session.connection().close(). How to close the connection
> without quit my application?

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

To post to this group, send email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages