4 Replies Latest reply on Jul 27, 2010 10:46 AM by Laird Nelson

    CDI and EJB: why is no transaction started?

    Laird Nelson Apprentice
      I'm building a Vaadin application following the [guidelines listed here (option 2)=>http://vaadin.com/wiki/-/wiki/Main/Creating%20JEE6%20Vaadin%20Applications].

      Long story short, the injection "hierarchy" looks like this:
      Servlet @Injects an Instance<MyApplication>...
      ...which @Injects an Instance<DAO>...
      ...where DAO is the local business interface implemented by lots of my EJBs.  One such EJB has...
      ...a @PersistenceContext-annotated EntityManager field.

      Deploying this tidy little hairball in an .ear file on Glassfish 3.1 works fine as far as injection is concerned; everything is non-null and I am able to invoke methods on everything.  All session beans implementing the DAO interface are discovered and available through the Instance<DAO> that can iterate over them.  So far so good.

      However, when I invoke a business method on the DAO interface, I get a javax.persistence.TransactionRequired exception.  This is despite the fact that the EJB in question does not define any additional @TransactionAttribute annotations, so it should pick up the REQUIRED transaction semantics.

      It is almost as if Weld is not recognizing that the DAO in question is in fact a stateless session bean.  I mean, obviously it's FINDING the stateless session bean in question, and injecting it, but I'd expect a transaction to be started automatically here, and none is.

      This is such a common case that I'm sure I'm doing something wrong, but I can't see what it is.  Help?

      Best,
      Laird
        • 1. Re: CDI and EJB: why is no transaction started?
          Laird Nelson Apprentice

          My related Glassfish forums post, in case any information there is helpful.


          Best,
          Laird

          • 2. Re: CDI and EJB: why is no transaction started?
            Pete Muir Master

            Looking at the stack trace from that post, it looks like it isn't being picked up as an EJB. Can you show your code?

            • 3. Re: CDI and EJB: why is no transaction started?
              Laird Nelson Apprentice
              I'll do my best.  I apologize in advance for the formatting; the formatter in this odd little forum won't let me use Java generics; it thinks they're invalid HTML.  That prevents me from using wiki markup to isolate the code blocks.  :-(

              Anyway, my servlet has this:

              @Inject
              private Instance<LEADApplication> application;

              The servlet is utterly uninteresting, as it serves merely to connect the interwebs to a Vaadin application instance.  That's pretty much it for the servlet.

              LEADApplication is defined like this:

              @SessionScoped
              public class LEADApplication extends Application {
                //...
              }

              Inside it, it has this:

              @Inject
              private Instance<DAO> daos;

              LEADApplication is housed in my .war file in the WEB-INF/classes directory tree.  WEB-INF/beans.xml is also there, so it's a bean archive.

              Onwards to the EJB business interface.

              DAO is a business interface, stored in its own jar file.  It is defined (in part) like this (no other annotations or CDI or EJB bits in there):

              public interface DAO extends Serializable {

              }

              That jar file lives in the ear under its lib directory.  It does not contain WEB-INF/beans.xml, as it consists entirely of interfaces.

              Next up, one of the EJBs.

              PersonManagerBean is defined like this:

              @Local({ PersonManager.class, DAO.class })
              @Stateless
              public class PersonManagerBean extends BasePersonManagerBean {

              }

              It is stored in a jar file at the root of the ear file, and declared as an EJB module in application.xml.  It has a META-INF/beans.xml, thus rendering it a bean archive.

              BasePersonManagerBean is declared like this:

              public class BasePersonManagerBean extends AbstractDAO implements PersonManager {

              }

              There are no annotations in it.  It is stored in its own jar file under the lib directory of the ear file.  It has a META-INF/beans.xml, thus rendering it a bean archive.

              The AbstractDAO from which it extends is declared like this:

              public abstract class AbstractDAO implements DAO, Serializable {

                @PersistenceContext
                protected EntityManager em;

              }

              It is housed in a jar file present in the lib directory in the .ear file.  Its jar file has a META-INF/beans.xml, thus rendering it a bean archive.

              As far as the code goes, that's really about it.  I hope this helps get to the bottom of this.  Let me know if you need more details.

              Best,
              Laird
              • 4. Re: CDI and EJB: why is no transaction started?
                Laird Nelson Apprentice

                Pete, it appears that there are lots of issues involved here; most seem to be Glassfish-related.  Nevertheless, perhaps you could chime in on the discussion over there?  You might spot something.  Thanks for your help.


                Best,
                Laird