com.yunmel.syncretic.ds.DataSourceAspect.java Source code

Java tutorial

Introduction

Here is the source code for com.yunmel.syncretic.ds.DataSourceAspect.java

Source

/*
 * Copyright (c) 2016 yunmle.com(?).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.yunmel.syncretic.ds;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.yunmel.syncretic.annotation.DataSourceChange;
import com.yunmel.syncretic.exception.DataSourceAspectException;

/**
 * 
 * @description 
 *      {@link com.yunmel.commons.annotation.DataSourceChange}???
 * @author xuyq - chainisit@126.com
 * @since 1.0 - 2016715
 */
@Aspect
@Component
public class DataSourceAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceAspect.class);

    @Around("@annotation(dataSourceChange)")
    public Object doAround(ProceedingJoinPoint pjp, DataSourceChange dataSourceChange) {
        Object retVal = null;
        boolean selectedDataSource = false;
        try {
            if (null != dataSourceChange) {
                selectedDataSource = true;
                if (dataSourceChange.slave()) {
                    DynamicDataSource.useSlave();
                } else {
                    DynamicDataSource.useMaster();
                }
            }
            retVal = pjp.proceed();
        } catch (Throwable e) {
            LOGGER.warn("???", e);
            throw new DataSourceAspectException("???", e);
        } finally {
            if (selectedDataSource) {
                DynamicDataSource.reset();
            }
        }
        return retVal;
    }
}