Fork me on GitHub
#sql
<
2022-12-20
>
Michael Gardner21:12:14

Is there a way to set session variables with next.jdbc's on-connection ?

Michael Gardner21:12:55

or with get-connection

seancorfield21:12:13

Can you clarify what you mean by "session variables"? Isn't a "session" just the lifetime of the Connection?

Michael Gardner21:12:54

in Postgres these would be like what you get with SET foo=...

seancorfield21:12:21

Have you tried just call execute! with that statement on the Connection?

Michael Gardner21:12:24

of course I can execute such a statement at the start of a connection separately, but I would like to avoid the extra round-trip if possible

Michael Gardner21:12:41

since this would happen with every single connection in the design I'm considering

seancorfield21:12:53

It's up to the driver whether it makes one or multiple round-trips for multiple statements (`;`-separated) in a single execute() call I think so I don't know that you can guarantee avoiding a round-trip for setting a variable?

Michael Gardner21:12:19

I think you can set it via the URL, but I'm not sure how to do that when passing a source with :dbname etc https://stackoverflow.com/questions/59060477/can-i-set-session-variables-for-postgres-from-the-jdbc-connection

seancorfield21:12:37

Just pass it as an extra key in the db-spec map.

seancorfield21:12:37

Anything in the db-spec hash map that isn't a core attribute to create the basic JDBC URL is added as part of the "query string".

Michael Gardner21:12:53

interesting, I'll try that. If that doesn't work out, I had considered jamming the SET statements at the beginning of my normal SQL statement, but when I do that with jdbc/execute! I always get [#:next.jdbc{:update-count 0}] as a return value, even for SELECTs

seancorfield21:12:05

This part of the docs talks about extra keys in the db-spec https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.3.847/doc/all-the-options (although it talks about being passed via Properties in the JDBC get connection call so it may not be obvious that they end up in the JDBC URL).

Michael Gardner21:12:54

ah, I need :multi-rs

seancorfield21:12:55

Re: "jamming ... statements at the beginning" -- not all drivers support multiple statements in a single execute() call. Some accept them if you provide some per-connection setting.

seancorfield21:12:27

Re :multi-rs -- see the various caveats in the docs about that.

seancorfield21:12:46

(databases are all such "special" individuals, unfortunately)

Michael Gardner21:12:06

luckily I only care about Postgres

Michael Gardner21:12:28

for :multi-rs, I don't see any caveats in the docs for execute!. Am I looking in the wrong place?

Michael Gardner22:12:05

looks like I need to pass :options "-c rls.user=foo" in the dbspec to get the equivalent of SET rls.user='foo' in Postgres, for future reference

seancorfield22:12:17

Interesting. I'm kind of shocked that works with a space in it 🙂 Re: :multi-rs there are some DB-specific caveats on the Tips & Tricks page but not for PostgreSQL it seems.

Michael Gardner22:12:26

thanks for all the help. I really appreciate how responsive you are!

seancorfield22:12:30

I try to respond quickly if I'm actually around (California business hours mostly) 🙂