3 Replies Latest reply on Nov 3, 2009 7:03 AM by Jan Westphal

    Some beginner's questions

    Jan Westphal Newbie

      Hello,


      I am trying to start with seam and in general, it works fine, but some issues seem weird to me, and I do not find very much information about it, so I ask here.


      1.


      The EJB PersistenceContext comes in two flavours, standard and extended. How does the seam-managed persistence context behave? Is the connection to the database kept open during the whole conversation or lifecycle of the component the entityManager is injected into?


      2.


      In a session EJB (doesn't matter which scope, it never works) there's a method annotated with @Create. I tried to do some database initialization in such a method, but something like


      MyObject obj=new MyObject();
      entityManager.persist();



      doesn't work, and I have no idea why, nor how to solve the problem another way. What I need is an application scoped bean annotated with @Startup with a @Create method, so some things happen at the very beginning of the application, which works, but writing something into the database at this point doesn't work.


      3.


      Am I right asssuming that injecting a EJB session bean into another EJB session bean with @In doesn't work? But when using simple POJOs, injecting one into anther works?


      4.


      Is it generally a bad idea not to use EJBs? For the application I am working on, no EJB features such as clustering are necessary. Can I stick with simple POJOs for start?


      Thanks
      Jan


        • 1. Re: Some beginner's questions
          Leo van den berg Master

          Hi,


          1. The persistence manager (i presume you're using JPA) manages the connection to the database in a very efficient way, meaning that , although open during the convesration, the database connection is only used when necessary.


          2. To properly use EJB's in Seam, they must be intercepted. Are you sure you enabled that by using the appropiate annotations on the bean or define the in ejb-jar.xml ? If you're not sure inject soemthing simple such as the logger and print a line. If it gets you nothing or a NPE seam is not properly configured.


          3. You can baiscally inject anything into the Seam-managed beans, but the beans should be managed by Seam. However the question is if that useful if you're not using that bean in other places, reference them in the normal way.


          4. I am very happy mixing up EJB's and normal beans. I normally define re-usable services as EJB (in case I don't want to use Seam), but annotate them to make the useful for Seam, besides an additional dependency at compile time, Seam-annotated ejb'scan be used without any problem in other projects.


          Leo

          • 2. Re: Some beginner's questions
            Shervin Asgari Master

            Jan Westphal wrote on Nov 01, 2009 10:23:


            Hello,

            2.

            In a session EJB (doesn't matter which scope, it never works) there's a method annotated with @Create. I tried to do some database initialization in such a method, but something like

            MyObject obj=new MyObject();
            entityManager.persist();



            doesn't work, and I have no idea why, nor how to solve the problem another way. What I need is an application scoped bean annotated with @Startup with a @Create method, so some things happen at the very beginning of the application, which works, but writing something into the database at this point doesn't work.


            Try using @PostConstruct on EJB. That will work regardless if it is a seam component.
            I also need more code here. I cannot know what is wrong in your example code. For instance is MyObject an entity bean? Are you automatically flushing? There might also be other reasons why writing to the database doesnt work on startup. Maybe the persistence context is not yet initialized. To come around this problem, what I do is create an application scoped component that I ensure only run once, then I put an observer on that method and listen on the seam loggedIn event. Then I do my stuff there. I have experienced the Startup to not fully work how I want it, so it is a good workaround.


            @Observer("org.jboss.seam.security.loginSuccessful") public void doSomething(){}






            3.

            Am I right asssuming that injecting a EJB session bean into another EJB session bean with @In doesn't work? But when using simple POJOs, injecting one into anther works?

            4.

            Is it generally a bad idea not to use EJBs? For the application I am working on, no EJB features such as clustering are necessary. Can I stick with simple POJOs for start?

            Thanks
            Jan




            No that is not correct. You can inject it without problem. Just remember that you must ensure the names are correct.


            ie:



            interface foo
            @Name("foo") 
            class fooImpl implements foo
            @In(create=true) bar bar;
            
            interface bar
            @Name("bar)
            class barImpl implements bar
            @In(create = true) Foo foo;



            • 3. Re: Some beginner's questions
              Jan Westphal Newbie

              Thanks a lot. I got everything working.


              The key to understanding the behaviour of the seam managed persistence context is that seam starts a new short-running conversation for every request, so withing such, the entityManager behaves like EJB TRANSACTIONAL. When the conversation is propagated to a long runnning, the entityManager rather behaves like EJB EXTENDED. Quite clever ;).