AnsweredAssumed Answered

Create Database at Process Engine Creation time - difference in behavior between StandAlone and Spring Configuration

Question asked by manchandap on Aug 5, 2014
Latest reply on Aug 6, 2014 by martin.grofcik
I am trying to create the Database Schema at the time of Process Engine Creation. However, there is difference in behavior of 'databaseSchemaUpdate' property when using the Stand Alone Configuration vs the Spring Based configuration that comes out of the bos with the Activiti Explorer web application. In both the cases, I am using MS SQL Database.
For reference, the details of the properties as per the Activiti User Guide:

<blockcode>
The following properties can be set, regardless of whether you are using the JDBC or data source approach:
    databaseType: it's normally not necessary to specify this property as it is automatically analyzed from the database connection meta data. Should only be specified in case automatic detection fails. Possible values: {h2, mysql, oracle, postgres, mssql, db2}. This property is required when not using the default H2 database. This setting will determine which create/drop scripts and queries will be used. See the 'supported databases' section for an overview of which types are supported.
    databaseSchemaUpdate: allows to set the strategy to handle the database schema on process engine boot and shutdown.
        false (default): Checks the version of the DB schema against the library when the process engine is being created and throws an exception if the versions don't match.
        true: Upon building the process engine, a check is performed and an update of the schema is performed if it is necessary. If the schema doesn't exist, it is created.
        create-drop: Creates the schema when the process engine is being created and drops the schema when the process engine is being closed.
</blockcode>

If I use the following code, the database schema gets created:

      ProcessEngineConfiguration standAloneProcessConfig = (StandaloneProcessEngineConfiguration)ProcessEngineConfiguration
             .createStandaloneProcessEngineConfiguration();  
       standAloneProcessConfig.setJdbcDriver(JDBC_DRIVER);
       standAloneProcessConfig.setJdbcPassword(JDBC_PWD);
       standAloneProcessConfig.setJdbcUrl(JDBC_URL);
       standAloneProcessConfig.setJdbcUsername(JDBC_USERNAME);
       standAloneProcessConfig.setDatabaseType(DB_TYPE);
       standAloneProcessConfig.setDatabaseSchemaUpdate("true");
        ProcessEngine processEngine = standAloneProcessConfig.buildProcessEngine();


However, the Activiti Explorer is not able to create the Database Schema. The snippet from <blockcode>activiti-standalone-context.xml</blockcode> is as follows:


  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
     <property name="dataSource" ref="dataSource" />
     <property name="transactionManager" ref="transactionManager" />
     <property name="databaseType" value="mssql" />
     <property name="databaseSchemaUpdate" value="true" />
     <property name="jobExecutorActivate" value="true" />
    <property name="enableDatabaseEventLogging" value="true" />
    <property name="customFormTypes">
      <list>
        <bean class="org.activiti.explorer.form.UserFormType"/>
        <bean class="org.activiti.explorer.form.ProcessDefinitionFormType"/>
        <bean class="org.activiti.explorer.form.MonthFormType"/>  
      </list>
    </property>
  </bean>

<blockcode>The databaseType property was configured by me</blockcode>

A quick look at the code of <blockcode>org.activiti.spring.SpringProcessEngineConfiguration</blockcode>, reveals that it extends the <blockcode>org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl</blockcode>. This Impl class hard codes the value for databaseSchemaUpdate to false in its initIdGenerator() method. The code snippet is reproduced below:

protected void initIdGenerator() {
    if (idGenerator==null) {
      CommandExecutor idGeneratorCommandExecutor = null;
      if (idGeneratorDataSource!=null) {
        ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
        processEngineConfiguration.setDataSource(idGeneratorDataSource);
        processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
        processEngineConfiguration.init();
        idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
      } else if (idGeneratorDataSourceJndiName!=null) {
        ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
        processEngineConfiguration.setDataSourceJndiName(idGeneratorDataSourceJndiName);
        processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
        processEngineConfiguration.init();
        idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
      } else {
        idGeneratorCommandExecutor = getCommandExecutor();
      }


So, before I dig further, my queries/confusion:
# Is the above hard coding reason for database schema not getting created
# Is this the intended behavior that we don't want the database creation to happen automatically when Process Engine is created by Spring
# Can we override this behavior

Outcomes