On Nov 26, 2003, at 3:23 AM, Max Ischenko wrote:
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.

Sessions can be accessed from multiple threads, though usually that would only happen when a browser was downloading dependent files (e.g., images), that were also served off WebKit. But you still don't want to put these in the session, because they aren't pickleable, and the session persists over restarts by using pickle.


You should either be able to use DBPool (or something like it), or do some other simple resource sharing. Lists work better than you might think:

_storage = []
def getResource():
    try:
        return _storage.pop()
    except IndexError:
        return makeNewResource()
def returnResource(rsrc):
    _storage.append(rsrc)

.append and .pop are threadsafe (enough) so it's pretty simple to implement.

--
Ian Bicking | [EMAIL PROTECTED] | http://blog.ianbicking.org



-------------------------------------------------------
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