Parameters are thread local, and from reading the docs of virtual-connection 
and connection-pool, I think they create new threads to handle the connections. 
These threads are probably created before you parameterize, and thus see the 
original values.

Try replacing the virtual-connection stuff with a single call to do-connect, 
and see if that works. (Obviously not the real fix, but may help identify the 
error).

On Apr 24, 2017, 4:35 PM -0400, David Storrs <david.sto...@gmail.com>, wrote:
> I'm finding parameters confusing and was hoping someone could explain them 
> for me.  The following is a simplified version of our production code; I'm 
> expecting it to fail but it does not.  What am I not understanding?
>
> The sequence goes:
>
> *) db.conf creates and provides some parameters (db username, db password, db 
> name, db port) and a function for creating database connections using those 
> parameters
>
> *) dbstuff.rkt requires db.conf, re-provides the parameters from db.conf, and 
> also provides a wrapper around the db connector from db.conf
>
> *) bar.rkt parses the command line, uses that command line data to set the 
> parameters that were imported from db.conf by way of dbstuff.rkt and makes a 
> DB call
>
> *) I run bar.rkt from the command line with an invalid username for the 
> database, expecting it to fail to connect, but it works fine???
>
>
>
> My first guess was that since I was using the parameters as default values 
> perhaps they were being evaluated at compile time instead of runtime.  I 
> tried changing the default value to #f and then doing (or u (db-user)) in the 
> body of the function, but that had no effect.
>
> I am utterly baffled at this point.  Any help would be much appreciated.
>
>
>
> ################################# file: "db.conf"
> #lang racket
>
> (require db)
>
> (define db-user     (make-parameter "postgres"))
> (define db-name     (make-parameter "test-db"))
> (define db-password (make-parameter "mellon"))
> (define db-port-num (make-parameter 5433))
>
> (define (do-connect #:u [u (db-user)]
>                     #:d [d (db-name)]
>                     #:p [p (db-password)]
>                     #:prt [prt (db-port-num)])
>   (println (~a "In db.conf db-user: " (db-user)))
>   (println (~a "db-name: " (db-name)))
>   (println (~a "db-password: " (db-password)))
>   (println (~a "db-port: " (db-port-num)))
>
>   (postgresql-connect #:user     u
>                       #:database d
>                       #:password p
>                       #:port     prt
>                       ))
>
> (define dbh (virtual-connection
>              (connection-pool
>               do-connect)))
>
> (define (make-connect)
>   dbh)
>
> (provide (all-defined-out))
>
>
> ################################# file: "dbstuff.rkt"
> #lang racket
>
> (require db)
> (require "db.conf")
>
> (define/contract (dbh)
>   (-> connection?)
>   (make-connect))
>
> (provide (all-defined-out)
>          (all-from-out db)
>          (all-from-out "db.conf"))
>
>
> ################################# file: "bar.rkt"
> #lang racket
>
> (require "dbstuff.rkt")
>
> (command-line
>  #:program "db demo"
>  #:once-each
>  [("--db-user") dbu "Username for database handles"
>   (db-user dbu)]
>  )
>
> (println (~a "In bar: db user: " (db-user)))
> (query-value (dbh) "select 'this should not have worked'")
>
>
> ################################# command line:
>
> $ racket bar.rkt --db-user invalid_user
> "In bar: db user: invalid_user"
> "In db.conf db-user: postgres"
> "db-name: test-db"
> "db-password: mellon"
> "db-port: 5433"
> "this should not have worked"
>
>
>
>
> --
> 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.

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