OWL ITS + 탐지시스템(인터넷 진흥원)
jhjang
2021-10-14 d680ff9fa4298ad3c0cd12f5f9d87f6c51110480
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package kr.wisestone.owl.config.persistence.routing;
 
import com.google.common.collect.Lists;
import kr.wisestone.owl.config.persistence.aop.TransactionDefinitionInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
 
public class TransactionDefinitionRoutingDataSource extends AbstractRoutingDataSource {
 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private List<Object> dataSourceKeys;
 
    private final AtomicInteger currentIndex = new AtomicInteger();
 
    @Override
    public void setTargetDataSources(Map<Object, Object> targetDataSources) {
        super.setTargetDataSources(targetDataSources);
        this.dataSourceKeys = Lists.newArrayList(targetDataSources.keySet());
    }
 
    @Override
    protected Object determineCurrentLookupKey() {
        if (TransactionDefinitionInterceptor
                .isCurrentTransactionReadOnly()
                && !dataSourceKeys.isEmpty()) {
            int size = dataSourceKeys.size();
            Object key = dataSourceKeys.get(currentIndex.getAndIncrement() % size);
            return key;
        }
        return null;
    }
 
}