Handling Unicode Encoding Issues and Database Transaction Rollback in Java Services
The article explains a character‑encoding pitfall caused by a Python middle‑layer converting parameters to Unicode, provides a Java utility to decode escaped Unicode strings, and demonstrates how to use Spring's @Transactional annotation with rollbackFor to ensure database operations are rolled back on errors.
During recent integration testing the author encountered a pitfall related to character encoding and learned about database transaction rollback.
The problem arose because a Python middle‑layer service automatically converted incoming request parameters to Unicode strings, turning the original Chinese text "测试" into its escaped form "\u6d4b\u8bd5" (UTF‑8), which caused confusion when the Java backend received the data.
When bypassing the middle layer and sending requests directly from the front end, the parameters appear correctly, confirming the issue lies in the middle layer's handling.
To resolve the Unicode conversion issue, the following Java method can be used to transform escaped Unicode sequences back to readable strings:
/**
* 处理Unicode码转成utf-8
*
* @param str
* @return
*/
public static String unicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
String group = matcher.group(2);
ch = (char) Integer.parseInt(group, 16);
String group1 = matcher.group(1);
str = str.replace(group1, ch + Constant.EMPTY);
}
return str;
}For database rollback, the author adds a Spring @Transactional annotation with rollbackFor = Exception.class to the service class, ensuring that if any step in a multi‑step transaction fails, all previous database operations are rolled back.
Example implementation of a user‑addition method with transaction handling and custom exception throwing:
@Override
public int addUser(EditUserBean user) {
int add = testUserMapper.addUser(user);
if (add == 1) {
TestUserCheckBean userCheckBean = new TestUserCheckBean();
userCheckBean.copyFrom(user);
TestUserCheckBean bean = new TestUserCheckBean();
int i = updateUserStatus(userCheckBean);
if (i != 1 || StringUtils.isEmpty(userCheckBean.getCertificate())) {
UserStatusException.fail(TestUserCode.CHECK_FAIL.getDesc());
}
} else {
UserStatusException.fail(TestUserCode.ADD_USER_FAIL.getDesc());
}
return user.getId();
}This approach allows the service to roll back both the newly created user record and any related entries when an error such as a failed association occurs, providing a robust error‑handling mechanism.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
