13 Replies Latest reply on May 29, 2012 7:59 PM by fastroller

    Seam persistence flush mode

    fastroller

      I was just looking through the seam persistence documentation but could find nothing about how to set the flush mode to manual. I see from browsing the code that there is an application scoped FlushModeManager. Should I inject this into an application scoped bean and set the flush mode to manual at startup? I can see from the code comments that it is not considered a finished piece of work. It would be nice to configure this with an annotation (around the EntityManagerFactory?). Does anyone have an official word on the matter?

       

      Just on a different track, if one is not using long running conversations then is there any difference in annotating the EntityManagerFactor with either @ConversationScoped or @RequestScoped? When a conversation is not long running it really becomes a pseudo-request scope. I've been using RequestScope for a while without issues, but perhaps it is not recommended.

        • 1. Re: Seam persistence flush mode
          lightguard

          Andrew Wheeler wrote:

           

          I was just looking through the seam persistence documentation but could find nothing about how to set the flush mode to manual. I see from browsing the code that there is an application scoped FlushModeManager. Should I inject this into an application scoped bean and set the flush mode to manual at startup? I can see from the code comments that it is not considered a finished piece of work. It would be nice to configure this with an annotation (around the EntityManagerFactory?). Does anyone have an official word on the matter?

          Yes, that works.

           

           

          Just on a different track, if one is not using long running conversations then is there any difference in annotating the EntityManagerFactor with either @ConversationScoped or @RequestScoped? When a conversation is not long running it really becomes a pseudo-request scope. I've been using RequestScope for a while without issues, but perhaps it is not recommended.

          No, there's no difference. If you're not using long running conversations there's also no difference in using @PersistenceContext and letting the container give you an entitymanager. This can be used on beans that are not EJBs in EE6.

          • 2. Re: Seam persistence flush mode
            fastroller

            I ran into a small problem with seam faces which prevents auto instantiation of application scoped beans with the error:

             

            Use of @javax.faces.bean.ManagedBean is forbidden. Please use @javax.inject.Named instead

             

            The bean is annotated with @ManagedBean(eager=true). As this is disabled by seam faces is there an equivalent alternative? For the moment I have my login page access the application bean (getting some arbitrary property) which initialises the bean and calls the post-construct where the flush mode is set.

             

            Setting flush mode to manual in this way does work.

            • 3. Re: Seam persistence flush mode
              lightguard

              In CDI all beans are created when they're accessed and live until their scope dies, there's no equivilant because simply by using @Named you get that anyway.

              • 4. Re: Seam persistence flush mode
                fastroller

                Thanks Jason, but what I was trying to point out is that the JSF spec does allow eager created beans. An application bean annotated with @ManagedBean(eager=true) will be created when the application starts not when first referenced. This annotation has been disabled by seam-faces so I have to reference it on all of the pages a user might first visit my site. It would be nice if seam-faces could implement this feature if it is going to disable the @ManagedBean attribute.

                • 5. Re: Seam persistence flush mode
                  lightguard

                  If what you want is a bean that's created when the application starts then use @ApplicationScoped.

                  • 6. Re: Seam persistence flush mode
                    hantsy

                    What the differnece between @ApplicationScoped and @Singleton and EJB @Singleton...a bit confused about these, is there any guide to use these annoations?

                    I am alsways try to use CDI ones.

                    • 7. Re: Seam persistence flush mode
                      lightguard

                      hantsy bai wrote:

                       

                      What the differnece between @ApplicationScoped and @Singleton and EJB @Singleton...a bit confused about these, is there any guide to use these annoations?

                      I am alsways try to use CDI ones.

                      There is no CDI @Singleton annotation. Not sure what you're referring to here.

                      • 8. Re: Seam persistence flush mode
                        hantsy

                        I refered the javax.inject.Singleton  which is from jsr 330  and is extracted from jsr 299 before JEE 6 was published...I would use the word "CDI" for jsr330 and jsr299...and weld also implemented the two specs.

                        • 9. Re: Seam persistence flush mode
                          lightguard

                          I've not used it, nor really seen it used. Also, if you want to continue the conversation, I suggest opening a different thread, this one is already answered, and the current topic is vastly different than the OP.

                          • 10. Re: Seam persistence flush mode
                            fastroller

                            As I said before the @ApplicationScoped bean does not get instantiated when the application starts up. All CDI beans are only created when first accessed. This means that to get the flush mode set before any meaningful work happens I must do the following:

                             

                            1) Define application bean:

                             

                            @Named("myApplication")

                            @ApplicationScoped

                            public class Application {

                                      private String name="myApplication";

                                      private String version="0.3";

                             

                                      @Inject

                                      private Logger log;

                             

                                      @Inject

                                      private FlushModeManager flushModeManager;

                             

                                      @PostConstruct

                                      public void initialise() {

                                                log.info("Setting flush mode to MANUAL");

                                                flushModeManager.setFlushModeType(FlushModeType.MANUAL);

                                      }

                             

                             

                                      public String getName() {

                                                return name;

                                      }

                                      public void setName(String applicationName) {

                                                this.name = applicationName;

                                      }

                             

                             

                                      public String getVersion() {

                                                return version;

                                      }

                             

                             

                                      public void setVersion(String applicationVersion) {

                                                this.version = applicationVersion;

                                      }

                            }

                             

                            On my login page do something like:

                             

                            <h1>Login to #{myApplication.name} version #{myApplication.version}</h1>

                             

                            If the first page I visit is "about us" then no log message is printed from the @PostConstruct. If I visit the login page then the message is printed. JSF can create these beans eargerly, but seam faces disables the annotation. Fair enough too as the @ManagedBean annotation is a bit of a waste of space. Perhaps this should be addressed by CDI.

                            • 11. Re: Seam persistence flush mode
                              lightguard

                              I think right now the best solution is to use @javax.ejb.Singleton and @javax.ejb.Startup

                              • 12. Re: Seam persistence flush mode
                                hantsy

                                The solution in the booking example...

                                 

                                @Alternative

                                public class ApplicationInitializer {

                                    private static final Logger log=LoggerFactory.getLogger(ApplicationInitializer.class);

                                    @Inject

                                    private EntityManager entityManager;

                                 

                                    @Inject

                                    private UserTransaction utx;

                                 

                                //    @Inject

                                //    private org.jboss.seam.logging.Logger log;

                                 

                                 

                                    public ApplicationInitializer() {

                                      

                                    }

                                 

                                    /**

                                     * Import seed data when Seam Servlet fires an event notifying observers that the web application is being initialized.

                                     */

                                    public void importData(@Observes @Initialized WebApplication webapp) {

                                        log.info("Importing seed data for application " + webapp.getName());

                                      

                                    }

                                1 of 1 people found this helpful
                                • 13. Re: Seam persistence flush mode
                                  fastroller

                                  Thanks Hantsy, that solution works perfectly. I modified my application bean (above) with the event listener and it works. Note to others: You don't need the @Alternative annotation.