Multiple data sources support


ActiveRecordPlugin can support multiple data sources, multiple dialects, multiple caches, multiple transaction levels and other features at the same time. Each ActiveRecordPlugin can be configured independently of each other. In short, JFinal can use multiple data sources at the same time, and can configure independent dialects, caches, transaction levels, etc. for these multiple data sources.


When using multiple data sources, you only need to specify a configName for each ActiveRecordPlugin. The following is a code example:


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

// mysql ActiveRecrodPlugin instance, and specify configName as mysql ActiveRecordPlugin arpMysql = new ActiveRecordPlugin("mysql", dsMysql); me .add(arpMysql);
arpMysql.setCache(new EhCache()); arpMysql.addMapping("user", User.class);

// oracle data source
C3p0Plugin dsOracle = new C3p0Plugin(…); me.add(dsOracle);

// oracle ActiveRecordPlugin instance, and specify configName as oracle ActiveRecordPlugin arpOracle = new ActiveRecordPlugin("oracle", dsOracle); me.add(arpOracle);
arpOracle.setDialect(new OracleDialect()); arpOracle.setTransactionLevel(8); arpOracle .addMapping("blog", Blog.class);
}

The above code creates two ActiveRecordPlugin instances arpMysql and arpOrace. Pay special attention to the creation of instances. At the same time, specify their configName as mysql and oracle respectively. arpMysql and arpOracle respectively map different Models and configure different dialects.



For the use of Model, different Models will automatically find their ActiveRecrodPlugin instance and related configuration for database operations. If you want the same Model to be switched to different data sources, it is extremely convenient. This usage is very suitable for tables in different data sources that have the same table structure. Developers want to use the same Model to operate these same table structures. table, the following is the sample code:

public void multiDsModel() {
// Associated by default when using arp.addMapping(...) Data source
Blog blog = Blog.me.findById(123);

// You only need to call the use method once to switch to another data source
blog.use("backupDatabase").save();
}

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


Special note: You only need to use the use method when the same Model wants to correspond to tables from multiple data sources. If the same Model is unique Corresponding to a table of one data source, the data source switching is automatic without using the use method.


For the use of Db + Record, switching data sources requires using the Db.use(cnfigName) method to obtain the database operation object, and then you can perform database operations. The following is Code example:

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

The above two lines of code get their respective database operation objects for mysql and oracle through configName, and then they can be used as single data Exactly the same way to use the database operation API. In short, for Db + Record, multiple data sources only require one more call to Db.use(configName) than single data source, and the subsequent API usage is exactly the same.


Note that the first created ActiveRecrodPlugin instance will become the main data source, and configName can be omitted. The configuration in the ActiveRecrodPlugin instance created first will become the main configuration by default. In addition, the main configuration can also be set by setting configName to the DbKit.MAIN_CONFIG_NAME constant.