Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@PostConstruct called multiple time for @ConversationScoped bean

I have a @ConversationScoped bean, with a start method, like so:

@PostConstruct
public void start() {
    if (conversation.isTransient()) {
        conversation.begin();
        log.debug("conversation.getId(): " + conversation.getId());
    }
}

My problem is that every time the page is refreshed a new conversation is started, a new conversation is also started every time I have an AJAX call to a method in the bean (which is my main problem).

What I really want to happen is for the sam conversation to hang around until I manually call conversation.end(). What am I missing here?

like image 211
Simon Avatar asked Dec 07 '22 21:12

Simon


2 Answers

Slightly off-topic, but hopefully valuable:

I'm not 100% sure that @PostConstruct is the right place to start a conversation. I'd rather use a faces-event like this:

<f:metadata>
        <f:event type="javax.faces.event.PreRenderViewEvent"
                listener="#{myBean.init}" />
</f:metadata>

and start the conversation if you are sure that you are not in a JSF-postback request.

public void init() {
       if (!FacesContext.getCurrentInstance().isPostback() && conversation.isTransient()) {
          conversation.begin();
       }
    }

If you use Seam 3, it's even easier:

<f:metadata>
   <s:viewAction action="#{myBean.init}" if="#{conversation.transient}" />
</f:metadata>
like image 155
Jan Groth Avatar answered Dec 10 '22 13:12

Jan Groth


Did you checked that the (AJAX) calls include the conversation ID parameter (cid)?

If that's missing, a new conversation is expected to start for each call.

like image 33
Arjan Tijms Avatar answered Dec 10 '22 11:12

Arjan Tijms