Re: Threads and clsql

Russ Tyndall <[email protected]> Tue, 13 Dec 2011 10:09:34 -0500
Newsgroups gmane.lisp.clsql.general
Message-ID <[email protected]>
Hello,

I wrote a blog post that touches on many of these facts and presents a 
solution located at:
http://russ.unwashedmeme.com/blog/?p=218

However, I don't believe that this will address your second point.  
IF-EXISTS :error should always throw an error if it finds an existing 
connection to the database.   Thus I don't see how using it would cause 
thread safety problems (though I could certainly be missing something).  
Likewise :if-exists :new will always create a new connection (and should 
thus prevent any thread safety problems).

The only thing I see missing in this descriptions is if you have :POOL T 
in the connection spec then it will entirely ignore your :IF-EXISTS 
specification and instead ask the pool to give it a connection.  If the 
connection that was returned from the pool is still saved on some 
database object (that is possibly being operated on in a separate 
thread), that could cause the issues you are seeing.

Perhaps this helps?
Russ Tyndall
Acceleration.net


On 12/12/2011 7:46 PM, JTK wrote:
> 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
> _______________________________________________
> CLSQL mailing list
> [email protected]
> http://lists.b9.com/cgi-bin/mailman/listinfo/clsql