1 Reply Latest reply on Mar 7, 2008 3:34 PM by Christian Bauer

    Seam, Spring and jBPM integration HOWTO

    Frank Bitzer Newbie

      In this post I will publish the work I did to integrate Seam, Spring and jBPM in order to use the same Hibernate SessionFactory in Spring and jBPM (and of course, Seam).

      Maybe it is not the perfect solution, but it works fine, at least for me.

      At first, make sure you use the latest version 2.1.0 of Seam since you could get trouble with 2.0.1 and SpringTransactions.

      The relevant parts of the configuration are:

      - in your Spring bean config, define your Hibernate sessionFactory as usual and set the following properties in special

      <bean id="hibernateSessionFactory"
           <!-- The hibernate properties  -->
           <property name="hibernateProperties">
                        <prop key="hibernate.hbm2ddl.auto">update</prop>
                     <!-- set to create-drop to NOT maintain state between two executions of the app -->
                     <prop key="hibernate.connection.release_mode">
      <!-- this property must be set to false so we can use independent sessions -->
           <property name="useTransactionAwareDataSource">
           <property name="mappingResources">
                      <!-- here you have to list all the *hbm.xml files for jBPM -->
                      <!-- see the default hibernate.cfg.xml file from jBPM -->

      - second, for the Seam Spring integration we need two beans

      <bean id="sessionFactory"
                <property name="sessionName" value="hibernateSession" />
      <bean id="localTransactionManager"
                <property name="sessionFactory" ref="hibernateSessionFactory" />

      That's it for Spring configuration.

      Now in components.xml, we need

      <!-- use the power of Spring transactions -->
      <spring:spring-transaction platform-transaction-manager-name="localTransactionManager"/>
      <persistence:managed-hibernate-session name="hibernateSession" auto-create="true"
      <component class="org.jboss.seam.bpm.Jbpm">
              <property name="processDefinitions">processdefinition.jpdl.xml</property>

      In order to use the hibernateSession in jBPM, I subclassed the DbPersistenceService from jBPM. You need two classes:

      package your.namespace.jbpm.integration;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.jboss.seam.Component;
      import org.jboss.seam.contexts.Contexts;
      import org.jbpm.svc.Service;
       * @author Frank Bitzer (FBI)
      public class DbPersistenceServiceFactory extends
                org.jbpm.persistence.db.DbPersistenceServiceFactory {
           private static final long serialVersionUID = 997L;
           SessionFactory sessionFactory;
            * {@inheritDoc}
           public Service openService() {
                //create instance of own service implementation
               return new your.namespace.jbpm.integration.DbPersistenceService(this);
            * Retrieve Hibernate sessionFactory
           public synchronized SessionFactory getSessionFactory() {
                if (sessionFactory==null) {
                           //access seam component holding session
                           Session session = (Session)
                           //and extract sessionFactory
                          sessionFactory = session.getSessionFactory();
               return sessionFactory;
            * Set sessionFactory
           public void setSessionFactory(SessionFactory sessionFactory) {
               this.sessionFactory = sessionFactory;

      package your.namespace.jbpm.integration;
      import org.hibernate.Session;
      import org.jbpm.JbpmContext;
      import org.jbpm.persistence.db.DbPersistenceServiceFactory;
      import org.jbpm.svc.Services;
      import org.springframework.orm.hibernate3.SessionFactoryUtils;
       * @author Frank Bitzer (FBI)
      public class DbPersistenceService extends
                org.jbpm.persistence.db.DbPersistenceService {
           private static final long serialVersionUID = 996L;
           public DbPersistenceService(
                     DbPersistenceServiceFactory persistenceServiceFactory) {
                this(persistenceServiceFactory, getCurrentServices());
           static Services getCurrentServices() {
                Services services = null;
                JbpmContext currentJbpmContext = JbpmContext.getCurrentJbpmContext();
                if (currentJbpmContext != null) {
                     services = currentJbpmContext.getServices();
                return services;
           DbPersistenceService(DbPersistenceServiceFactory persistenceServiceFactory,
                     Services services) {
                this.persistenceServiceFactory = persistenceServiceFactory;
                this.isTransactionEnabled = persistenceServiceFactory
                this.isCurrentSessionEnabled = persistenceServiceFactory
                this.services = services;
            * Use Hibernate sessionFactory to retrieve a Session instance.
           public Session getSession() {
                if ((session == null) && (getSessionFactory() != null)) {
                     session = getSessionFactory().openSession();
                     mustSessionBeClosed = true;
                     mustSessionBeFlushed = true;
                     mustConnectionBeClosed = false;
                     isTransactionEnabled = !SessionFactoryUtils.isSessionTransactional(
                               session, getSessionFactory());
                     if (isTransactionEnabled) {
                return session;

      To finish work, simply use the brand-new DbPersistenceService in jbpm.cfg.xml like

          <service name="persistence">
                     <bean class="your.namespace.jbpm.integration.DbPersistenceServiceFactory">
                          <field name="isTransactionEnabled">

      That's it!

      If you have some improvements, please let me know.

      @Seam people: if you want this article to appear somewhere else than in this forum (e.g. in the Knowledge Base), feel free to adapt it.