Grails的Webflow的 - 让事情*出*流量范围
-
18-09-2019 - |
题
我失去了一些东西......
我有一个Grails Webflow的,看起来像这样: -
def childFlow = {
start {
action {
def targets = []
Target.list().each {target ->
targets.add(new TargetCommand(name: target.name, id: target.id))
}
log.debug "targets are $targets"
[children: targets]
}
on('success').to('selectChild')
}
...
TargetCommand是可序列化。但我得到这个错误: -
Caused by: java.io.NotSerializableException: com.nerderg.groupie.donate.Target
出于某种原因,“目标”对象,它是Target.list(内侧)。每个{}封盖得到投入流动范围,我不能找出如何将其标记为瞬态
我有在具有放置在流范围中的对象服务的一些代码的时候,不希望他们太
如何停止正在实施的封锁局部瞬时变量在流动范围是什么?
解决方案
精炼上面的回答,而不是清除persistenceContext我们简单地退出该实例作为我们与他们完成,像这样:
Target.list().each {
targets.add(new TargetCommand(name: it.name, id: it.id))
flow.persistenceContext.evict(it)
}
这仍然是一个变通为不能够标记闭合变量作为瞬态
其他提示
在回答我的问题是:
流对象是一个包含对“persistenceContext”,这是一个org.hibernate.impl.SessionImpl因此流程试图存储整个会话,即使对象未改变(上下文我想的基准的地图)
本不正确从Grails的1.1.x版本的文档例子给了我们一个线索做什么:
processPurchaseOrder {
action {
def a = flow.address
def p = flow.person
def pd = flow.paymentDetails
def cartItems = flow.cartItems
flow.clear()
def o = new Order(person:p, shippingAddress:a, paymentDetails:pd)
o.invoiceNumber = new Random().nextInt(9999999) cartItems.each { o.addToItems(it) }
o.save()
[order:o] }
on("error").to "confirmPurchase"
on(Exception).to "confirmPurchase"
on("success").to "displayInvoice"
}
在flow.clear()清除整个流图包括persistenceContext或会话,然后使整个流会由于缺乏一会话。
所以中间“溶液”是使用persistenceContext在这种情况下清除它。所以这个工程: -
def childFlow = {
start {
action {
sponsorService.updateTargetsFromTaggedContent()
def targets = []
Target.list().each {
targets.add(new TargetCommand(name: it.name, id: it.id))
}
flow.persistenceContext.clear()
[children: targets]
}
on('success').to('selectChild')
on(Exception).to 'finish'
}
与此明显的问题是,这次会议被完全清除,而不是仅仅保持了事情,我不想在流。
有一个更好的方式想要的,在这里是一个广义的解决方案,将删除流的persistenceContext任何非可序列化的对象。这可以被服务的方法给出的流程: -
def remove = []
flow.persistenceContext.getPersistenceContext().getEntitiesByKey().values().each { entity ->
if(!entity instanceof Serializable){
remove.add(entity)
}
}
remove.each {flow.persistenceContext.evict(it)}
如果你像我一样需要也许驱逐所有你喜欢做的事
flow.persistenceContext.flush()
flow.persistenceContext.persistenceContext.clear()
不隶属于 StackOverflow