Threads and clsql

JTK <[email protected]> Mon, 12 Dec 2011 14:46:27 -1000
Newsgroups gmane.lisp.clsql.general
Message-ID <[email protected]>
Hello,

I'm a bit confused about thread safety in clsql.

I believe(d) that



1. when doing (UPDATE-INSTANCE-FROM-RECORDS  VIEW-CLASS-OBJECT :DATABASE SOME-DATABASE) then SOME-DATABASE  is ignored  if the VIEW-DATABASE slot in VIEW-CLASS-OBJECT is a working database connection.  This means that it is unsafe to pass CLSQL objects among threads.  One solution is to null-out this field before passing an object around, then then use (WITH-DATABASE … )  when updating. I believe this from the existence of this project: http://common-lisp.net/project/clsql-fluid/

2. when doing (WITH-DATABASE DB CONNECTION-SPEC :DATABASE-TYPE … :IF-EXISTS  :ERROR) clsql will safely reuse connections among threads.



However, I'm having doubts about point 2, because of a SBCL+MYSQL+CLSQL memory corruption crash I can prevent by locking database accesses between threads.

Is it necessary to do (WITH-DATABASE DB CONNECTION-SPEC :DATABASE-TYPE … :IF-EXISTS  :NEW) to be safe?

Evidence for this latter belief is a 4 year old posting here: http://paste.lisp.org/display/39787  where 'Victor' tries to impose thread safety on clsql by hashing on the current thread.

Once I get this figured out, should I submit a patch for the CLSQL manual, like a brief section explaining the limits of CLSQL's thread safety?  

Or would it be useful to put the thread of the creation thread into the database object (and pooling) and catch it when connections cross threads, like Victor did?

Or maybe I really have no idea what is happening and all of the above is wrong ...

Thanks for any tips, 

John Klein