shutdown-agents
takes up to one minute to complete (assuming no agents are running an action).
This is due to the way java.util.concurrent
cached thread pools work.
题
The following program, when run from an überjar, exits at the end only when using the in-memory Datomic database; when connecting to the Datomic server, it hangs indefinitely rather than exiting the JVM:
(ns myns.example
(:use [datomic.api :only [db q] :as d])
(:gen-class))
;; WORKS: (def uri "datomic:mem://testdb")
(def uri "datomic:free://localhost:4334/testdb2")
(defn -main []
(println 1)
(when (d/create-database uri)
(d/connect uri))
(shutdown-agents)
(println 2))
Run as:
lein uberjar && java -cp target/myns-0.1.0-SNAPSHOT-standalone.jar myns.example
Outputs:
1
2
and hangs. It only hangs if the DB doesn't exist when the program starts.
Anyone know why, or how to fix? This is with both datomic-free-0.8.4020.26
and datomic-free-0.8.3941
.
UPDATE -- the above program does actually terminate, but it takes a very long time (> 1 minute). I'd like to know why.
解决方案
shutdown-agents
takes up to one minute to complete (assuming no agents are running an action).
This is due to the way java.util.concurrent
cached thread pools work.
其他提示
shutdown
function
Usage: (shutdown shutdown-clojure)
Shut down all peer resources. This method should be called as part of clean shutdown of a JVM process. Will release all Connections, and, if shutdown-clojure is true, will release Clojure resources. Programs written in Clojure can set shutdown-clojure to false if they manage Clojure resources (e.g. agents) outside of Datomic; programs written in other JVM languages should typically set shutdown-clojure to true.
Added in Datomic Clojure version 0.8.3861
(ns myns.example
(:require [datomic.api :as d])
(:gen-class))
(def uri "datomic:free://localhost:4334/testdb2")
(defn -main []
(d/create-database uri)
(let [conn (d/connect uri)]
(try
;; do something
(finally (d/shutdown true)))