2

I'm using primefaces 5.1.7 with wildfly and have a Problem when multiple pictures are uploaded at the same time.

I have multiple instances of a ViewScoped bean which are created (tested using breakpoints in @PostConstruct)

xhtml:

<h:form enctype="multipart/form-data" prependId="true">
    <p:fileUpload fileUploadListener="#{myBean.handleFileUpload}" 
    mode="advanced" auto="true" update="list"
    allowTypes="/(\.|\/)(gif|jpe?g|png)$/" multiple="true"/>
    ...

bean:

import javax.faces.view.ViewScoped;

@Named
@ViewScoped
public class MyBean implements Serializable {
   ...
   public void handleFileUpload(FileUploadEvent event) {
   ...
   }
}

This happens about 75% of the time but sometimes everything is just fine. Any idea how i can avoid those multiple instances?

jobe
  • 325
  • 2
  • 14
  • 25
  • What does the import for viewscoped look like? – Aksel Willgert Jan 13 '15 at 08:08
  • import javax.faces.view.ViewScoped; – jobe Jan 13 '15 at 09:41
  • I think you're mixing the scopes, you should use either CDI or faces.. this answer explains well: http://stackoverflow.com/a/14384824/833031 – Hatem Alimam Jan 15 '15 at 09:12
  • @HatemAlimam - javax.faces.view.ViewScoped is working with CDI from JEE7, so with Wildfly I suppose the version are the right one. – Guaido79 Jan 15 '15 at 09:14
  • @user1667910 From JavaDoc: Please verify also that any beans stored in view scope must be serializable and proxyable as defined in the CDI specification http://docs.oracle.com/javaee/7/api/javax/faces/view/ViewScoped.html – Guaido79 Jan 15 '15 at 09:14
  • @Guaido79 I missed Wildfly :/ yes in JSF 2.2 there is a CDI compatible view scope. – Hatem Alimam Jan 15 '15 at 09:16
  • @user1667910 Is there any component binding? For upload component of Primefaces I am used to use a RequestScope Bean, because, frequently, inside the bean there is a reference to a Stream that is not serializable. – Guaido79 Jan 15 '15 at 09:22
  • I still think that mixing CDI and JSF scopes is causing the @PostConstruct to be called multiple times somehow! They're reporting that this bug is fixed but I'm not sure which is exactly your Mojarra version ? – Hatem Alimam Jan 15 '15 at 09:25
  • @Hatem Alimam Mojarra Mojarra 2.2.8-jbossorg-1. – jobe Jan 15 '15 at 12:48
  • @Guaido80 what do you mean with "a stream that is not serializable"? My Bean and all its instance variables must be serializable? I could create a new Request scoped bean, the problem is that i want to store the files in a List inside my Bean before to save them in the DB – jobe Jan 15 '15 at 13:14
  • Do you have any other referents to the bean in the page ? – Hatem Alimam Jan 15 '15 at 13:50
  • @Hatem Alimam yes, in datatables, textfields, ... why? – jobe Jan 15 '15 at 13:56
  • I have the same application server with the same @Named @ViewScoped etc.. everything works fine! the only case where I had reproduced your case is where I had nothing but the `actionListener` of the upload, in that case the PostConstruct was called for each file upload. in other words if I included `#{myBean.getDummyText}` and then uploaded it will work, but if the bean is never initilized then the case would be present. Based on your comment you have datatables which are calling the lists(I suppose) from the same bean then it should be fine ! other than that I couldn't reproduce the problem! – Hatem Alimam Jan 15 '15 at 14:06
  • As is said, it doesn't happen all the time. and why do you think serialization is important here? – jobe Jan 16 '15 at 07:01

1 Answers1

0

a possible solution... (not tested!!):

Your Session scoped CDI Bean:

...
import javax.enterprise.context.SessionScoped
...

@SessionScoped
public class MySession implements Serializable {

    public void doSomeWork(...) {
        ...
    }

}

And your Conversation scoped CDI Bean:

...
import javax.inject.Named
import javax.enterprise.context.ConversationScoped
...

@Named
@ConversationScoped
public class MyBean implements Serializable {

    @Inject
    private Conversation conversation;

    @Inject
    private MySession mySession;

    @PostConstruct
    public void init() {
        conversation.begin();
    }

    public void handleFileUpload(FileUploadEvent event) {
        ...
        mySession.doSomeWork(...);
        ...
    }

    public void remove() {
        conversation.end();
    }

}

And your Controller:

@Named
public class MyController implements Serializable {

    @Inject
    private MyBean myBean;

    public String doSomethingAfterAllUploadsAreFinished() {
        ...

        myBean.remove();

        return "SUCCESS";
    }

}
StefanHeimberg
  • 1,455
  • 13
  • 22
  • Hello. Unfortunately, i Need Information contained in my session scoped bean. – jobe Jan 22 '15 at 09:05
  • in your session scoped bean; do you use the javax.enterprise.context.SessionScoped or the javax.faces.bean.SessionScoped annotation? with my example you should use the javax.enterprise.context.SessionScoped annotation... then it should work... --> i have updated the example in my answer below – StefanHeimberg Jan 22 '15 at 09:59
  • Its viewscoped. import javax.faces.view.ViewScoped; – jobe Jan 27 '15 at 14:56