Analysis and solution of mysql scrambled codes

  • 2020-05-15 02:16:52
  • OfStack

In the process of development, there is no way to avoid encountering disgusting scrambled codes, or 1 series problems caused by scrambled codes. Generally speaking, it has nothing to do with logic. That is to say, there is no problem with the direction of the whole system, and some bugs appear in some small places, thus causing the program to fail to run normally. Therefore, it is a very disgusting thing to have scrambled codes. Here is a brief introduction of 1 I encountered garble code problem and the process of solving the problem in the idea and the general operation, we should learn how to analyze the problem and then solve the problem, not just according to the operation of the Internet to once solve the difficulties at hand, "fish" and "fishing" is the difference between this.

Background:
To achieve the function is very simple, dom4J to read the XML file and then with the help of Spring and Hibernate to write the data to the MySql database (multi-table), of course, the whole process is completed by Spring to control the transaction once, there is an exception to roll back.
Phenomenon: an error is reported when writing data to the database at the point of 1/2. The general error message is: the record is not unique to 1.
Analysis and operation:
I have checked my code once and saved the data with HibernateTemplete in Spring. There should be no problem.

Observe the phenomenon again: it seems to be a problem of foreign key constraints, since the data is stuck every time a table with a foreign key is inserted.
Analysis and operation:
Since is the foreign key table problem, first should determine the content of the foreign key table is what, then manually change the isolation level of the database to see the data, as expected inside is chaos code!

Question: how does the garbled code get into the database?
Analysis and operation:
Either xml is garbled when it is read, or it becomes garbled when it is written to the database. Breakpoint debugging tests whether the code is garbled in the program.

Phenomenon: the printed information in eclipse has no garbled problem
Analysis and operation:
This means that there is no problem in the program 1 cut, is the problem of the database. So first change the database link string to specify the encoding, then change the database encoding, again breakpoint debugging.

Phenomenon: the database is still garbled code, but the form of garbled code changed, before all is "?" So now we have some messy 78 bad text.
Analysis and operation:
It is clear that the above series 1 changes to the encoding format worked, but not correctly. Check the encoding format of each item again, no problem was found
The problem has reached an impasse...
Ask for help: qiu put forward don't use the command window, too much trouble, use the third side client, so change to the third side client, strange is that the client shows no problem!
Analysis and operation:
There is no problem with the client command window, that means the database is no longer garbled code, garbled code may be the command line display problem. Set the command line to display characters, as expected, the display is normal! Test data import again --1 slice OK.

Reflection and summary:
Phenomenon: in fact, in the first time to modify the code when the problem has been solved, but because I did not let the program finish debugging when the break point so 1 straight think that the database is still garbled code.
Conclusion:
Instead of focusing on the error itself, let the program run every time you debug it, and simulate the context of the error.

Phenomenon: where encodings are involved, there are encodings for XML, read encodings for dom4j, database encodings, database connection string encodings, and command line window display encodings. Any one of these that doesn't notice the problem can't be solved (here you don't realize the last one).
Conclusion:
Global view means to grasp every control variable, from the beginning to the end, consciously out of the environment to make assumptions.

Related articles: