Home > Back-end > Why the price of Java throw exceptions is much larger than find a database
Why the price of Java throw exceptions is much larger than find a database
Time:11-22
When inserted into the database code, try to catch a DuplicateKeyException abnormalities, colleagues suggest first check the database first
CodePudding user response:
The need to see the actual business scenarios, basically see your business is how to insert data, if it is a front import, should first check the data? If other way to import, can consider to duplicate records skip or update? Because some business can not be thrown exception directly to business interruption, will ask only once, for example, direct throw exceptions will lead to the normal data will be lost, so the price is still the business and technology solutions for business services, simply discuss technical solution to have what meaning,
CodePudding user response:
First query is certainly soon don't have the same key to insert the same key is generating a new key, if you're not can be directly try catch key when there is no question of your operation, so if you have the same key to appear in the input into how do you make up for, and if there is a nested transaction you inserted the exception to capture someone else in time for me to call you inserted into the interface of his without you there is an error you turn anomaly captured it's transaction successful people will think you are successful
CodePudding user response:
Not throw exception code is big, But repeated injection, it should be the final out, try to avoid relying on the ability of the database, in the code to get rid of, If you don't support the only index database design, how to do?
CodePudding user response:
Is I didn't express clearly, is the database design of a field, called the ACCOUNT is the only index, in simple terms the front-end calls will transfer the ACCOUNT and PASSWORD, so I catch in the code DuplicateKeyException this exception, colleagues are suggested in the try catch first to check the front travels outside the ACCOUNT exists, if there is a direct return, don't need to try again in the catch
CodePudding user response:
Try to stop the JVM to rearrangement of the code, in the case of abnormal need to a snapshot of the stack, recording stack frame points to the name of the class, the method name... And so on, there is a performance impact
CodePudding user response:
When an exception occurs, the system will help address some things, it would take some performance, so in high performance scenarios, try not to an exception occurs (such as to determine the divisor is 0, is not 0 then in addition to the operation, can avoid throwing was 0 except exception).