1. What is the use of database middleware

One day, when you go to Sanya to play, you want to play surfing. Even if you don’t have a short amount of money, do you still have to buy your own equipment such as speedboats and skateboards to meet this few whims? Just rent one. This is actually what connection pooling does.

Database middleware can be understood as a secondary component with connection pooling capabilities, but more advanced than connection pooling, with many additional features. In addition to renting surfboards, you can also provide various services such as location recommendation and insurance.

From the information on the Internet, zdal should be considered semi-open source. It seems that it was open sourced before, but it was not prepared for maintenance afterwards, and then it was deleted. However, GitHub has been forked down a lot, and a random search is a piece. Currently, just the old version. At present, the zdal inside the ant seems to have been updated to zdal5, then we can’t see it.

The more complex the system, the greater the role of database middleware. For example, zdal provides a unified solution for data access layers, such as database sharding tables, result collection merge, SQL parsing, and dynamic switching of database Failover. Let’s take a look at what its internal implementation is.

As shown in the figure above, zdal has four important components:

Component diagrams can be a great help for the overall architecture and the understanding of the components and interconnections. A simplified version of the component drawing for a long time, there are many mistakes, but it is probably the meaning, hey, the basic skills to lose ~

You can see more clearly from the above figure:

In most cases, we use an ORM framework like MyBatis for database operations. In fact, whether it is ORM or otherwise, the application layer needs to configure the data source.

Therefore, the client exposed a JDBC compliant datasource data source, ZdalDataSource, to meet the requirements of framework configuration data sources such as application layer ORMs.

The ZdalDataSource#init() method is the core entry for configuration loading. init is responsible for loading the Spring configuration, initializing the data source according to the configuration, and creating a connection pool. At the same time, the correspondence between the logical table and the physical library is maintained for subsequent routing calls.

From the class diagram above and the two entry methods here, I can roughly understand the startup process of zdal configuration loading. Let’s take a closer look at how the rules of read-write separation and database sharding are loaded and how they work.

First, we need to have the relevant configuration of the data source, as shown in the following figure:

This XML configuration is initialized when the init method is called and resolves to a property of the ZdalConfig class. The main members of the ZdalConfig class are shown in the following code:

As you can see, the rules in the XML are parsed into xxxRules. Here is groupRules as an example, the same is true for Failover.

The next step is to initialize the data source by parsing the resulting zdalConfig:

The initForLoadBalance method is as follows:

As you can see, the DB selector is first built and then assigned to runtimeConfigHolder for runtime to fetch. When building the DB selector, in fact, all data sources are built according to two dimensions, that is, group_r and group_w contain 5 data sources, but their respective weights are different:

Taking delete as an example, updating and deleting is to write to the library:

getConnection takes the DBSelecter from the runtimeConfigHolder mentioned above and executes the execute method.

If it is read-related, then choose DBSelecter for _r, and if it is write-related, then choose DBSelecter for _W. So, how does executeUpdate0 perform read-write data source discrimination? In fact, this set of data sources is filtered according to the weight.

4. Summary

This article summarizes the components and loading processes related to Alibaba database middleware, and elaborates on the internal implementation of the most basic group read/write separation process. Saying that it is an analysis is actually to provide everyone with a reading idea, after all, the space is limited, if you are interested in middleware, you can Fork the code and read it according to the above logic.

When looking at source code, middleware such as Dubbo is actually easier to get started with, because they all rely on Spring to load JavaBeans. Therefore, the init, load methods exposed to Spring containers are good entry points. Personal thoughts, I hope to help everyone.

– EOF –

1. Dig three feet to solve the problem of Redis and MySQL data consistency

2. A quick start to sub-database sub-table middleware Sharding-JDBC (compulsory course)

3, a MySQL misoperation caused by the accident, “high availability” can not resist!

Got a harvest after reading this article? Please forward and share it with more people

Follow “ImportNew” to improve your Java skills

Likes and looks are the biggest support ❤️