Data master key generation method

xiaoxiao2021-03-06  115

From: http://forum.javaeye.com/viewtopic.php?t=1071&postdays=0&postorder=asc&start=30

With regard to the main key generation, there are several views: 1. Use an integer, key value in memory; 2. Use an integer, save the key value to a table in the database; 3, use UUID; if you compare performance Sensitive, I suggest you use uuid, on the one hand, the generation of uuid needs to spend a certain time. On the other hand, the storage of UUID is more space than integers, and it is very unfavorable to performance; then should we use an integer as a primary key, answer Is it uncertain, to be analyzed according to the actual situation. Using the first solution, it has the highest efficiency, like IncrementGenerator in Hibernate, because Hibernate itself is cross-database platform, this way does not have to worry about the problem of cross-database platforms. But the first solution cannot be applied in a charter environment. In the second way, it is possible to apply this approach to the AUTONUMBER solution in JBoss, but this method also has a large performance problem. When the data is inserted, the conflict can be caused. So do you have a solution that is highly efficient and can be applied in a charting environment? The first scheme has the highest efficiency, but cannot be applied in a cluster environment, and the second middle scheme can be applied in the charting environment, but the efficiency is low. So how do we combine these two programs? How to combine it? Let's take a look at this program: We know that for the second solution, when you want to generate a primary key value, the system will read and update the table in the database, which is equivalent to a value process, each The generation of primary key values ​​will have at least once database connections, which is exactly the performance bottleneck. In order to solve this problem, we don't have a value at each "value", but take up 100 or more at a time. When you need the primary key value, the current value of the main key value is saved in memory. And the value count, when this count is reduced to 0, that is, the primary key value previously taken out is running, and then the 100 or more value is removed in the database. In this way, it greatly reduces the number of connected databases, reducing the possibility of conflicts, can greatly improve the performance issues of the second middle scheme, and can be applied in the charting environment. The value of this program is generally deployed in an EJB species, just like the autonumber in jboss, to perform transaction isolation, prevent the primary key update transaction and the application code mixed in the same transaction, when the application code failed transaction will roll To update the code, the primary key is also rolled back to the primary key generation mechanism confusion. Of course, this approach does not have no shortcomings, it should be noted that every time the system is turned off, there will be a certain key value. The size is related to each time it is worth it. This primary key generation mechanism is implemented in the Dudoj framework. Dudoj also implements a primary key generator, which is an agent class that automatically selection between the first scheme and the last scheme, has reached the best performance.

转载请注明原文地址:https://www.9cbs.com/read-125471.html

New Post(0)