Before using @Transaction
, my business code model was probably as follows:
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(def);
try {
studentMapper.insertOnestdent(student);
logger.info("Insert one record successfully: " + student.toString());
} catch (Exception ex) {
dataSourceTransactionManager.rollback(transactionStatus);
// ex.printStackTrace();
logger.error("Insert one record unsuccessfully: " + student.toString());
return false;
}
dataSourceTransactionManager.commit(transactionStatus);
return true;
Similar to the above, no matter whether my above database operation is executed successfully or fails, I can get feedback in the log.
However, the above approach will cause a lot of code duplication. Such a logging function can be implemented using the aop provided by spring. However, after reading the document, I found that there is a @Transaction
annotation. Using this annotation, The code can be simplified as follows:
@Transactional(rollbackFor = Exception.class)
public boolean insertOneStudent(Student student) {
studentMapper.insertOneStudent(student);
logger.info("Insert one student successfully" + student.toString());
return true;
}
In this way, when the database operation is executed successfully, I can successfully record it in the log. But in this case, how should I record it in the log when the insertion operation fails.
My initial idea now is to manually implement a dynamic reflection function similar to @Transaction
, so that when every method that affects the data modification in the database is executed, I will be dynamically reflectinginvoke()
Exception handling try catch
is performed in the method. But I don’t know if spring has such a built-in function?
p.s: I try to put all database operations and processing at the service
level, and don’t want to throw log records above.
insertOneStudent method entire try catch
Or implement @RestControllerAdvice and print exception logs in it
Like this:
Spring also provides aspect exception handling
Of course, if there is xml configuration, there will be annotation configuration. There are mainly the following comments
@Aspect declares aspect configuration
@Pointcut declares aspects
@AfterThrowing declaration processing method
Notes:
The class that handles exceptions does not need to implement any interface or inherit any class.
The method of handling exceptions needs to declare two parameters (you can also not declare it, the exception information cannot be obtained), for example
The joinPoint parameter can obtain the method parameter information that throws the exception. Needless to say, the Exception object.
The above content is for reference only. Different spring versions may cause the above instructions to be different from the actual situation. For standard instructions, please refer to Spring’s official documentation