Tôi đang sử dụng SpringBoot, cùng với Hibernate là nhà cung cấp kiên trì. Đối với đơn đăng ký của tôi, tôi bắt buộc phải chọn động giữa 2 DB.Spring AbstractRoutingDataSource + Hibernate- Hbm2ddlSchemaUpdate được thực hiện chỉ trên mặc định DB
(For simplicity sake,
domain : localhost:8080 ---> hem1 DB
domain : 127.0.0.1:8080 ---> hem2 DB
)
Tiếp theo là việc thực hiện AbstractRoutingDB
public class MyRoutingDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
/*
* this is derived from threadlocal set by filter for each web
* request
*/
return SessionUtil.getDB();
}
}
Sau đây là cấu hình DB:
package com.hemant.basic.dataSource;
import java.beans.PropertyVetoException;
import java.util.HashMap;
import java.util.Map;
import javax.naming.ConfigurationException;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
public class DBConfig {
@Bean(name = "dataSource")
public DataSource dataSource() throws PropertyVetoException,
ConfigurationException {
MyRoutingDataSource routingDB = new MyRoutingDataSource();
Map<Object, Object> targetDataSources = datasourceList();
// hem1 is the default target DB
routingDB.setDefaultTargetDataSource(targetDataSources.get(1));
routingDB.setTargetDataSources(targetDataSources);
routingDB.afterPropertiesSet();
return routingDB;
}
private Map<Object, Object> datasourceList() throws PropertyVetoException,
ConfigurationException {
final Map<Object, Object> datasources = new HashMap<Object, Object>();
ComboPooledDataSource datasource = null;
for (int id = 1; id <= 2; id++) {
datasource = getDatasource(id);
datasources.put(id, datasource);
}
return datasources;
}
private ComboPooledDataSource getDatasource(int id)
throws PropertyVetoException, ConfigurationException {
ComboPooledDataSource datasource = new ComboPooledDataSource();
// set the connection pool properties
datasource.setJdbcUrl("jdbc:postgresql://localhost/hem" + id);
datasource.setUser("hemant");
datasource.setPassword("");
datasource.setDriverClass("org.postgresql.Driver");
datasource.setMaxPoolSize(30);
datasource.setInitialPoolSize(10);
datasource.setMinPoolSize(10);
return datasource;
}
}
thiết lập Cũng Sau đây là trong application.properties để cập nhật sơ đồ tự động là ON.
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
Vấn đề: khi tôi khởi động ứng dụng, cập nhật schema hbm2ddl chỉ được thực hiện trên hem1 (defaultTargetDb) nhưng không phải trên cơ sở dữ liệu mục tiêu khác
Sau đây là một phần của bản ghi khởi động
[main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
[main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000102: Fetching database metadata
[main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000396: Updating schema
[main] java.sql.DatabaseMetaData : HHH000262: Table not found: users
[main] java.sql.DatabaseMetaData : HHH000262: Table not found: users
[main] java.sql.DatabaseMetaData : HHH000262: Table not found: users
[main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000232: Schema update complete`enter code here`
NÀY CHỈ ĐƯỢC THỰC HIỆN CHO 1 DB.
** Sau này khi tôi thực hiện phần còn lại URL nói
GET localhost: 8080/người dùng - Kết quả được lấy thành công cho DB hem1 được cập nhật.
Nhưng khi 127.0.0.1:8080/users GET được truy cập, kể từ khi schema không được cập nhật/tạo ra, kết quả CNTT trong SQL Exception **
Làm thế nào chúng ta có thể đảm bảo rằng "update schema hbm2ddl" được thực hiện trên tất cả các cơ sở dữ liệu đích của một AbstractRoutingDataSource