6 Replies Latest reply on Dec 9, 2013 1:26 AM by Vladimir Belyakof

    Work with database in WorkItemHandler?

    Vladimir Belyakof Newbie

      Hello i have user database.

       

      I Initalize:

       

      <persistence-unit name="userData" transaction-type="JTA">... (persistence.xml)

       

      I use Class for work with database:

       

      @Stateless

      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

      public class BdService {

          @PersistenceContext(unitName = USER_PERSISTENCE_UNIT_NAME)

          private EntityManager entityManager;

           .........

      }

       

      I Work with bd in HttpServlet used:

       

      @EJB

      private BdService bdService;



      Unfortunately, in a custom task @ EJB annotation does not work. Please tell me how I can work with the database from heir WorkItemHandler. Thanks.

        • 1. Re: Work with database in WorkItemHandler?
          Brian Wallis Master

          You haven't stated what container you are running this in or how you are registering your WorkItemHandler.

           

          In any case, the WorkItemHandler will need to be created and registered and that instance will remain active for the life of the engine instance (I think) so it cannot be a managed bean which has a container defined lifecycle.

           

          So, what you can do is lookup the EJB manually rather than try to inject it.

           

          BdService bdService = (BdService)new InitialContext().lookup("java:module/BdService");
          
          

           

          Your JNDI name might be different from this, see the startup logs for the actual JNDI name. You should do this each time you need it and let the container worry about managing the instances.

          • 2. Re: Work with database in WorkItemHandler?
            Vladimir Belyakof Newbie

            Thank you. So I'm moving in the right direction. Could not tell how I can get JNDI name?

            • 3. Re: Re: Work with database in WorkItemHandler?
              Brian Wallis Master

              This should help if you are running jboss 7.1 or EAP 6 JNDI Reference - JBoss AS 7.1 - Project Documentation Editor. It is also output during your server startup when the beans are initialised (each bean has multiple names registered in JNDI). You should see something similar to this for each bean:

               

              16:06:18,648 INFO  [org.jboss.weld.deployer] (MSC service thread 1-5) JBAS016002: Processing weld deployment simpleflow.war
              16:06:18,693 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-5) JNDI bindings for session bean named Workflow in deployment unit deployment "simpleflow.war" are as follows:
              
                java:global/simpleflow/Workflow!au.com.infomedix.simpleflow.Workflow
                java:app/simpleflow/Workflow!au.com.infomedix.simpleflow.Workflow
                java:module/Workflow!au.com.infomedix.simpleflow.Workflow
                java:global/simpleflow/Workflow
                java:app/simpleflow/Workflow
                java:module/Workflow
              
              
              1 of 1 people found this helpful
              • 5. Re: Re: Work with database in WorkItemHandler?
                Brian Wallis Master

                Ahh, now that is a different problem altogether.

                 

                From your logging I see this:

                 

                09:44:02,191 INFO  [org.hibernate.tool.hbm2ddl.SchemaExport] (http-/0.0.0.0:8080-1) HHH000227: Running hbm2ddl schema export
                09:44:02,191 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (http-/0.0.0.0:8080-1) HHH000231: Schema export unsuccessful: java.sql.SQLException: javax.resource.ResourceException: IJ000460: Error checking for a transaction
                

                 

                From which I deduce that you have hibernate set to create the database on startup (is this correct?)

                 

                            <property name="hibernate.hbm2ddl.auto" value="create" />
                

                 

                I think that the problem here is that the creation of the schema is happening without a transaction. Hibernate doesn't play well with container managed transactions as it is effectively trying to use a bean managed (or manual) transaction. (I've had a similar problem before).

                 

                Can you get the schema setup prior to the workflow engine running?

                • 6. Re: Re: Work with database in WorkItemHandler?
                  Vladimir Belyakof Newbie

                  It's my fault did not remove the test code. This worked thanks very much!