What happens in the following code?

(define dbh (postgresql-connect ...))

;; Use the DBH in a new thread
(thread (thunk
  (while ...some long-running condition...
    (sleep 1) ; don't flood the DB
    (query-exec dbh "insert into users ..."))))

;; And in the main thread
(query-exec dbh ...)

I now have a database object that's being shared between two threads, yes?
Or is the object copied when the new thread is created and, if so, what
will that do to the underlying connection to the DB?

New scenario:

(define dbh (postgresql-connect ...))
(define current-dbh (make-parameter dbh))
(query-value (current-dbh) "select ...")
(sleep 1000000)
(query-value (current-dbh) "select ...")

I would expect the first query-value to work but by the time the second
runs the handle has probably been disconnected by the database and the
query will throw an exception.  Am I understanding that properly?

Final scenario, identical to the previous except that the parameter is a
(define dbh (postgresql-connect ...))
(define current-dbh (make-parameter (delay dbh)))
(query-value (force (current-dbh)) "select ...")
(sleep 1000000)
(query-value (force (current-dbh)) "select ...")

Would this make any difference?  I can't see why but thought I should ask.

Obviously I can prevent these issues by using a connection pool or handle
generator function.  I'm simply curious about the underlying functionality.

You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to