gpt4 book ai didi

java - spring-data 多数据源配置运行但插入错误的数据源

转载 作者:行者123 更新时间:2023-12-05 07:39:26 24 4
gpt4 key购买 nike

配置

我已经按照本教程在 spring-data 中配置了两个数据源:

https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

成功启动tomcat并使用flyway初始化两个数据库。

第一个数据源配置是这样的

@Configuration
@EnableTransactionManagement
@EnableMybatisRepositories(
value = "com.domain.api.userManagement.repository",
mapperLocations = {
"classpath*:/mappers/userManagement/*Mapper.xml",
"classpath*:/beforemappers/userManagement/*Mapper.xml"
}
)
public class UserManagementDbConfig {

@Value("${api.db.userManagement.version}")
private String version;

@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "api.db.userManagement")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

@Bean
@Qualifier("userManagement")
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory;
}


@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}

@Bean(initMethod = "migrate")
public Flyway flyway() throws SQLException {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(true);
flyway.setLocations("classpath:db/migration/userManagement");
flyway.setSqlMigrationPrefix("V");
flyway.setSqlMigrationSuffix(".sql");
flyway.setEncoding("UTF-8");
flyway.setValidateOnMigrate(false);
flyway.setOutOfOrder(true);
flyway.setTargetAsString(version);
flyway.setDataSource(dataSource());
return flyway;
}
}

第二个数据源:

@Configuration
@EnableTransactionManagement
@EnableMybatisRepositories(
value = "com.domain.api.companyManagement.repository",
mapperLocations = {
"classpath*:/mappers/companyManagement/*Mapper.xml",
"classpath*:/beforemappers/companyManagement/*Mapper.xml"
}
)
public class CompanyManagementDbConfig {

@Value("${api.db.companyManagement.version}")
private String version;

@Bean(name = "companyManagementDataSource")
@ConfigurationProperties(prefix = "api.db.companyManagement")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}

@Bean
@Qualifier("companyManagement")
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "companyManagementSqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory;
}

@Bean(name = "companyManagementSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}


@Bean(initMethod = "migrate", name = "companyManagementFlyway")
public Flyway flyway() throws SQLException {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(true);
flyway.setLocations("classpath:db/migration/companyManagement");
flyway.setSqlMigrationPrefix("V");
flyway.setSqlMigrationSuffix(".sql");
flyway.setEncoding("UTF-8");
flyway.setValidateOnMigrate(false);
flyway.setOutOfOrder(true);
flyway.setTargetAsString(version);
flyway.setDataSource(dataSource());
return flyway;
}
}

这就是我编写负责插入的服务的方式:

@Service
public class TestServiceImpl extends AbstractCrudService<TestRepository, Test, Long> implements TestService {

@Autowired
public TestServiceImpl(TestRepository repository) {
super(repository);
}

@Override
@Transactional("companyManagement")
public void insert(Test user) {
super.insert(user);
}

@Override
@Transactional("companyManagement")
public void updateIgnore(Test user) {
super.updateIgnore(user);
}
}

应用程序.yml:

server:
port: 8080
session:
timeout: -1

api:
db:
userManagement:
version: 0.0.17
url: jdbc:postgresql://localhost:3333/UM?autoReconnect=true&useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=UTF-8
username: ******
password: ******
driver-class-name: org.postgresql.Driver
minIdle: 0
maxIdle: 10
maxActive: 50
maxWait: 6000
testOnBorrow: true
validationQuery: SELECT 1
timeBetweenEvictionRunsMillis: 1800000
numTestsPerEvictionRun: 50
minEvictableIdleTimeMillis: 10
testWhileIdle: true
companyManagement:
version: 0.0.17
url: jdbc:postgresql://localhost:3334/CM?autoReconnect=true&useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=UTF-8
username: ******
password: ******
driver-class-name: org.postgresql.Driver
minIdle: 0
maxIdle: 10
maxActive: 50
maxWait: 6000
testOnBorrow: true
validationQuery: SELECT 1
timeBetweenEvictionRunsMillis: 1800000
numTestsPerEvictionRun: 50
minEvictableIdleTimeMillis: 10
testWhileIdle: true

结果

Fri Nov 03 17:18:46 ICT 2017
There was an unexpected error (type=Internal Server Error, status=500).
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: relation "cm_test" does not exist Position: 13 ### The error may involve com.domain.api.companyManagement.domain.Test._insert-Inline ### The error occurred while setting parameters ### SQL: insert into "cm_test"("name") values(?) ### Cause: org.postgresql.util.PSQLException: ERROR: relation "cm_test" does not exist Position: 13 ; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: relation "cm_test" does not exist Position: 13

此存储库应尝试插入第二个数据源,但它尝试使用第一个(主要)数据源。

复制

网址:https://github.com/kopax/spring-data-mybatis-test您需要使用 docker-compose up -d 或在没有 docker 的情况下手动启动两个数据库。

您是如何设法使用多个 DataSource 的?

最佳答案

在使用两个数据库的情况下,应该创建和指定单独的 TransactionManager-s

@Bean(name = "companyTransactionManager")
public PlatformTransactionManager transactionManager()
{
return new DataSourceTransactionManager(dataSource());
}

角色服务实现

 @Override
@Transactional("companyTransactionManager", readOnly = true)
public Role getByName(String name) {

关于java - spring-data 多数据源配置运行但插入错误的数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47094268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com