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

com.github.antelopeframework.mybatis.shard.routing.DynamicRoutingDataSource Maven / Gradle / Ivy

There is a newer version: 1.1.5
Show newest version
package com.github.antelopeframework.mybatis.shard.routing;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import com.github.antelopeframework.mybatis.shard.ShardContext;

import lombok.extern.slf4j.Slf4j;

/**
 * 数据源路由实现,持有多数据源
 * 
 * 该数据源继承自Spring提供的AbstractRoutingDataSource,可以根据配置文件中的数据源key对多数据源进行动态切换,
 * 能够非常方便的实现数据源路由工作
 * 
 * 
* 多数据源事务的配置, 请参考:
* http://forum.spring.io/forum/spring-projects/data/jdbc/746453-problem-in-managing-transaction-with-abstractroutingdatasource
* http://www.vedantatree.com/2014/05/spring-routingdatasource-to-work-with.html * * @author yangzhi.yzh * */ @Slf4j public class DynamicRoutingDataSource extends AbstractRoutingDataSource { private Map targetDataSources; public Map getTargetDataSources() { return targetDataSources; } /** * {@inheritDoc} */ public void setTargetDataSources(Map targetDataSources) { super.setTargetDataSources(targetDataSources); this.targetDataSources = targetDataSources; } public DataSource getCurrentDataSource() { return super.determineTargetDataSource(); } @Override public Object determineCurrentLookupKey() { Object dataSourceKey = ShardContext.getDataSourceKey(); if (dataSourceKey != null && log.isInfoEnabled()) { log.info("switch DataSource. target={}", dataSourceKey); } return ShardContext.getDataSourceKey(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy