We use sqlalchemy 0.4.6 with elixir 0.5.2 and zope.

Below is some excerpt to show how we currently create/manage one
engine instance per zope thread.

>From what I understand from reading the documentation, the
'connection' and 'transaction' objects themselves are not thread safe
- but the engine instance when bound to metadata can manage
connections from a pool and as long as we 'close' (release back to
pool) within each thread then it may be safe.

We want to try to limit our application from consu,ing too many
database conections.
So can somebody confirm whether it's prudent to change our approach
and share the engine instance among multiple threads?

thanks
Aj


+++++++++++++++++++++++
# global declarations
__session__ =
scoped_session(sessionmaker(twophase=False,transactional=True,autoflush=True))
__metadata__ = ThreadLocalMetaData()

_tld = threading.local()

# Manage one engine instance per thread
if not hasattr(thread_local_data,'txm_orm_engine_dict'):
                # THREAD LOCAL ATTR DOESN'T EXIST. CREATE IT.
                thread_local_data.txm_orm_engine_dict = dict()
                engine = sqlalchemy.create_engine(dsn,echo=True)

                # UPDATE DICTIONARY
                thread_local_data.txm_orm_engine_dict[dsn] = engine
else:
                # ALREADY THREAD LOCAL ENGINE DICTIONARY.
                if thread_local_data.txm_orm_engine_dict.has_key(dsn):
                        # ALREADY AN ENGINE FOR THIS DSN. USE IT
                        engine =
thread_local_data.txm_orm_engine_dict[dsn]

                else:
                        # NO ENGINE FOR THIS DSN. CREATE IT
                        engine =
sqlalchemy.create_engine(dsn,echo=True)
                        # UPDATE DICTIONARY
                        thread_local_data.txm_orm_engine_dict[dsn] =
engine


__metadata__.bind = engine


-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to