sql

All things SQL and JDBC...
Ronny Li 2021-01-15T14:03:44.007400Z

Hi everyone, I'm using the following https://www.reddit.com/r/Clojure/comments/dyaqh6/connecting_to_snowflake_db_with_nextjdbc/f88k70a for connecting to Snowflake with next.jdbc. My question is: Is there a better way to create and close the connection per query? The reddit thread appears to only create the connection.

seancorfield 2021-01-15T17:31:06.010Z

That is super-complicated and absolutely not needed:

(def db-spec {:dbtype "snowflake" :classname "net.snowflake.client.jdbc.SnowflakeDriver" :user "..." :password "..." :host (str account-name ".<http://snowflakecomputing.com|snowflakecomputing.com>")})
(def ds (jdbc/get-datasource db-spec))
(jdbc/execute! ds ["some sql here"])
^ that ought to work @ronny463

seancorfield 2021-01-15T17:31:36.010600Z

LMK if the above does work and then I can go respond to that thread...

Ronny Li 2021-01-15T17:33:53.012800Z

Thank you @seancorfield! Your approach looks much simpler :) Unfortunately that thread is 1 year old so I think it's locked to future replies. It's also unfortunate that it's literally the first result when googling "next.jdbc Snowflake" 😛

Ronny Li 2021-01-15T17:35:58.013Z

And just to check, is it okay for me to repeatedly use the same ds to execute queries or should I be creating a new one each time?

seancorfield 2021-01-15T17:37:12.013300Z

Per the next.jdbc docs, you should make a datasource (which could be a plain datasource, one built from a db-spec via jdbc/get-datasource, or a connection pooled datasource) and then use the datasource everywhere.

seancorfield 2021-01-15T17:37:51.013500Z

next.jdbc gets a Connection from the DataSource as needed and closes it out after use.

seancorfield 2021-01-15T17:38:22.014100Z

grumbles about Reddit not being a very good place to ask technical questions like that

😄 2
Ronny Li 2021-01-15T18:56:35.014400Z

got it, thank you!

Ronny Li 2021-01-15T20:10:28.014600Z

Hi Sean, just confirming that this worked perfectly. Thanks again!

seancorfield 2021-01-15T21:38:10.014800Z

Thanks @ronny463!