"Java EE Learning Day 77th" "Data acquisition system Nineth Day" "Using spring to implement the answer level library" "Unresolved issues: Sub-Library query problems"

Source: Internet
Author: User



Previously said, if the amount of data to be stored in a database is relatively small, but one table stores more data, such as log table, it is necessary to consider the table storage; But what if the overall storage capacity of a database is relatively large? At this point, we need to consider the library, is to build multiple databases to save data. Here, for example, even if the survey is not many, but the number of people involved in the survey is very large, so the amount of data to be saved is very big, how to save the answer in a rule to a different database is the problem that needs to be considered now (query the problem of the library is not resolved, first archived ).



One, the method of the library



The Sub-Library is divided into two types: horizontal sub-Library and vertical sub-Library.



(1) Horizontal sub-Library



The database is isomorphic, but the data is stored in a different range. For example, I'll use the horizontal library method to save the answer to a different database. Two databases have answer tables, and the fields and constraints are exactly the same, the difference between the two is only the data saved, such a library method is the level of the library.



(2) Vertical sub-Library



The structure between the database and the database is not the same, such as a database to hold a module function, each module is relatively strong independence. And the volume is relatively large.



Ii. steps to achieve the library of answers: A case study of 2 databases



1. Create a second database Lsn_surveypark1



2. Configure the data source



Since the data source has been previously configured, it is only necessary to inherit the previous data source directly and modify the URL address.


1 <!-Configure data source (main library)->
 2 <bean id = "dateSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
 3 <property name = "driverClass" value = "$ {jdbc.driverclass}"> </ property>
 4 <property name = "jdbcUrl" value = "$ {jdbc.url}"> </ property>
 5 <property name = "user" value = "$ {jdbc.username}"> </ property>
 6 <property name = "password" value = "$ {jdbc.password}"> </ property>
 7
 8 <!-Configure c3p0's own parameters->
 9 <property name = "maxPoolSize" value = "$ {c3p0.pool.maxsize}"> </ property>
10 <property name = "minPoolSize" value = "$ {c3p0.pool.minsize}"> </ property>
11 <property name = "initialPoolSize" value = "$ {c3p0.pool.initsieze}"> </ property>
12 <property name = "acquireIncrement" value = "$ {c3p0.pool.increment}"> </ property>
13 </ bean>
14 <!-(From the library) In order to implement the function of sub-library, a data source must be configured for each database. The special attribute of package inheritance is used here. The dataSource is inherited using the parent attribute.->
15 <bean id = "dataSource1" class = "com.mchange.v2.c3p0.ComboPooledDataSource"
16 parent = "dateSource">
17 <property name = "jdbcUrl" value = "jdbc: mysql: // localhost: 3306 / lsn_surveypark1"> </ property>
18 </ bean> 


3. Configure the data source router



The data source router will decide which data source to use based on the policy.


1 <!-Configure data source router->
  2 <bean id = "dataSource_router" class = "com.kdyzm.datasource.SurveyparkDatasourceRouter">
  3 <property name = "targetDataSources">
  4 <map>
  5 <!-If the id is even, save it to the main library->
  6 <entry key = "even" value-ref = "dateSource"> </ entry>
  7 <!-If the id is odd, save it to the library->
  8 <entry key = "odd" value-ref = "dataSource1"> </ entry>
  9 </ map>
10 </ property>
11 <!-If the above rules are not met, use the default data source directly->
12 <property name = "defaultTargetDataSource" ref = "dateSource"> </ property>
13 </ bean> 


The policy here encapsulates the surveyparkdatasourcerouter in a class, The class must inherit the Org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource abstract class and override the Determinecurrentlookupkey method to determine the policy.



4. Custom routed data Source policy



The custom method is to inherit the Org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource class and override the abstract method.


1 package com.kdyzm.datasource;
 2 
 3 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 4
 5 import com.kdyzm.domain.Survey;
 6
 7 / **
 8 * Custom data source router
 9 * There is a default implementation class that routes data by propagating attributes.
10 * @author kdyzm
11 *
12 * /
13 public class SurveyparkDatasourceRouter extends AbstractRoutingDataSource {
14
15 / **
16 * This method actually determines a strategy for where data is stored
17 * Use the id attribute to determine here
18 * If the answer id is even, I want to store it in the answer table (main table) in the lsn_surveypark database
19 * If the answer id is odd, store it in the answer table (from the table) in the lsn_surveypark1 database
20 * /
21 @Override
22 protected Object determineCurrentLookupKey () {
23 SurveyToken surveyToken = SurveyToken.getSurveyToken ();
24 if (surveyToken! = Null) {
25 Survey survey = surveyToken.getSurvey ();
26 int surveyId = survey.getSurveyId ();
27 System.out.println ("Survey object is not empty, the value is:" + surveyId);
28 / **
29 * Must be unbound here
30 * If you do not unbind here, the log will be written to the lsn_surveypark1 database.
31 * Because there is no log table in the lsn_surveypark1 database, an error will be reported
32 * /
33 SurveyToken.unbind ();
34 return (surveyId% 2) == 0? "Even": "odd"; // If it is even, it returns the even string, if it is odd, it returns the odd string
35}
36 System.out.println ("survey object is empty");
37 return null;
38}
39
40} 


The policy of the routed data source is determined in the overridden method: If the survey ID is even, it is saved to the answer table in the main library Lsn_surveypark, and if it is odd, it is saved to the answer table from the library LSN_SURVEYPARK1.



The next step is to solve the problem of how to get the survey object, the yellow background part of the code is the key.



Note that if the survey object is empty, the default data source is used: the main library, which is determined by the configuration in the previous configuration file.



5. Use threadlocal to solve the problem of getting survey.



(1) Analysis of problems















"Java EE Learning Day 77th" "Data acquisition system Nineth Day" "Using spring to implement the answer level library" "Unresolved issues: Sub-Library query problems"


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.