Home > Back-end >  Spring @ Transactional throws an exception, when will the rollback?
Spring @ Transactional throws an exception, when will the rollback?

Time:09-22

Today try to transaction rollback and encounter a problem:
In my controller try a method, this method performs a statements with @ Transactional annotation, and an exception is thrown, the controller in the catch this exception, from the console output into the content of the ,
I feel should be inserted into the statement is rolled back, content to be deleted, but the result is: the database is rolled back, and did the output content is inserted,
Want to ask a great god, and what is this principle?
Controller:
 @ RequestMapping ("/regist ") 
Public ModelAndView regist (it request, User User) throws the Exception {
If (checkParams (new String [] {user. GetUsername (), the user, getPassword ()})) {
//TODO @ Transaction check if the account has already exist
Try {
UserService. SaveUser (user);
} the catch (Exception e) {
//TODO Auto - generated the catch block,
System. The out. Println (" regist "+ um participant etMessage ());
Request. SetAttribute (" go ", um participant etMessage ());
}
Request. SetAttribute (" username ", the user. The getUsername ());
Request. SetAttribute (" password ", user. GetPassword ());
System.out.println(user);
Return new ModelAndView (" succ ");
}
Return new ModelAndView (" regist ");
}

Service:
 @ Transactional (rollbackFor=Exception class) 
Public void saveUser User (User) throws the Exception {
If (the user!=null & amp; & User. GetId ()!=null) {
UserDao. UpdateUser (user);
} else {
//TODO find if there is a repetition, can also use SQL
Try {
List AllUser=userDao. GetUser ();
UserDao. InsertUser (user);
For (User _user: allUser) {
If (_user. GetUsername () equals (user. The getUsername ()))
Throw new Exception (" The account is exist!" );
}
} the catch (Exception e) {
Throw e;
}
}
}

The console output:
The User [userid=1, the username=1, the password=1]
RegistThe account is exist!
The User [userid=2, the username=1, the password=2]
Database:

CodePudding user response:

The practice of the spring configuration will be thrown when RuntimeException rolled back, when you many times to the database operation, only successful when all will pass, otherwise will all rollback, personal understanding,

CodePudding user response:

Principle I don't know if

CodePudding user response:

Want to know the principle is to look at the underlying code , I am also a small white

CodePudding user response:

@ love1390700626 just don't know why the rollback is successful, insert data exists, should be deleted:

CodePudding user response:

The service logic no problem?

CodePudding user response:

The
reference 5 floor u011318721 response:
@ love1390700626 just don't know why the rollback is successful, insert data exists, should be deleted ah


I don't understand!!!!!! First of all, you're gonna go wrong, it is wrong, you throw exceptions, then you the insert is not rolled back? The database is not itself has a data? How can delete the original? Just all transactions in the transaction control operation. But the transaction control cannot be deleted data before ah, how to save data or database?

CodePudding user response:

refer to 7th floor unclezh0730 response:
Quote: refer to fifth floor u011318721 response:

@ love1390700626 just don't know why the rollback is successful, insert data exists, should be deleted:


I don't understand!!!!!! First of all, you're gonna go wrong, it is wrong, you throw exceptions, then you the insert is not rolled back? The database is not itself has a data? How can delete the original? Just all transactions in the transaction control operation. But the transaction control cannot be deleted data before ah, how to save data or database?


If you say your database without original data, then you the thrown exception if conditions are not met, exception thrown out, the transaction will not roll back

CodePudding user response:

Like sping management affairs in the service layer cannot catch exceptions, you catch the exception will not be able to rollback, you remove the throw an exception that e affairs should be should not roll back

CodePudding user response:

refer to the eighth floor unclezh0730 response:
Quote: refer to 7th floor unclezh0730 response:

Quote: refer to the fifth floor u011318721 reply:

@ love1390700626 just don't know why the rollback is successful, insert data exists, should be deleted:


I don't understand!!!!!! First of all, you're gonna go wrong, it is wrong, you throw exceptions, then you the insert is not rolled back? The database is not itself has a data? How can delete the original? Just all transactions in the transaction control operation. But the transaction control cannot be deleted data before ah, how to save data or database?


If you say your database without original data, then the thrown exception if conditions are not met, you didn't throw out, the transaction will not roll back


It should be said that the exception thrown not exception, shall be the exception isn't what you throw, is the _user getxxx error

CodePudding user response:

The service method in capture RuntimeException and its subclasses exception will be rolled back

CodePudding user response:

Spring the default transaction only in the case of not being captured runtimeexcetpion rolled back, your saveUser (user) methods are you try... The catch, so cannot be rolled back,
Can choose to remove the try... Catch
 
@ Transactional (rollbackFor=Exception class)
Public void saveUser User (User) throws the Exception {
If (the user!=null & amp; & User. GetId ()!=null) {
UserDao. UpdateUser (user);
} else {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related