On Oct 16, 9:39 am, "Heston James - Cold Beans"
<[EMAIL PROTECTED]> wrote:
> Afternoon Guys,
>
> I have a suspicion that I'm leaving MySQL database connections open when I
> shouldn't be and I'm trying to understand how they are managed by
> SQLAlchemy.
>
> I currently create an engine instance and bind my session maker too it like
> this:
>
>             # Create the engine to the database.
>
>             engine = create_engine(connection_string, echo=False)
>
>             # Connect the session.
>
>             Session = sessionmaker(bind=engine)
>
> I then create sessions around my application by using:
>
>             # Create a new session.
>
>             session = Session()
>
> and once finished with it closing the session like this:
>
>             # Close the session.
>
>             session.close()
>
> When are new connections established to the database when using this method?
> And when are they closed again? The only reason I ask is that I've seen a
> few errors recently on high load instance of my application which struggle
> to connect to the database, I'm also seeing a few table corruptions and I
> think they're all related issues from me perhaps creating too many
> connections.


the engine is using a connection pool which by default will keep five
connections opened persistently.  It also has an "overflow" of 10
additional connections which are opened on an as-needed basis and
closed after usage.  At the point of 15 connections in use, the pool
throttles additional requests until a connection is available.  This
is of course all entirely configurable and the docs explain how to do
this in detail.

Each of your Session instances will procure a connection from the pool
when they are first used (such as, issuing a query).  They then hold
on to that connection persistently, which is considered to be the
active transaction, until you rollback(), commit(), or close() the
session, or the session is garbage collected.  The "persistent
transaction" behavior of session is known in the 0.4 series as
"transactional" and in the 0.5 series as "autocommit=False".  If you
flip this flag, then the Session only pulls connections from the pool
for each individual statement execution and/or flush(), and returns it
immediately afterwards.





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

Reply via email to