2 Replies Latest reply on Jun 10, 2006 12:22 AM by nomad

    Question regarding JbpmContext

    nomad

      Hi,

      I'm a newbe with this jbpm. I really hope I can get some help from the experts here.
      I'm confused about the usage of JbpmContext.

      1. When exactly do we need to call JbpmConfiguration.createJbpmContext() vs JbpmContext.getCurrentJbpmContext() ?

      2. when do we need to close this JbpmContext object ?
      In the source code, an instance of JbpmContext is kept using ThreadLocal, so is that mean as long as the thread is not dead the JbpmContext object should never be closed ? Assuming a code using jbpm is running inside an app server which has 15 active threads, should each thread keep its own JbpmContext and never close it ? or is it based on usage, so every time a request arrive on the appserver, one thread will serve the reqest, create a new jbpmContext, do the transaction (or what ever needed..) and close it after finished ?


      2. Base on the source code (if i'm not mistaken of course), JbpmContext is created using ObjectFactory and all the constructor parameters needed by the new JbpmContext such as Services and ObjectFactory itself is provided by the ObjectFactory, but what I don't understand is inside method JbpmContext.close(), it actually calls services.close() which in turn close and release all the services. How come JbpmContext close the services that it doesn't own, I mean, the services object is actually provided by the ObjectFactory and the same services object could be used in some other part of the code (..in my case I happen to use it, especially for the hibernate session), so don't you think JbpmContext should never close the services ?


      Well, I must be wrong at some point here, but I don't have a clue. Please help.


      Regards,

      Martin

        • 1. Re: Question regarding JbpmContext
          hosierdm

          I have code running inside an app server, and what I do is create a context in every method and close at the end. For example, I might have something like this:

          public void deleteUser(String user) {
           JbpmContext jbpmContext = _jbpmConfig.createJbpmContext();
           try {
           // do some stuff
           }
           finally {
           jbpmContext.close();
          }
          
          public void deleteGroup(String group) {
           JbpmContext jbpmContext = _jbpmConfig.createJbpmContext();
           try {
           // do some other stuff
           }
           finally {
           jbpmContext.close();
          }
          


          Where _jbpmConfig is just a cached instance of the JbpmConfiguration object. I haven't noticed any problems doing things this way. I haven't messed with it too much, but I imagine that if in one of my methods above, I called another method and needed a reference to the context, I could call getCurrentJbpmContext() and I would get the one that I created at the start of the calling method. I'm sure someone like Ronald can offer more insight (and hopefully set me straight if I'm way off base here...especially since I have an application going into system test on Monday using code like that above!)

          • 2. Re: Question regarding JbpmContext
            nomad

            Yeah, actually I write my code the same way as you do hosierdm. I haven't tested the code inside appserver, only tested it using a swing base client. What I notice is after I close the jbpmcontext object I can no longer use my hibernate session because it already closed inside JbpmContext.close() method. Even creating a new JbpmContext object again will still use the same hibernate session that has been closed previously. So this code will not work :

            
            public void deleteUser(String user) {
             JbpmContext jbpmContext = _jbpmConfig.createJbpmContext();
             try {
             // do some stuff
             }
             finally {
             jbpmContext.close();
             }
            
             // access persistence layer using hibernate session will throw error
             // indicating that the hibernate session is already closed
            
             //even creating a new JbpmContext like this will not help
             jbpmContext = _jbpmConfig.createJbpmContext();
             try {
             // do some stuff
             }
             finally {
             jbpmContext.close();
             }
            
            }
            



            May be I made a mistake somewhere, but not sure where. I use spring + jbpm based on the code by criess which you can find here :
            http://www.jboss.com/index.html?module=bb&op=viewtopic&t=78052&start=0&postdays=postDays&postorder=postOrder&highlight=highlight

            Basically I let spring to manage my Hibernate session, the JbpmConfiguration and the JbpmContext object creation.


            By the way, thx for your response hosierdm, well , at least I'm quite sure now that I've used jbpm api quite correctly :p

            Regards,

            Martin