title: JSF - CDI instancing again a session bean
body: <p>The source code:</p>
  
  <pre><code>public class ReportGenerator implements Serializable {
  
      private static final long serialVersionUID = -3995091296520157208L;
      @Inject
      private ReportCacheSession reportCacheSession;
      @Inject
      private UserSessionBean userSessionBean;
      @Inject
      private Instance&lt;ReportBuilder&gt; reportBuilderInstance;
  
      public static final int BUILD_ERROR = 0;
      public static final int BUILD_OK = 1;
      public static final int BUILD_NOPAGES = 2;
  
      private ReportBuilder reportBuilder = null;
  
      private FileData build(String jasperName, Map&lt;String, Object&gt; params, String extension, boolean guardarCache, boolean inline) {
          FileData fd = null;
  
          reportBuilder = reportBuilderInstance.get();
          if (reportBuilder != null) {
              reportBuilder.jasperName = jasperName;
              reportBuilder.emailName = SevUtils.getEmailName(userSessionBean.getUserInfo().getEmail());
              reportBuilder.sessionId = JSFUtils.getSessionId();
              reportBuilder.params = params;
              reportBuilder.extension = extension;
              //reportBuilder.config(jasperName, SevUtils.getEmailName(userSessionBean.getUserInfo().getEmail()), JSFUtils.getSessionId(), params, extension);
              reportBuilder.start();
  
              try {
                  reportBuilder.join();
              } catch (InterruptedException ex) {
                  Logger.getLogger(ReportGenerator.class.getName()).log(Level.SEVERE, null, ex);
              }
  
              fd = reportBuilder.getFileData();
          }
  
          if (fd != null &amp;&amp; fd.getState() == BUILD_OK) {
              fd.setInline(inline);
  
              if (guardarCache) {
                  reportCacheSession.addReport(fd);
              }
          }
  
          return fd;
      }
  }
  </code></pre>
  
  <p>reportBuilder.start(); is a new Thread to generate the report(s), the problem is when the line reportCacheSession.addReport(fd); is called CDI create a new instance each time, but ReportCacheSession is a session bean annotated with javax.inject.Named and javax.enterprise.context.SessionScoped.</p>
  
  <p>I don't know why this happens, but my solution is add a new line, like this:</p>
  
  <pre><code>FileData fd = null;
  reportCacheSession.toString(); //NEW LINE
  reportBuilder = reportBuilderInstance.get();
  </code></pre>
  
  <p>reportCacheSession.toString(); create the instance of ReportCacheSession before my thread is called and all works OK...</p>
  
  <p>How the new thread affects to CDI? Why CDI created a new instance of my session bean when I called the thread before?</p>
  
  <p><strong>UPDATE 08/15/12:</strong></p>
  
  <p>Ok, I have changed  my code to use the EJB annotation @Asynchronous, in this case I have problem when I'm generating a large PDF report (the XLS report works without problem), the file's size is incomplete(less bytes) and when I try to open it this appear in blank... Maybe a problem/bug with JRExporter#exportReport method...</p>
  
  <p><strong>LAST UPDATE:</strong></p>
  
  <p>Ok, the report generation was my mistake... the question is <strong>which alternative is best to use EJB Asynchronous or JMS?</strong> Thanks to all, each comment have led me to find a good solution...</p>