Skip to content

Commit d849468

Browse files
committed
split datasources between xpanse and activiti
1 parent 71dc444 commit d849468

File tree

6 files changed

+87
-15
lines changed

6 files changed

+87
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* SPDX-FileCopyrightText: Huawei Inc.
4+
*
5+
*/
6+
7+
package org.eclipse.xpanse.api.config;
8+
9+
import jakarta.persistence.EntityManagerFactory;
10+
import javax.sql.DataSource;
11+
import org.springframework.beans.factory.annotation.Qualifier;
12+
import org.springframework.boot.context.properties.ConfigurationProperties;
13+
import org.springframework.boot.jdbc.DataSourceBuilder;
14+
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.context.annotation.Configuration;
17+
import org.springframework.context.annotation.Primary;
18+
import org.springframework.orm.jpa.JpaTransactionManager;
19+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
20+
import org.springframework.transaction.PlatformTransactionManager;
21+
22+
/** Datasource Config. */
23+
@Configuration
24+
public class DatasourceConfig {
25+
26+
@Primary
27+
@Bean(name = "dataSource")
28+
@ConfigurationProperties(prefix = "spring.datasource")
29+
public DataSource dataSource() {
30+
return DataSourceBuilder.create().build();
31+
}
32+
33+
/** EntityManagerFactory Bean. */
34+
@Primary
35+
@Bean(name = "entityManagerFactory")
36+
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
37+
EntityManagerFactoryBuilder builder) {
38+
return builder.dataSource(dataSource())
39+
.packages("org.eclipse.xpanse")
40+
.persistenceUnit("xpansePU")
41+
.build();
42+
}
43+
44+
@Primary
45+
@Bean(name = "transactionManager")
46+
public PlatformTransactionManager transactionManager(
47+
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
48+
return new JpaTransactionManager(entityManagerFactory);
49+
}
50+
51+
}

modules/workflow/src/main/java/org/eclipse/xpanse/modules/workflow/config/ActivitiConfig.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,21 @@
2525
import org.activiti.spring.SpringProcessEngineConfiguration;
2626
import org.springframework.beans.factory.annotation.Autowired;
2727
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.boot.context.properties.ConfigurationProperties;
29+
import org.springframework.boot.jdbc.DataSourceBuilder;
2830
import org.springframework.context.ApplicationContext;
2931
import org.springframework.context.annotation.Bean;
3032
import org.springframework.context.annotation.Configuration;
3133
import org.springframework.core.io.Resource;
3234
import org.springframework.core.io.ResourceLoader;
3335
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
36+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3437
import org.springframework.transaction.PlatformTransactionManager;
35-
import org.springframework.transaction.TransactionManager;
3638

