CYQ. Data V5 database read/write splitting function introduction, cyq. data function Introduction

Source: Internet
Author: User

CYQ. Data V5 database read/write splitting function introduction, cyq. data function Introduction
Preface

I haven't provided an introduction to the new functions of this framework for many years. These new features have been updated silently over the years. We can see from the records on Nuget:

In the past few days, I have been reading some Java things. Apart from the fact that Java and. NET have a high degree of similarity, it is the beginning of Java Taiyuan, which is in urgent need of saving.

Then I went back to the. NET thinking and thought about how to implement read/write splitting control request switching in the framework.

I have been thinking over the past few months. I think the existing code changes to the framework may be too large, so I am not sure how to start it.

Recently, when I finally squatted down the toilet, I felt like my thoughts were coming, and then I gave it to me.

Three steps of database read/write separation 1: implement the master (read) slave (write) synchronization function

A: Copy and subscribe of MSSQL;

B: configure the ini for MySql Master-Slave.

C: Oracle components or third-party software.

For details about how to implement this function, use the read/write splitting as the keyword to Google.

2: three methods for read/write splitting control requests

A: Use code to control link Switching (commonly used ).

B: implemented through an intermediate third-party plug-in (abandonment of treatment required: Data Consistency ).

C: It is implemented through the database Driver (not to mention ).

3: Data Consistency

Because of the synchronization time difference between the master and slave databases, data consistency is inevitable. Therefore, the data is usually controlled on the Business Code.

Code solution master (write) from (read) Request allocation and Data Consistency 1: read/write request allocation principles

At the underlying layer: three methods of DbCommand:

Read allocation: ExecuteScalar and ExecuteReader

Allocate write: ExecuteNonQuery

2: read/write request appending principles:

A: The transaction is fully distributed to the master database.

B: when the transaction is not performed: the same link, as long as the primary database is generated, the subsequent reads are still linked to the primary database (initially solving the data consistency problem ).

3. Handle Data Consistency Issues:

Example scenario: After a user finishes writing a blog, the user jumps to the view Article Page (read ).

Obviously, if the Master/Slave database has not been synchronized, You need to prompt the user: the cache is being synchronized. Please wait for N seconds to refresh it?

A: In this scenario, it is not the same link, but the same person.

B: After the same person needs to write data to the primary database, all operations that must be performed within N seconds will remain in the primary database.

Therefore, I used three methods to determine whether the same person was the same:

A: SessionID.

B: Cookie

C: thread ID

Now, the read/write splitting function is complete.

Use of CYQ. Data read/write splitting

The following is an example of using read/write splitting in the ASP. NET Aries framework:

1: Download the source code of the ASP. NET Aries open-source framework from SVN.

Specific look: http://www.cnblogs.com/cyq1162/p/5487796.html

2: Make sure that the CYQ. Data version under packages is V5.6 or later. if the version is not new enough, search for cyqdata on Nuget to update it.

3: Set up the Master/Slave database subscription and replication function (MSSQL) in the database. The demo is on the same computer, and the Copy function is the slave (read) database.

4: Add a database link to Web. config.

PS description:

If the master database name is XXXX, the slave database configuration is: XXXX_Slave1, XXXX_Slave2, XXXX_Slave3 ...... xxxxxx_slaven

It's so easy to use, as long as the database connection is configured, everything will be OK.

After the interface is run

1: log on to the Chorme browser and edit the user information (the primary database operated at this time ):

2: After saving, you will jump to the List page: The primary database to be operated at this time:

3: refresh and view immediately in the FireFox browser: read from the database. The master and slave databases are not synchronized yet, so the old data is still used.

Hidden skills:

By configuring XXXX_Bak, when the master database fails, it will automatically switch to the backup database.

Therefore, the architecture is changed to 1 master + 1 slave + N slave mode.

Summary

With this framework, you will find that there is no need to modify any line of business code. You only need to configure the config file with N slave data links, such as database read/write splitting, it's yours now.

Don't ask me why I haven't promoted this framework in a very low profile over the past few years. It's just a little bit of a bit, but it doesn't matter. It's best to use a framework that only belongs to a few people.

The good news is that, starting with V5.5.6.1, MSSQL databases are free of charge and no authorization is required.

For messages that are free to use MSSQL, just like one!

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.