All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jquicker.persistent.rdb.tx.TransactionManager Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
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[] ts = transactional.rollbackFor();
		Class[] nts = transactional.noRollbackFor();
		
		boolean isRollback = false;
		for (Class clazz : ts) {
			if(clazz.isAssignableFrom(throwable.getClass())) {
				isRollback = true;
			}
		}
		if(isRollback) {
			for (Class 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