com.jquicker.persistent.rdb.tx.TransactionManager Maven / Gradle / Ivy
package com.jquicker.persistent.rdb.tx;
import java.lang.reflect.Method;
import java.sql.SQLException;
import com.jquicker.annotation.aop.Aspect;
import com.jquicker.annotation.tx.Transactional;
import com.jquicker.commons.log.JLogger;
import com.jquicker.persistent.rdb.ds.DataSourceManager;
/**
* @author OL
*/
@Aspect
public class TransactionManager implements TransactionAspect {
@Override
public void start() throws SQLException {
JLogger.debug("开启事务...");
DataSourceManager.getConnection();
}
@Override
public boolean isOpen(){
return DataSourceManager.isOpen();
}
@Override
public void commit() throws SQLException {
JLogger.debug("事务提交...");
DataSourceManager.commit();
}
@Override
public void rollback(Method method, Throwable throwable) throws SQLException {
Transactional transactional = method.getAnnotation(Transactional.class);
if(transactional == null) {
Class> clazz = method.getDeclaringClass();
transactional = clazz.getAnnotation(Transactional.class);
}
Class extends Throwable>[] ts = transactional.rollbackFor();
Class extends Throwable>[] nts = transactional.noRollbackFor();
boolean isRollback = false;
for (Class extends Throwable> clazz : ts) {
if(clazz.isAssignableFrom(throwable.getClass())) {
isRollback = true;
}
}
if(isRollback) {
for (Class extends Throwable> clazz : nts) {
if(clazz.isAssignableFrom(throwable.getClass())) {
isRollback = false;
}
}
}
if(isRollback) {
JLogger.debug("事务回滚...");
DataSourceManager.rollback();
}
}
@Override
public void close() throws SQLException {
JLogger.debug("关闭连接...");
DataSourceManager.close();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy