Camunda Spring事务集成无法正常工作

时间:2016-05-27 14:32:16

标签: java spring transactions camunda

我正在使用Camunda 7.3,Spring 4.2.4和Hibernate 4.3.8,我试图将它们与Camunda Documentation中所述的相同事务一起使用。该事务在Hibernate操作中运行正常,但在Camunda操作中没有运行,如果发生事务回滚只是恢复了休眠操作。

@Configuration
public class CamundaConfiguration {

    // Variables with connection Data

    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setPersistenceUnitName("PostgreSQL");
        bean.setDataSource(dataSource());
        bean.getJpaPropertyMap().put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
        bean.getJpaPropertyMap().put("hibernate.ejb.naming_strategy", NamingStrategyLowerCase.class.getCanonicalName());
        bean.getJpaPropertyMap().put("hibernate.jdbc.batch_size", 0);
        bean.getJpaPropertyMap().put("hibernate.cache.use_second_level_cache", true);
        bean.getJpaPropertyMap().put("hibernate.cache.use_query_cache", true);
        bean.getJpaPropertyMap().put("javax.persistence.sharedCache.mode", SharedCacheMode.ALL);
        bean.getJpaPropertyMap().put("hibernate.cache.default_cache_concurrency_strategy", "read-write");
        bean.getJpaPropertyMap().put("javax.persistence.validation.factory", validator);
        bean.getJpaPropertyMap().put("hibernate.cache.region.factory_class", SingletonEhCacheRegionFactory.class.getCanonicalName());
        bean.setPersistenceProviderClass(org.hibernate.jpa.HibernatePersistenceProvider.class);
        bean.setPackagesToScan("br.com.model");
        return bean;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager bean = new JpaTransactionManager(entityManagerFactory());
        bean.getJpaPropertyMap().put("org.hibernate.flushMode", FlushMode.AUTO);
        bean.setDataSource(dataSource);
        bean.setPersistenceUnitName("PostgreSQL");
        return bean;
    }

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(driverClass);
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(username);
        config.setPassword(password);
        config.setMaximumPoolSize(50);
        config.setConnectionTestQuery("select 1");

        HikariDataSource bean = new HikariDataSource(config);
        return new LazyConnectionDataSourceProxy(bean);
    }

    @Bean
    public ManagedProcessEngineFactoryBean processEngine() {
        ManagedProcessEngineFactoryBean processEngineFactoryBean = new ManagedProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(processEngineConfiguration());
        return processEngineFactoryBean;
    }

    @Bean
    public SpringProcessEngineConfiguration processEngineConfiguration() {
        SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
        processEngineConfiguration.setDataSource(dataSource());
        processEngineConfiguration.setTransactionManager(transactionManager());
        processEngineConfiguration.setJobExecutorActivate(true);
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfigurationImpl.DB_SCHEMA_UPDATE_TRUE);
        return processEngineConfiguration;
    }

    @Bean
    public TaskService taskService() throws Exception {
        return processEngine().getObject().getTaskService();
    }
}

dataSource和transactionManager与Spring和Hibernate使用的相同。

@Service
public class TaskManager {
    @Inject
    private TaskService taskService;

    @Transactional
    public void completeTask(String taskId, final Map<String, Object> variables) {

        org.camunda.bpm.engine.task.Task camundaTask = taskService.createTaskQuery().taskId(taskId).singleResult();
        taskService.complete(camundaTask.getId(), variables);

        // Hibernate Operations

        throw new RuntimeException("Exception test");
    }
}

执行上面的代码时,会发生回滚并且回滚'Hibernate Operations',但是taskService.complete中执行的操作不会。

我已经调试了Camunda代码,一切似乎都没问题,我找到了一个SpringTransactionInterceptor,命令在TransactionTemplate.execute()内执行,此时交易处于活动状态。

1 个答案:

答案 0 :(得分:1)

在研究了事务,Jpa和Spring之后,我发现问题是jpaDialect没有配置,它负责同步JDBC和JTA事务。

  

dialect对象可用于检索底层JDBC   连接因此允许将JPA事务公开为JDBC   交易。

我在配置中包含以下代码,现在它正在运行:

@Configuration
public class CamundaConfiguration {
    ....

    @Bean
    public JpaDialect jpaDialect() {
        return new org.springframework.orm.jpa.vendor.HibernateJpaDialect();
    }

    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setJpaDialect(jpaDialect());
        bean.setJpaVendorAdapter(new org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter());
        ...
        return bean;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager bean = new JpaTransactionManager(entityManagerFactory());
        bean.setJpaDialect(jpaDialect());
        ...
        return bean;
    }

    ...
}