且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用clojure.java.jdbc从MySQL流式传输

更新时间:2022-06-15 22:35:26

此答案是针对postgresql而不是MySQL,但应同时应用于两者.

This answer refers to postgresql instead of MySQL, but should apply to both.

用(clojure.java.jdbc/transaction)包装with-query-results函数,所以:

Wrap your with-query-results function with (clojure.java.jdbc/transaction), so:

(let [query (query-only (fetch-all big-table))]
  (clojure.java.jdbc/with-connection (get-connection (:db query))
    (clojure.java.jdbc/transaction        
      (clojure.java.jdbc/with-query-results rows
        (into [{:fetch-size Integer/MIN_VALUE
                :concurrency :read-only
                :result-type :forward-only} (:sql-str query)]
              (:params query))
        (throw (Exception. (str "retrieved a row: " (pr-str (first rows))))))))))

PostgreSQL文档为启用流式传输:连接不得处于自动提交模式."默认情况下,连接是在自动提交启用的情况下创建的,但是使用(clojure.java.jdbc/transaction)进行换行将在自动提交关闭的情况下运行内部代码.您也可以自己在连接上调用.setAutoCommit.

The postgresql docs specify one more requirement for enabling streaming: "The Connection must not be in autocommit mode." By default the connection is created with autocommit on, but wrapping with (clojure.java.jdbc/transaction) will run the inner code with autocommit off. You could also call .setAutoCommit on the connection yourself.