DataSource
Multiple data source connection
The JDBC persistence module supports multiple data source configuration by default. The following is a simple configuration to show how to connect multiple databases:
# 定义两个数据源分别用于连接MySQL和Oracle数据库,同时指定默认数据源为default(即MySQL数据库) ymp.configs.persistence.jdbc.ds_default_name=default ymp.configs.persistence.jdbc.ds_name_list=default|oracledb # 连接到MySQL数据库的数据源配置 ymp.configs.persistence.jdbc.ds.default.connection_url=jdbc:mysql://localhost:3306/mydb ymp.configs.persistence.jdbc.ds.default.username=root ymp.configs.persistence.jdbc.ds.default.password=123456 # 连接到Oracle数据库的数据源配置 ymp.configs.persistence.jdbc.ds.oracledb.connection_url=jdbc:oracle:thin:@localhost:1521:ORCL ymp.configs.persistence.jdbc.ds.oracledb.username=ORCL ymp.configs.persistence.jdbc.ds.oracledb.password=123456
As can be seen from the above configuration, when configuring different data sources, you only need to define a list of data source names, and then configure them one by one according to the list;
Connection pool configuration
The data source types provided by the JDBC persistence module are as follows:
- default: default data source adapter, directly connected to the database through DriverManager, recommended for testing only;
- c3p0: Data source adapter based on C3P0 connection pool;
- dbcp: Data source adapter based on DBCP connection pool;
- jndi: Data source adapter based on JNDI;
Just adjust the configuration of the corresponding data source name according to the actual situation, such as:
ymp.configs.persistence.jdbc.ds.default.adapter_class=dbcp
Configuration for dbcp and c3p0 connection pool file and content, please place the corresponding dbcp.properties or c3p0.properties file under the classpath root path of the project. For configuration content, please refer to the sample file in the JDBC persistence module open source project;
Of course, you can also It is implemented by itself through the IDataSourceAdapter interface. The framework provides an abstract encapsulation AbstractDataSourceAdapter class for the IDataSourceAdapter interface, which can be inherited directly;
Database connection holder (IConnectionHolder)
Use To record the original state of the real database connection object (Connection) and the corresponding relationship with the data source;