Fork me on GitHub
#sql
<
2022-12-07
>
Travis18:12:41

Hi guys, attempting to do a batch delete by keys with jdbc.next to avoid a lot of round tripping to the DB?

seancorfield18:12:06

Did you look at next.jdbc/execute-batch! ? Do you have a specific question about how to use it?

Travis18:12:32

So i am using it for inserts but it doesn't seem to work for delete

Travis18:12:47

i will show you what i am doing maybe I am doing something dumb in this context

Travis18:12:13

(jdbc/execute-batch! db "delete from table where key1 = ? and key2 = ?"
                         data
                         {:rewriteBatchInserts true})

Travis18:12:42

[[key key2]]

Travis18:12:44

for the data

Travis18:12:41

the connection/component

Travis18:12:20

Can't infer the SQL type to use for an instance of clojure.lang.PersistentVector. Use setObject() with an explicit Types value to specify the type to use.

Travis18:12:10

@U04V70XH6 IGNORE Me! I got it working. I forgot to source the code that I changed into the batch statement, lol

seancorfield18:12:29

One thing to watch out for: `rewriteBatchInserts` reWriteBatchedInserts needs to be on the JDBC connection string (URL) so if you are using a pooled datasource, you need to specify it when that is created, not on operations that use those connections.

Travis18:12:49

oh, good to know!

seancorfield18:12:05

And I think it's actually reWriteBatchedInserts for PostgreSQL, looking at some search results (I do not use PG so the docs are based on what people have told me)

seancorfield18:12:26

Ah, yes, the docs are correct: :rewriteBatchedStatements for MySQL and :reWriteBatchedInserts for PostgreSQL... so it may not matter for PG since you're not doing inserts -- but execute-batch! should still help for those delete statements.

Travis18:12:34

cool, i will make the adjustment to add it to the connection pool

Travis18:12:00

@U04V70XH6 would the rewriteBatchInserts go under

:datasourceProperties
for the connection pool ? Using the Hikari pool

seancorfield18:12:24

How exactly are you creating the connection pool? Are you using the next.jdbc.connection/->pool or component calls?

Travis18:12:57

(defrecord Database [db-spec     ; configuration
                     init-fn     ; callback to initialize the database
                     datasource] ; state

  component/Lifecycle
  (start [this]
    (log/info "Starting Database of type " (:dbtype db-spec))     
    (if datasource
      this ; already initialized
      (let [database (assoc this :datasource (connection/->pool HikariDataSource db-spec))]
        ;; set up database if necessary
        (log/info "testing db connection")
        (.close (jdbc/get-connection (:datasource database))) ;;ensure connection and datasource setup correctly
        (log/info "Connection pool established")
        (when init-fn
          (log/info "Initializing DB") 
          (init-fn database))
        (log/info "Database started")
        database)))
  (stop [this]
    (assoc this :datasource nil))

Travis18:12:46

if thats the spot i would just add it to that key on the

db-spec

seancorfield18:12:37

Yup, that would be the place. You know that next.jdbc.connection/component will build that for you? https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.3.847/api/next.jdbc.connection#component

seancorfield19:12:16

It doesn't have the init-fn tho'... that's an interesting idea... I could add that as an option to n.j.c/component 🙂

Travis19:12:23

I think modeled that after your user polylith example so I can't take credit for that, lol.

Travis18:12:05

Not sure if it can be done or not. My JDBC is rusty