Skip to content

Latest commit

 

History

History

section-14-dynamic-datasource

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

动态数据源

这是一个并不常见的需求的实现方案,记录在此为了以后再遇到时查找方便。

需求

整个项目中有一个主数据库(肯定存在)和若干可以在主库的某个表内配置的数据库组成。例如:每个客户有一个单独的数据库,客户数据存在客户数据库内。

主数据库:main_db

若干客户数据库,在main_db.customer内记录

每个请求默认使用客户的数据库,请求会在request.header中增加一个customerId来标志是哪个客户。

数据库

main_db内有一个表

create table customer(
    id serial primary key,

    dataSourceClassName varchar(100),
    dataSourceUser varchar(50),
    dataSourcePassword varchar(50),
    dataSourceDataBaseName varchar(50),
    dataSourcePortNumber int,
    dataSourceServerName varchar(50)
)

insert into customer(dataSourceClassName, dataSourceUser, dataSourcePassword, dataSourceDataBaseName, dataSourcePortNumber, dataSourceServerName)
    values('org.postgresql.ds.PGSimpleDataSource', 'pgdbo', '', 'tvseries', 5432, '127.0.0.1');
    

上表中登记的tvseries数据库,和其他章节的数据库相同。

代码说明

测试

使用浏览器直接访问,因为没有传递request header customerId会出现500错误。

使用curl,传递了customerId的header,且值在customer表内存在则不会出错了。

curl -H "customerId:1" http://localhost:8080/tvseries/