Camunda使用单独的用户创建数据库脚本

时间:2019-04-15 06:13:32

标签: database-migration camunda

在我的Camunda Spring Boot应用程序中,我使用application.properties设置数据库连接信息,即

spring.datasource.url=<database url>
spring.datasource.username=<app username>
spring.datasource.password=<password>
spring.datasource.driverClassName=<driver>

当我第一次对空数据库运行时,Camunda会重用此用户信息以运行其数据库创建脚本。是否可以为Camunda sql脚本指定一个单独的数据库用户?我想有2个数据库用户,一个用于应用程序,一个用于数据库迁移任务。

1 个答案:

答案 0 :(得分:0)

如果要有2个数据库用户,则应在application.yml中定义所有2个数据源的属性,然后应创建CamundaConfiguration类以在春季启动时配置camnuda数据源。

将下面的代码插入application.yml

datasource:
    initialize: false
    type: com.zaxxer.hikari.HikariDataSource
    jdbcUrl: jdbc:oracle:thin:@192.168.0.1:1521:MYDB
    username: MYAPP
    password: MYAPP


camunda.bpm:
        job-execution-enabled:  true
        history-level: FULL
        auto-deployment-enabled: true
        id-generator: strong
        checkProcessDefinitions: true
        deployment-resource-pattern:  classpath:/process/**/*.bpmn
        datasource:
          type: com.zaxxer.hikari.HikariDataSource
          schema-update: true
          schema-name: camunda
          jdbcUrl: jdbc:oracle:thin:@192.168.0.1:1521:MYDB
          username: camunda
          password: camunda

创建CamundaConfiguration类

 package org.valz.service.impl;
 import java.io.IOException;
 import java.sql.SQLException;

 import javax.naming.NamingException;
 import javax.sql.DataSource;

 import org.valz.framework.common.core.Constants;
 import org.camunda.bpm.engine.spring.ProcessEngineFactoryBean;
 import org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration;
 import org.camunda.bpm.engine.spring.SpringProcessEngineServicesConfiguration;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
 import org.springframework.context.annotation.Profile;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.support.ResourcePatternResolver;
 import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 import org.springframework.jndi.JndiTemplate;
 import org.springframework.transaction.PlatformTransactionManager;
 @Configuration
 @Import( SpringProcessEngineServicesConfiguration.class )
 public class CamundaConfiguration  {
     @Value("${spring.camunda.bpm.history-level}")
     private String historyLevel;

     // add more configuration here
     // ---------------------------

     // configure data source via application.properties

     @Autowired
     private ResourcePatternResolver resourceLoader;

     @Bean
     // @ConfigurationProperties(prefix="spring.camunda.bpm")
     public SpringProcessEngineConfiguration processEngineConfiguration() throws IOException {
         SpringProcessEngineConfiguration config = new SpringProcessEngineConfiguration();

         config.setDataSource(camundaDataSource());
         config.setDatabaseSchemaUpdate("true");

         config.setTransactionManager(transactionManager());
         config.setHistory(historyLevel);

         config.setJobExecutorActivate(false);
         config.setMetricsEnabled(false);
         config.setJdbcBatchProcessing(false);
         // deploy all processes from folder 'processes'classpath:/process/*.bpmn
         Resource[] resources = resourceLoader.getResources("classpath:/process/**/*.bpmn");
         config.setDeploymentResources(resources);

         return config;
     }

     @Bean
     public PlatformTransactionManager transactionManager() {
         return new DataSourceTransactionManager(camundaDataSource());
     }

     @Bean(name="camundaBpmDataSource")
     @ConfigurationProperties(prefix="spring.camunda.bpm.dataSource")
     @Profile(Constants.SPRING_PROFILE_DEVELOPMENT)
     public DataSource camundaDataSource() {
         System.out.println("spring.camunda.bpm.dataSource");
         return DataSourceBuilder.create().build();
     }
     @Bean(name = "camundaBpmDataSource")
     @Profile(Constants.SPRING_PROFILE_PRODUCTION)
     public DataSource jndiDataSource() throws SQLException, NamingException {
         JndiTemplate jndi = new JndiTemplate();
         DataSource dataSource = (DataSource) jndi.lookup("camunda");
         return dataSource;
     }
     @Bean
     public ProcessEngineFactoryBean processEngine() throws IOException {
         ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
         factoryBean.setProcessEngineConfiguration(processEngineConfiguration());
         return factoryBean;
     }

 }

并也添加此类

package org.Valz.boot.config;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {
   public DatabaseConfiguration() {
   }

@Bean(
    name = {"dataSource"}
)
@Primary
@ConfigurationProperties(
    prefix = "spring.datasource"
)

public DataSource dataSource() {
    return DataSourceBuilder.create().build();
}

}