7 Replies Latest reply on Jan 9, 2007 9:44 AM by norman.richards

    Entity not persisting from within a Servlet Filter

    brentgfox

      I'm attempting to integrate a servlet filter I wrote into a new seam application, and I can't seem to get it to persist an entity bean. I've created an EntityManager from an EntityManagerFactory, and can execute queries against it, but it doesn't seem to persist the data.

      I've tried a few combinations - but to no avail.

      From my servlet filter:

      public void doFilter(ServletRequest request, ServletResponse response,
       FilterChain chain) throws IOException, ServletException
       {
       ...
       try
       {
       InitialContext ctx = new InitialContext();
       emf = (EntityManagerFactory) ctx.lookup("java:/userDatabase");
       EntityManager em = emf.createEntityManager();
       try
       {
       System.out.println("Attempting ejb query.");
       List existing = em.createQuery("from User").getResultList();
       Iterator ie = existing.iterator();
       while (ie.hasNext())
       {
       ie.next();
       System.out.println("Object Found");
       }
      
       // And the code that doesn't work.
       // User u = new User("tester");
       // em.persist(user);
      
      
      ...
      


      And from my persistence.xml file

      ...
       <persistence-unit name="userDatabase">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/Postgres</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/userDatabase"/>
       </properties>
       </persistence-unit>
      ...
      


      And from my web.xml

      ...
       <!-- Persistence Context Ref -->
       <persistence-context-ref>
       <persistence-context-ref-name>persistence/UserDatabase</persistence-context-ref-name>
       <persistence-unit-name>userDatabase</persistence-unit-name>
       </persistence-context-ref>
      ...
      


      The query returns objects - but when I attempt to persist the User entity - nothing happens. No exceptions, no results, no record in the database.

      I'm running JBoss 4.0.5GA, with ejb3, Seam 1.1, connecting to a PostgreSQL 8.1 database.

      Anyone have an idea why it will not persist?

        • 1. Re: Entity not persisting from within a Servlet Filter

          Does it work if your start a transaction?

          • 2. Re: Entity not persisting from within a Servlet Filter
            brentgfox

            Thanks for the reply.

            Now, after wrapping it in a transaction, when I attempt to get the transaction manager from the entity manager, I get the following exception:

            ...
            java.lang.IllegalStateException: JTA EntityManager cannot access a transactions
            ...
            


            I've hunted around the web, and found a few people that have seen this, but no solutions. Not sure what that means - any ideas?


            • 3. Re: Entity not persisting from within a Servlet Filter

              You'll probably need to look up the the the user transaction object in JNDI (java:/UserTransaction) and then explicitly call begin/commit.

              As you can see, it's always preferable to be doing these things in a managed environment where you don't need to think about these things. :)

              • 4. Re: Entity not persisting from within a Servlet Filter
                shane.bryzak

                I agree with Norman. I'd be inclined to put your business logic into a Seam component and then call that from your filter. SeamServletFilter is a good example of how to set up the Seam contexts for the request.

                • 5. Re: Entity not persisting from within a Servlet Filter
                  brentgfox

                   

                  "sbryzak2" wrote:
                  I agree with Norman. I'd be inclined to put your business logic into a Seam component and then call that from your filter. SeamServletFilter is a good example of how to set up the Seam contexts for the request.


                  I've tried wrapping it in a transaction gathered from a context lookup - but it still doesn't seem to persist the entity.

                  The more I look at it - the suggestion that I put this into a seam component and call it from the filter seems like the right way to go. Managing it from outside defeats the purpose of using an application framework like seam. Old habits die hard. ;-)

                  I appreciate the quick responses.
                  Thanks again.

                  • 6. Re: Entity not persisting from within a Servlet Filter
                    smokingapipe

                    You can definitely do this from a filter. I have a servlet that gets an EntityManager. It must get a transaction to work! It can do queries without a transaction but it cannot do writes without one. As for why it can't find a transaction, I have no idea. Maybe post some of your persistence configuration stuff? With my setup it works no problem, and a Servlet is not very different from a Filter.

                    But in general, most things like that should happen in Seam components.

                    • 7. Re: Entity not persisting from within a Servlet Filter

                      Hmm... If you have the TX started then you really ought to be able to persist. I'm really curious what the issue is in this.