Spring中异常时的事务回滚(Transaction rollback when exception in Spring)
我正在学习Spring,我在Spring中遇到了一些问题。
这是我的代码
@Transactional(rollbackFor = Exception.class) public void createGroupStudent(Student A,Student B,String nameGroup){ try{ //create Group createGroup(nameGroup); //createMember createMember(A,B); }catch(Exception e){ logger.error(e.getMessage()); } } @Transactional(rollbackFor = Exception.class) public void createGroup(String nameGroup){ try{ repoGroup.save(nameGroup); }catch(Exception e){ logger.error(e.getMessage()); } } @Transactional(rollbackFor = Exception.class) public void createMember(Student A,Student B){ try{ // function will throw a kind of Exception involve to " error constraint sql oracle " . //It's my intended repoMember.save(A,B); }catch(Exception e){ logger.error(e.getMessage()); } }
问题是当函数
createMember()
抛出异常时,事务总是回滚为什么? 我无法理解发生了什么! 我添加了try,catch in each methods但是它没有用。虽然方法
createMember()
在保存到数据库时遇到麻烦(这里我使用函数saveAndFlush()
)。我知道它并且我捕获了该异常。 父事务createGroupStudent()
认为自己没有问题并且提交事务。 但是当再次提交方法时,createMember()
会中断并抛出异常。我认为方法createGroup()
不会回滚。 但实际上,该函数回滚,所有事务都回滚了吗? 发生了什么?。我正在使用atomikos交易。
非常感谢
I am learning Spring and I have some troubles with transaction in Spring.
Here is my code
@Transactional(rollbackFor = Exception.class) public void createGroupStudent(Student A,Student B,String nameGroup){ try{ //create Group createGroup(nameGroup); //createMember createMember(A,B); }catch(Exception e){ logger.error(e.getMessage()); } } @Transactional(rollbackFor = Exception.class) public void createGroup(String nameGroup){ try{ repoGroup.save(nameGroup); }catch(Exception e){ logger.error(e.getMessage()); } } @Transactional(rollbackFor = Exception.class) public void createMember(Student A,Student B){ try{ // function will throw a kind of Exception involve to " error constraint sql oracle " . //It's my intended repoMember.save(A,B); }catch(Exception e){ logger.error(e.getMessage()); } }
The problem is when function
createMember()
throws Exception, transaction alway rollback why? I can't understand what happened! I added try, catch in each method but it didn't work.Although method
createMember()
have trouble while saving to Database (Here i'm using functionsaveAndFlush()
).I know it and I catch that exception. Parent transactioncreateGroupStudent()
thinks itself is no problems and commit transaction . But when commit once again methodcreateMember()
will break and throw Exception.I think methodcreateGroup()
won't rollback. But in real, that function rollbacked,all of transactions were rollback? What happened?.I'm using atomikos transaction.
Thanks so much
原文:https://stackoverflow.com/questions/36759599
最满意答案
String x_ify(String input) { String output = ""; int start = 0; int count = 0; int nextL; while ((nextL = input.indexOf('L', start)) >= 0) { if (nextL > start) { output = output + input.substring(start, nextL); } ++count; for (int i = 0; i < count; ++i) { output = output + "x"; } start = nextL + 1; } if (start < input.length()) { output += input.substring(start); } return output; }
String x_ify(String input) { String output = ""; int start = 0; int count = 0; int nextL; while ((nextL = input.indexOf('L', start)) >= 0) { if (nextL > start) { output = output + input.substring(start, nextL); } ++count; for (int i = 0; i < count; ++i) { output = output + "x"; } start = nextL + 1; } if (start < input.length()) { output += input.substring(start); } return output; }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如果它始终是相同的字符,你可以使用: stringByReplacingOccurrencesOfString:withString: 如果在相同的位置是相同的字符串可以使用: stringByReplacingOccurrencesOfString:withString:options:range: 如果只是一个特定的位置,您可以使用: stringByReplacingCharactersInRange:withString: 文档资料: https : //developer.apple.com ...
-
public int GetNthIndex(string s, char t, int n) { int count = 0; for (int i = 0; i < s.Length; i++) { if (s[i] == t) { count++; if (count == n) { return i; } ...
-
String x_ify(String input) { String output = ""; int start = 0; int count = 0; int nextL; while ((nextL = input.indexOf('L', start)) >= 0) { if (nextL > start) { output = output + input.substring(start, nextL); ...
-
不需要正则表达式来解决您的问题,如果您使用的是Java8 +,则可以使用: String input = "Testing"; Map
result = Arrays.stream(input.split("")) .map(String::toLowerCase) .collect(Collectors.groupingBy(s -> s, LinkedHashMap::new, Collectors.counting())); 输出 {t=2 ... -
您可以在String.replace函数中使用正则表达式: var str = "name0, name1, name2"; str = str.replace(/name./g, 'name'); You can use regex with the String.replace function: var str = "name0, name1, name2"; str = str.replace(/name./g, 'name');
-
如果您想为此使用SQL,请使用以下逻辑创建一个函数 DECLARE @value VARCHAR(255) SET @value = 'Hello.Bye.Today.Yesterday' DECLARE @result VARCHAR(255) DECLARE @curChar CHAR(1) DECLARE @start INT, @end INT SET @start = 2 SET @end = LEN(@value) SET @result = SUBSTRING(@value, 1,1) ...
-
String str = "clyde"; str = str.Substring(0, str.Length - 1) + 'o'; 尝试了一些在线VB转换器 Dim str As String = "clyde" str = str.Substring(0, str.Length - 1) & "o"C String str = "clyde"; str = str.Substring(0, str.Length - 1) + 'o'; Tried some online VB converter ...
-
当你写 'R' + 'V' 这导致添加char的ascii代码的int和replacecable也必须是String。 你在找什么 System.out.println(Str2.replace("RV", "n")); 或者如果你只想使用char的话,你应该尝试一下 String replace = Str2.replace('R', 'n'); replace = Str2.replace('V', 'n'); System.out.println(r ...