我正在尝试模拟交易中的冲突。我还没有理解如何做到这一点,但我遇到了错误,
我使用clojure
和任务运行gradle
。
clojure
代码看起来像这样
(ns com.lapots.functional.clojure.script
(:gen-class))
(def value "V0")
(def tx-value (ref value))
(def thread-1
(future
(dosync
(println "tx-value (thread-1) =>" @tx-value)
(ref-set tx-value "V1")))) ; V1`
(def thread-2
(future
(dosync
(println "tx-value (thread-2) =>" @tx-value)
(ref-set tx-value "V2"))))
(Thread/sleep 2000)
(ref-set tx-value "V3"))))
(println @tx-value)
@thread-2 ; start first as waiting
@thread-1
(println @tx-value)
gradle
文件看起来像这样
apply plugin: 'clojure-min'
clojure {
aotCompile = true
warnOnReflection = true
}
repositories {
clojarsRepo()
}
dependencies {
compile "org.clojure:clojure:$clojureVersion"
}
task runClojure(dependsOn: classes, type: JavaExec) {
main = 'com.lapots.functional.clojure.script'
classpath = sourceSets.main.runtimeClasspath
}
但是我收到了这个错误
tx-value (thread-1) => V0
tx-value (thread-2) => V1
Exception in thread "main" java.lang.IllegalStateException: No transaction running, compiling:(script.clj:19:13)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3657)
at clojure.lang.Compiler.compile1(Compiler.java:7474)
at clojure.lang.Compiler.compile(Compiler.java:7541)
at clojure.lang.RT.compile(RT.java:406)
at clojure.lang.RT.load(RT.java:451)
at clojure.lang.RT.load(RT.java:419)
at clojure.core$load$fn__5677.invoke(core.clj:5893)
at clojure.core$load.invokeStatic(core.clj:5892)
at clojure.core$load.doInvoke(core.clj:5876)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.core$load_one.invokeStatic(core.clj:5697)
at clojure.core$compile$fn__5682.invoke(core.clj:5903)
at clojure.core$compile.invokeStatic(core.clj:5903)
at clojure.core$compile.invoke(core.clj:5895)
at clojuresque.tasks.compile$eval41$main_task_driver__43$fn__47.invoke(compile.clj:16)
at clojuresque.tasks.compile$eval41$main_task_driver__43.invoke(compile.clj:13)
at clojuresque.tasks.compile$eval41$main__56.invoke(compile.clj:5)
at clojure.lang.AFn.applyToHelper(AFn.java:152)
at clojure.lang.AFn.applyTo(AFn.java:144)
at clojure.core$apply.invokeStatic(core.clj:646)
at clojure.core$apply.invoke(core.clj:641)
at clojure.lang.Var.invoke(Var.java:383)
at clojuresque.Driver.main(Driver.java:49)
Caused by: java.lang.IllegalStateException: No transaction running
at clojure.lang.LockingTransaction.getEx(LockingTransaction.java:208)
at clojure.lang.Ref.set(Ref.java:162)
at clojure.core$ref_set.invokeStatic(core.clj:2352)
at clojure.core$ref_set.invoke(core.clj:2346)
at clojure.lang.AFn.applyToHelper(AFn.java:156)
at clojure.lang.AFn.applyTo(AFn.java:144)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3652)
... 22 more
有什么问题?
答案 0 :(得分:1)
def
thread-2
dosync
有非常无意义的parens,所以在你认为自己之前就关闭了(def thread-2
(future
(dosync
(println "tx-value (thread-2) =>" @tx-value)
(ref-set tx-value "V2")
(Thread/sleep 2000)
(ref-set tx-value "V3"))))
块。
Your order no. 000000045 has been successfully placed.<br/><br/>Thank you for shopping at our store!