1 2 3 4 Previous Next 59 Replies Latest reply on Oct 15, 2012 6:43 AM by chrisharris Go to original post
      • 45. Re: How do I get a JPA EntityManager reference in Weld?
        sboscarine

        Arbi Sookazian wrote on Nov 23, 2009 18:50:

        I think you've asked this how can I use Tomcat with Weld question in this thread several times.  It's a good question for obvious reasons but maybe you should give up temporarily with the Tomcat/Weld route.


        For those just joining us...


        The JBoss team has just started a persistence module as part of Seam3.  The code posted now works with the addition of the META-INF/services/javax.enterprise.inject.spi.Extension file.  It'll be in their repo soon, so please be patient.  I'll be posting it tonight if  a JBoss team member hasn't beaten me to it.  Hopefully someone will add a link when it's ready...very soon.


        Regarding my persistence (no pun intended), Arbi, I'll bet you a beer (redeemable in Boston only), that many users besides me will be interested in JSF 2.0, find their way into CDI, and want to actually use it...and that most of them will have easier access to Tomcat 6+ than JBoss 5.  I am asking as much for the next user as I am asking for myself. 

        • 46. Re: How do I get a JPA EntityManager reference in Weld?
          nickarls

          And any users that can be won over with extensions is free...

          • 47. Re: How do I get a JPA EntityManager reference in Weld?
            asookazian

            Steven Boscarine wrote on Nov 23, 2009 23:33:



            Arbi Sookazian wrote on Nov 23, 2009 18:50:

            I think you've asked this how can I use Tomcat with Weld question in this thread several times.  It's a good question for obvious reasons but maybe you should give up temporarily with the Tomcat/Weld route.


            Regarding my persistence (no pun intended), Arbi, I'll bet you a beer (redeemable in Boston only), that many users besides me will be interested in JSF 2.0, find their way into CDI, and want to actually use it...and that most of them will have easier access to Tomcat 6+ than JBoss 5.  I am asking as much for the next user as I am asking for myself. 


            The beer must be Sam Adams followed by Natural Ice as chaser (my favs after years of [ab]use).  Anyways, if you're referring to corporate developers who typically have very minimal choice in what app server (or in a lot of cases, which platform, e.g. .NET or JEE, they develop on) they use, then yes, I'd imagine there are many Tomcat production deployments in the Java space.  Keep in mind, however, that both Tomcat and JBoss are free to download (community editions, at least).  So in terms of experimenting with JEE 6 and CDI, it's more than likely than JBoss 5/6 will be preferred download over Tomcat 6.


            But I may be totally wrong, there are lots of Spring/Tomcat/Hibernate users already out there who are comfortable with Tomcat and will want to stick with it when using CDI.  But also keep in mind that the servlet container that ships with JBoss is........Tomcat.


            And if you're only going to use CDI as a DI container, then it's possible users may end up with JSF 2.0, RF 4.0, Guice, JPA 2.0, etc. and may not even need to use CDI/Weld or any other implementation of 299...

            • 48. Re: How do I get a JPA EntityManager reference in Weld?
              asookazian

              btw, if you're not constrained by corporate standards to Tomcat, then what is the advantage(s), if any, of using Tomcat vs. JBoss 5/6 for JEE 6 development?  You're limiting yourself to WAR deployments, which in JEE 5 meant no EJBs, but I believe in EE 6 we can deploy JARs in WARs?  And what about clustering Tomcat instances with httpd and modjk/modcluster?  Is that more difficult than clustering JBoss instances?


              The so-called lighweight solution of Spring/Tomcat is how light when comparing with EE 6 web profile and add'l built-in services provided by CDI and the EJB container??

              • 49. Re: How do I get a JPA EntityManager reference in Weld?
                sboscarine

                Arbi, those are interesting questions that probably deserve there own thread. 


                The advantages and disadvantages don't matter because most of us don't get to pick the container.  The only reason I and many other people I know are considering CDI is because it can run in a servlet container. 


                Running something on our existing tomcat 6 cluster is nearly free.  Deploying a second container cluster is a six figure endeavor.  If I was paying my own salary, I'd have more options to choose from. 

                • 50. Re: How do I get a JPA EntityManager reference in Weld?
                  asookazian

                  Well, Spring runs in a servlet container as well.  I've used Spring recently at my current project.  I prefer Seam but the question becomes the following:


                  Most Java developers who are currently running Tomcat in production clusters are using Spring as part of their stack for a variety of reasons but mostly for DI and AOP as well as Spring services like tx mgmt, JdbcTemplate, HibernateDaoSupport, etc.


                  So of these projects running mostly Spring 2.x, why would they migrate towards a new stack using CDI/Weld?  Why not stick with Tomcat/Spring 2.x or upgrade to Spring 3 in the future?


                  So are you telling me your current stack is not Tomcat/Spring?  You have the option to start using Weld with Tomcat suddenly?

                  • 51. Re: How do I get a JPA EntityManager reference in Weld?
                    asookazian

                    Sorry, this is getting totally off the JPA integration topic....

                    • 52. Re: How do I get a JPA EntityManager reference in Weld?

                      Arbi Sookazian wrote on Nov 24, 2009 05:40:



                      The beer must be Sam Adams followed by Natural Ice as chaser (my favs after years of [ab]use).  Anyways, if you're referring to corporate developers who typically have very minimal choice in what app server (or in a lot of cases, which platform, e.g. .NET or JEE, they develop on) they use, then yes, I'd imagine there are many Tomcat production deployments in the Java space.


                      Yes, many many...



                      Keep in mind, however, that both Tomcat and JBoss are free to download (community editions, at least).  So in terms of experimenting with JEE 6 and CDI, it's more than likely than JBoss 5/6 will be preferred download over Tomcat 6.


                      Yes, we downloaded Tomcat, JBoss 4.x, JBoss 5.x, Glassfish and Geronimo... we also have some servers with OC4J... if OC4J were free and JDK6 compatible, since it is the only one with an included solution for classpath hell, and since JDeveloper makes hot-redeployment on it really fast, that would have been our only choice... but since we did not want to get stuck in JDK1.5 and paying more deployment licenses we endedup using Tomcat which, is the fastests and one with the best support for hot deployment from Eclipse.... and also the easies to manage (the only defect is that it does not have a shared libs classpath hell solution).



                      But I may be totally wrong, there are lots of Spring/Tomcat/Hibernate users already out there who are comfortable with Tomcat and will want to stick with it when using CDI.  But also keep in mind that the servlet container that ships with JBoss is........Tomcat.


                      The secret to building half a product instead of a half-ass product is saying no. Do I need the extra features, slow startup time, etc, etc of JBoss... as it turns out... no. The Spring and Seam on top of Tomcat give us anything we need. If we ever need something more... we probably go for Geronimo... (Unless JBoss 6 gets to have something a classpathhell solution like this)



                      And if you're only going to use CDI as a DI container, then it's possible users may end up with JSF 2.0, RF 4.0, Guice, JPA 2.0, etc. and may not even need to use CDI/Weld or any other implementation of 299...


                      I don't get what you mean...

                      • 53. Re: How do I get a JPA EntityManager reference in Weld?

                        Arbi Sookazian wrote on Nov 24, 2009 06:29:


                        Well, Spring runs in a servlet container as well.  I've used Spring recently at my current project.  I prefer Seam but the question becomes the following:

                        Most Java developers who are currently running Tomcat in production clusters are using Spring as part of their stack for a variety of reasons but mostly for DI and AOP as well as Spring services like tx mgmt, JdbcTemplate, HibernateDaoSupport, etc.

                        So of these projects running mostly Spring 2.x, why would they migrate towards a new stack using CDI/Weld?  Why not stick with Tomcat/Spring 2.x or upgrade to Spring 3 in the future?


                        That is a good question...



                        So are you telling me your current stack is not Tomcat/Spring?  You have the option to start using Weld with Tomcat suddenly?


                        In our case, switching from Spring to Weld is possible, and I think it might easy for  Steven because Spring and Weld are internal to the application. If I want to start using Weld tomorrow in an application I can do it, and I will not affect the other applications in the Tomcat where they are all deployed. It is like switching from Prototype to JQuery, or from JSP to JSF, Tomcat just does not care. Also, Spring (and Weld) start really fast, so performance is not visibly affected during development... but if I wanted to introduce JBoss, the first thing I had to do is get the server administrator to install it on the server, open a port in the firewall of the server (or configure reverse proxy in apache httpd), configure it so that it starts automatically on reboot, and convince the developers that it is fine to wait 30 seconds until JBoss restarts (each time they need to do so) instead of the 10 seconds Tomcat takes to do the same...


                        If I could install JBoss inside Tomcat as easy as I can add Spring or Weld to an app this story would be different, but I can not, a project named Embedded Jboss exists, but it requires that I modify stuff that is outside of my application, I need to modify stuff in the shared area of Tomcat, that again, means asking the server administrator for permission, convince him to adopt this child and, frankly... I am not even convinced about doing so, so I am not likely to succeed.

                        • 54. Re: How do I get a JPA EntityManager reference in Weld?
                          axiopisty



                          You asked for it, so here it is :-)
                          ...
                          Heres an interceptor that will manage the EntityTransaction for you if you annotate a method @Transactional:

                          I'm experimenting using WELD with JPA in a JSE environment. After reading this long thread I am successfully able to

                          @Inject
                          private EntityManager em;

                          Thanks to Gavin's instructions.


                          However, @Transactional does not work (EntityTransactionInterceptor is never invoked).


                          The EntityTransactionInterceptor does not get invoked when I call a method that I've marked @Transactional. How is the EntityTransactionInterceptor supposed to be invoked?


                          Here is an example of how I am using @Transactional:


                          package org.seamframework.tx;
                          
                          import javax.interceptor.InterceptorBinding;
                          import java.lang.annotation.Documented;
                          import java.lang.annotation.Retention;
                          import java.lang.annotation.Target;
                          
                          import static java.lang.annotation.ElementType.METHOD;
                          import static java.lang.annotation.ElementType.TYPE;
                          import static java.lang.annotation.RetentionPolicy.RUNTIME;
                          
                          @Retention(RUNTIME)
                          @Target({METHOD, TYPE})
                          @Documented
                          @InterceptorBinding
                          public @interface Transactional {
                          }
                          



                          package org.seamframework.tx;
                          
                          import javax.inject.Inject;
                          import javax.interceptor.AroundInvoke;
                          import javax.interceptor.Interceptor;
                          import javax.interceptor.InvocationContext;
                          import javax.persistence.EntityManager;
                          
                          /**
                           * Declarative JPA EntityTransactions
                           *
                           * @author Gavin King
                           */
                          @Transactional
                          @Interceptor
                          public class EntityTransactionInterceptor {
                          
                               @Inject
                               private EntityManager em;
                          
                               @AroundInvoke
                               public Object aroundInvoke(InvocationContext ic) throws Exception {
                                    boolean act = !em.getTransaction().isActive();
                                    if(act)
                                         em.getTransaction().begin();
                                    try {
                                         Object result = ic.proceed();
                                         if(act)
                                              em.getTransaction().commit();
                                         return result;
                                    } catch(Exception e) {
                                         if(act)
                                              em.getTransaction().rollback();
                                         throw e;
                                    }
                               }
                          }
                          



                          ...
                          @ApplicationScoped
                          public class App {
                             @Inject
                             private EntityManager em;
                             ...
                             @Transactional
                             public void insertTestData() {
                                Genius genius = new Genius("Gavin King");
                                em.persist(stud);
                             }
                             ...
                          }
                          



                          Maybe the problem is because I'm using JSE.


                          Any suggestions?

                          • 55. Re: How do I get a JPA EntityManager reference in Weld?
                            axiopisty

                            I wasn't very careful with my last post and just realized a typo. This is how I am using the @Transactional annotation on the insertTestData method:


                            ...
                            @ApplicationScoped
                            public class App {
                               @Inject
                               private EntityManager em;
                               ...
                               @Transactional
                               public void insertTestData() {
                                  Genius genius = new Genius("Gavin King");
                                  em.persist(genius);
                               }
                               ...
                            }
                            



                            ignore the line in the previous post

                            em.persist(stud)

                            • 56. Re: How do I get a JPA EntityManager reference in Weld?
                              ssamayoagt

                              Check this:
                              http://seamframework.org/Documentation/WeldAndJPARunningInTomcat


                              If the EM is application or session scoped (didnt tried conversation) it work for me but request dont.


                              Regards.

                              • 57. Re: How do I get a JPA EntityManager reference in Weld?
                                pmuir

                                Did you enable the interceptor in beans.xml?

                                • 58. Re: How do I get a JPA EntityManager reference in Weld?
                                  leonardoprc
                                  The extension (PersistenceContextExtension) was deployed normally:
                                  INFO: Beginning the scanning process
                                  (...)

                                  But the Weld rejects the Producer bean, claiming that the field (entityManager) has to be scope Dependent:

                                  SEVERE: Exception sending context initialized event to listener instance of class org.jboss.weld.environment.servlet.Listener
                                  org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManager] with qualifiers [@Any @Default] declared as [[field] @Produces @ApplicationScoped @PersistenceContext pacote.library.PersistenceProducer.entityManager]] must be @Dependent scoped
                                       at org.jboss.weld.bean.builtin.ee.EEResourceProducerField.checkEEResource(EEResourceProducerField.java:133)
                                  (...)



                                  Here the bean producer:

                                  @Named(value = "init")
                                  @ApplicationScoped
                                  public class PersistenceProducer implements Serializable {
                                       private static final long serialVersionUID = -6504559420550815184L;

                                       @Produces
                                       @ApplicationScoped
                                       //@Singleton
                                       @PersistenceContext(unitName = "TemplateRichFacesPU")
                                       EntityManager entityManager;
                                  }


                                  There's no way to force Weld to accept the field (entityManager) as: @ApplicationScoped or @Singleton. Weld only accepted @Dependent. SO, MANY ENTITY MANAGER INSTANCES (TRANSACTIONS) ARE REFERENCED.
                                  • 59. Re: How do I get a JPA EntityManager reference in Weld?
                                    chrisharris

                                    Hi, Gavin

                                     

                                    Following http://seamframework.org/Documentation/WeldAndJPARunningInTomcat, I was able to get the example running in Tomcat 7.0.26 running.  Is this example project still valid with more current binaries such as weld-servlet 1.1.8.Final+ in existence?

                                     

                                    Whenever I update the weld dependencies, the app breaks.  weld-servlet 1.1.8.Final is such an example.  I figure before diving into details, I check with you if some weld-related dependency with this Tomcat functionality was pushed to Central.

                                     

                                    Thanks.

                                    1 2 3 4 Previous Next