3 Replies Latest reply on Apr 10, 2007 7:32 AM by iantimothy

    SessionFactory creation

    srsuarez

      I am developing a java web application (on Tomcat) that uses jbpm.
      The problem is that when I run the apache tomcat server with my application, it doesn't create the sessionfactory object.
      I have tested the example web application (from the starter kit) on my tomcat server and it creates the sessionfactory object when I run the tomcat server. (I watch it on the console window: ...[SessionFactoryImpl] building session factory...)
      Why my application doesn't create it?
      Thanks.

        • 1. Re: SessionFactory creation
          iantimothy

          Hello. The SessionFactory would are referring to is part of Hibernate. You can read up on the Hibernate site how to create a SessionFactory.

          For example, you could put the hibernate.cfg.xml file at the root of your classpath.

          Then to get a SessionFactory, you can use this line of code:

          SessionFactory sf = new Configuration().configure().buildSessionFactory();

          There is also another way to get a SessionFactory.

          In your jbpm.cfg.xml, there should be this line

          <service name="persistence"
          factory="org.jbpm.persistence.db.DbPersistenceServiceFactory"/>


          If there isn't, add it in between <jbpm-context></jbpm-context>

          Add jbpm.cfg.xml to the root of your classpath.

          You could then create a class with the a static field. i.e.:

          private static final JbpmConfiguration jbpmConfiguration;


          you can use a static initalizer in this class to create the JbpmConfiguration object.i.e.:

          static {
           try {
           jbpmConfiguration = JbpmConfiguration.getInstance();
           } catch (Throwable ex) {
           throw new ExceptionInInitializerError(ex);
           }
          }


          and write a static method to get this object.i.e.:

          public static JbpmConfiguration getJbpmConfiguration() {
           return jbpmConfiguration;
          }


          You could call this class something like JbpmUtil

          You could use a listener to create an instance of this object when your application starts.

          From then on, when you need a SessionFactory, you can use these lines of code:

          JbpmConfiguration jbpmConfig = JbpmUtil.getJbpmConfiguration();
          JbpmContext jbpmContext = jbpmConfig.createJbpmContext();
          SessionFactory sessionFactory = jbpmContext.getSessionFactory();


          Hopefully this helps. This is the way it worked for me to integrate jbpm, hibernate and my own application.

          Hopefully some of the seniors can add to this.

          • 2. Re: SessionFactory creation
            srsuarez

            Hi iantimothy. Thanks for your answer.
            I am creating the sessionfactory object like you comment. My problem is that I want that the sessionFactory object could be created when I start the tomcat server, because when I create the sessionFactory when I am going to make a query on my application, it spent a lot of time.
            I see that the example web application (starter kit) creates the sessionFactory when I start the tomcat server. That is what I want.
            Thanks.

            • 3. Re: SessionFactory creation
              iantimothy

              hello.

              yup...like the example code I gave, you would need to create a class (e.g. JbpmUtil.java) and use a static initializer to create an instance of the JbpmConfiguration object.

              Also, you need a static method to access the created static jbpmConfiguration.

              Then, you would need a listener. i.e.

              public class AppListener implements ServletContextListener


              You can try googling how to implement the listener.

              then you can put this line in the web.xml file

              <listener>
               <listener-class>package.AppListener</listener-class>
               </listener>


              You could google how to do this also.

              In your AppListener, you would need to use this code.

              public void contextInitialized(ServletContextEvent arg0) {
               try {
               Class.forName("package.JbpmUtil").newInstance();
               }
               catch (Exception e) {
               e.printStackTrace();
               }
              }


              I suggest you google this to get the actual code but I hoped that you have been pointed in the right direction.

              What happens is when the server starts, the listener contextInitialized method will be called which will then create an instance of JbpmUtil. When this happens, the JbpmConfiguration is created. I think from then on, you can get the sessionfactory u need from jbpmConfiguration.