Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

5.12 Multi-data source support


May 14, 2021 JFinal manual



ActiveRecordPlugin supports features such as multi-data source, multi-word, multi-cache, multi-transaction level, and each ActiveRecordPlugin can be configured independently of each other. In short, JFinal can use multiple data sources at the same time, and you can configure separate dialects, caches, transaction levels, and so on for across these data sources.


When using multiple data sources, you only need to specify one configName for each ActiveRecordPlugin, as follows:

public void configPlugin(Plugins me) {
// mysql data source
C3p0Plugin dsMysql = new C3p0Plugin(...); me.add(dsMysql);

// mysql A ctiveRecrodPlugin instance, and designated configName as mysql ActiveRecordPlugin arpMysql s new ActiveRecordPlugin ("mysql", dsMysql); me.add(arpMysql);
arpMysql.setCache( new EhCache()); a rpMysql.addMapping("user", User. class );

Oracle data source
C3p0Plugin dsOracle = new C3p0Plugin(...); me.add(dsOracle);

Oracle ActiveRecrodPlugin instance, and designate configName as oracle ActiveRecordPlugin arpOracle s new ActiveRecordPlugin ("oracle", ds Oracle); me.add(arpOracle);
arpOracle.setDialect( new OracleDialect()); a rpOracle.setTransactionLevel(8); a rpOracle.addMapping("blog", Blog. class );
}

The above code creates two ActiveRecordPlugin instances arpMysql and arpOrace, paying special attention to creating a real case while specifying its configName as mysql and oracle, respectively. a rpMysql and arpOracle map different Models and configure different dialects.


For the use of Model, different Models automatically find the ActiveRecrodPlugin instance to which they belong and the associated configuration for database operations. If you want the same Model to switch to a different data source, which is extremely convenient, this usage is ideal for situations where table in different data sources has the same table structure, and developers want to use the same Model to manipulate table with these same table structures, here's the sample code:

public void multiDsModel() {
Default use arp.addMapping (...) data source associated with the time
Blog blog = Blog. me .findById(123);

You can switch to another data source by calling the use method only once
blog.use("backupDatabase").save();
}

In the code in the example above, the blog.use ("backupDatabase") method switches the data source to backupDatabase and saves the data directly.


Note in particular: The use method is only required if the same Model wants to correspond to table for multiple data sources, and if the same Model is the only table that corresponds to one data source, the switching of data sources is automatic and does not require the use method.


For the use of Db-Record, the switching of data sources requires the db.use (cnfigName) method to get the database operation object, and then you can do the database operation, and here's an example of the code:

Query the user in the dsMysql data source
List<Record> users = Db.use("mysql"). find ("select * from user");
Query the dsOracle data source blog
List<Record> blogs = Db.use("oracle"). find ("select * from blog");

The above two lines of code, through configName for mysql, oracle to get their own database operation objects, and then you can use the database operation API in exactly the same way as a single data. In short, for Db s Record, multiple data sources require only one more call to Db.use than a single data source, and the SUBSEQUENT APIs are used exactly the same way.


Note that the first ActiveRecrodPlugin instance will become the primary data source, omitting configName. T he configuration in the activeRecrodPlugin instance created first will default to the primary configuration, and you can also set the master configuration by setting configName to DbKit.MAIN_CONFIG_NAME constant.