Grails Webflow - Storing Things * Outside * Stream Volume

Something is missing for me ....

I have a Grails web stream that looks like this: -

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 is serializable. but I get this error: -

Caused by: java.io.NotSerializableException: com.nerderg.groupie.donate.Target

For some reason, the "target" object, which is inside the Target.list () object. Each {} closure falls into the flow region, and I cannot figure out how to designate it as transient.

I have code in a service that has objects placed in the stream area when I don't want them either.

How to stop local transition variables in closure that are placed in the stream area?

+3
source share
4 answers

persistenceContext , :

    Target.list().each {
        targets.add(new TargetCommand(name: it.name, id: it.id))
        flow.persistenceContext.evict(it)
    }

- ,

+3

:

- , "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'
        }

, , , .

+2

for a better way, here is a generic solution that removes any non-serializable objects from the persistenceContext stream. This may be a flow-based maintenance method: -

def remove = []
flow.persistenceContext.getPersistenceContext().getEntitiesByKey().values().each { entity ->
    if(!entity instanceof Serializable){
        remove.add(entity)
    }
}
remove.each {flow.persistenceContext.evict(it)}
0
source

If I like, you should evict everyone, maybe you like to do

flow.persistenceContext.flush()
flow.persistenceContext.persistenceContext.clear()
0
source

Source: https://habr.com/ru/post/1722188/


All Articles