Max Ischenko said:
>
> Hi,
>
> I have a question regarding usage of db connection in WebKit and
> (related to it), how thread-safe servlets are.
>
> Currently I have a db conn. as a module global variable in a sitepage
> and it is used by several servlets. But I'm not sure this is a good way
> to deal with db. Anyway it is probably buggy as most DB-API interfaces
> are not thread-safe and WebKit is multithreaded and hence there is a
> possiblilty for crash due to simulationous access to the same conn from
> multiple threads.
>
> I think db conn should be created when needed and released when
> unneeded, as to conserve system resources. But was is the best way to
> achieve this in WebKit?
>
> I think about using a conn pool and storing a conn in a session variable
>  or in a servlet instance variable (via awake/sleep). Only not sure
> whether it would be guaranteed that it can't be accessed from mulitiple
> threads.
>
> If session variable or servlet instance variable can be accessed from
> mulitiple threads then I see no other route as to request conn only
> where it needed (in a method body) and release it immediately.
>
> What do you think?
>

How you can safely use a database connection in a multi-threaded
environment depends on the threadsafety of the DB API module that you are
using.  Some modules are only thread safe if each thread has exclusive use
of its own connection (threadsafety of 1).  Other modules allow multiple
threads to use the same connection simultaneously (threadsafety >= 2). 
The module you are using should have a "threadsafety" or "threadlevel"
attribute that contains the level of thread safety it supports.

The modules that I have experience with (MySQLdb and mx.ODBC.DB2) have
only had a threadsafety of 1.  In order to guarantee that each thread has
exclusive use of a connection, it must either create and close the
connection itself, or get a connection from a pool that guarantees that
the connection will not be "handed out" to another thread while the first
thread is using it.

The DBPool.py module in the MiscUtils package of Webware provides a simple
thread-safe pooling implementation that works for DB API modules with a
threadsafety of 1 or greater.  However, several people (including myself)
have found it lacking in features.

I recently added an alternative to DBPool.py to the Webware Sandbox:

http://cvs.sf.net/viewcvs.py/webware-sandbox/Sandbox/wsmith323/DbConnectionPool.py

It provides the same basic functionality for threadsafety = 1 modules as
DBPool.py, but has additional features that require it to be completely
different in the way it implements that functionality internally.  Have a
look at the doc string in the module for more details.

-- 
Warren Smith
[EMAIL PROTECTED]




-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to