cn.vonce.sql.spring.datasource.TransactionalInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vonce-sqlbean-spring Show documentation
Show all versions of vonce-sqlbean-spring Show documentation
This is a tool that uses java object-oriented idea to write and generate SQL statements. On this basis,
it also implements lightweight plug-in support similar to JPA for mybatis and spring JDBC. A large number of
common SQL execution methods are built in plug-ins to improve development efficiency, reduce a large number of
SQL statement writing, and make developers focus on business code writing.
The newest version!
package cn.vonce.sql.spring.datasource;
import cn.vonce.sql.spring.annotation.DbTransactional;
import cn.vonce.sql.uitls.IdBuilder;
import cn.vonce.sql.uitls.StringUtil;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* 事务拦截器
*
* @author Jovi
* @email [email protected]
* @date 2022/12/14 11:34
*/
public class TransactionalInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
DbTransactional dbTransactional = methodInvocation.getThis().getClass().getAnnotation(DbTransactional.class);
if (methodInvocation.getMethod().isAnnotationPresent(DbTransactional.class)) {
dbTransactional = methodInvocation.getMethod().getAnnotation(DbTransactional.class);
}
Object result;
try {
String xid = TransactionalContextHolder.getXid();
//已经存在事务则加入事务并执行
if (StringUtil.isNotBlank(xid)) {
result = methodInvocation.proceed();
return result;
}
//当前没有事务则创建事务
else {
if (dbTransactional.readOnly()) {
ConnectionContextHolder.setReadOnly(true);
}
TransactionalContextHolder.setXid(IdBuilder.uuid());
result = methodInvocation.proceed();
//移除事务id
TransactionalContextHolder.clearXid();
//提交或回滚事务
ConnectionContextHolder.commitOrRollback(true);
}
} catch (Throwable e) {
//移除事务id
TransactionalContextHolder.clearXid();
Class extends Throwable>[] rollbackFor = dbTransactional.rollbackFor();
Class extends Throwable>[] noRollbackFor = dbTransactional.noRollbackFor();
boolean needRollback = false;
if (rollbackFor.length > 0) {
//遇到哪些异常回滚
for (Class extends Throwable> thr : rollbackFor) {
if (thr.isAssignableFrom(e.getClass())) {
needRollback = true;
break;
}
}
}
if (noRollbackFor.length > 0) {
//遇到哪些异常不回滚
for (Class extends Throwable> thr : noRollbackFor) {
if (thr.isAssignableFrom(e.getClass())) {
needRollback = false;
break;
}
}
}
if (rollbackFor.length == 0 && noRollbackFor.length == 0) {
//回滚事务
needRollback = true;
}
ConnectionContextHolder.commitOrRollback(!needRollback);
throw e;
}
return result;
}
}