3739
/** Activiti configuration class. */
3840
@Configuration
3941
public class ActivitiConfig {
4042

41-
private final DataSource dataSource;
42-
43-
private final TransactionManager transactionManager;
44-
4543
private final ApplicationContext applicationContext;
4644

4745
@Value("${spring.activiti.history-level}")
@@ -50,23 +48,30 @@ public class ActivitiConfig {
5048
/** constructor for ActivitiConfig bean. */
5149
@Autowired
5250
public ActivitiConfig(
53-
DataSource dataSource,
54-
TransactionManager transactionManager,
5551
ApplicationContext applicationContext) {
56-
this.dataSource = dataSource;
57-
this.transactionManager = transactionManager;
5852
this.applicationContext = applicationContext;
5953
}
6054

55+
56+
@Bean(name = "activitiDataSource")
57+
@ConfigurationProperties(prefix = "spring.activiti.datasource")
58+
public DataSource activitiDataSource() {
59+
return DataSourceBuilder.create().build();
60+
}
61+
62+
@Bean(name = "activitiTransactionManager")
63+
public PlatformTransactionManager activitiTransactionManager() {
64+
return new DataSourceTransactionManager(activitiDataSource());
65+
}
66+
6167
/** Create ProcessEngineConfiguration object into SpringIoc. */
6268
@Bean
6369
public ProcessEngineConfiguration processEngineConfiguration() throws IOException {
6470
SpringProcessEngineConfiguration springProcessEngineConfiguration =
6571
new SpringProcessEngineConfiguration();
6672
springProcessEngineConfiguration.setIdGenerator(strongUuidGenerator());
67-
springProcessEngineConfiguration.setDataSource(dataSource);
68-
springProcessEngineConfiguration.setTransactionManager(
69-
(PlatformTransactionManager) transactionManager);
73+
springProcessEngineConfiguration.setDataSource(activitiDataSource());
74+
springProcessEngineConfiguration.setTransactionManager(activitiTransactionManager());
7075
springProcessEngineConfiguration.setDatabaseSchemaUpdate("true");
7176
springProcessEngineConfiguration.setDeploymentMode("single-resource");
7277
Resource[] resources =

runtime/src/main/java/org/eclipse/xpanse/runtime/XpanseApplication.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
@EnableCaching
2424
@EnableRetry
2525
@EnableAsync(proxyTargetClass = true)
26-
@EnableJpaRepositories(basePackages = "org.eclipse.xpanse")
26+
@EnableJpaRepositories(
27+
basePackages = "org.eclipse.xpanse",
28+
entityManagerFactoryRef = "entityManagerFactory",
29+
transactionManagerRef = "transactionManager")
2730
@EntityScan(basePackages = "org.eclipse.xpanse")
2831
@ComponentScan(basePackages = "org.eclipse.xpanse")
2932
@EnableJpaAuditing(dateTimeProviderRef = "auditDateTimeProviderConfig")

runtime/src/main/resources/application-mysql.properties

+2
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
spring.datasource.name=mysql
77
spring.datasource.url=jdbc:mysql://localhost:3306/xpanse
88
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
9+
spring.activiti.datasource.jdbcUrl=jdbc:mysql://localhost:3306/activiti
10+
spring.activiti.datasource.driverClassName=com.mysql.cj.jdbc.Driver
911
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

runtime/src/main/resources/application.properties

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ spring.lifecycle.timeout-per-shutdown-phase=300s
1010
spring.profiles.active=oauth,zitadel,zitadel-local,terra-boot,tofu-maker
1111
spring.banner.location=classpath:banner.txt
1212
spring.datasource.name=h2
13-
spring.datasource.url=jdbc:h2:file:./xpanse;DB_CLOSE_ON_EXIT=FALSE
13+
spring.datasource.jdbcUrl=jdbc:h2:file:./xpanse;DB_CLOSE_ON_EXIT=FALSE;
1414
spring.datasource.driverClassName=org.h2.Driver
1515
spring.datasource.username=xpanse
1616
spring.datasource.password=Xpanse@2023
17+
spring.activiti.datasource.jdbcUrl=jdbc:h2:file:./activiti;DB_CLOSE_ON_EXIT=FALSE;
18+
spring.activiti.datasource.driverClassName=org.h2.Driver
19+
spring.activiti.datasource.username=activiti
20+
spring.activiti.datasource.password=Activiti@2023
1721
spring.jpa.show-sql=false
1822
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
1923
spring.jpa.hibernate.ddl-auto=none

runtime/src/test/java/org/eclipse/xpanse/runtime/database/mysql/AbstractMysqlIntegrationTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ public abstract class AbstractMysqlIntegrationTest extends ZitadelTestContainer
3333

3434
@DynamicPropertySource
3535
static void props(DynamicPropertyRegistry registry) {
36-
registry.add("spring.datasource.url", mysqlContainer::getJdbcUrl);
36+
registry.add("spring.datasource.jdbcUrl", mysqlContainer::getJdbcUrl);
3737
registry.add("spring.datasource.username", mysqlContainer::getUsername);
3838
registry.add("spring.datasource.password", mysqlContainer::getPassword);
3939
}
40+
41+
@DynamicPropertySource
42+
static void activitiProps(DynamicPropertyRegistry registry) {
43+
registry.add("spring.activiti.datasource.jdbcUrl", mysqlContainer::getJdbcUrl);
44+
registry.add("spring.activiti.datasource.username", mysqlContainer::getUsername);
45+
registry.add("spring.activiti.datasource.password", mysqlContainer::getPassword);
46+
}
4047
}

0 commit comments

Comments
 (0)