|
| 1 | +package com.codingapi.tx.datasource.relational; |
| 2 | + |
| 3 | +import com.codingapi.tx.aop.bean.TxTransactionLocal; |
| 4 | +import com.codingapi.tx.datasource.bean.LCNDataSourceLocal; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import javax.sql.DataSource; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.concurrent.ConcurrentHashMap; |
| 12 | + |
| 13 | + |
| 14 | +/** |
| 15 | + * 关系型数据库动态代理连接池对象 |
| 16 | + * create by lorne on 2017/7/29 |
| 17 | + */ |
| 18 | + |
| 19 | +public class LCNDynamicTransactionDataSource extends LCNTransactionDataSource { |
| 20 | + |
| 21 | + |
| 22 | + private Logger logger = LoggerFactory.getLogger(LCNDynamicTransactionDataSource.class); |
| 23 | + |
| 24 | + |
| 25 | + private Map<String,DataSource> dataSourceMap = new ConcurrentHashMap<>(); |
| 26 | + |
| 27 | + |
| 28 | + private String getNowDataSourceKey(){ |
| 29 | + if(LCNDataSourceLocal.current()==null){ |
| 30 | + return "default"; |
| 31 | + } |
| 32 | + return LCNDataSourceLocal.current().getKey(); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean hasGroup(String group) { |
| 37 | + return super.hasGroup(getNowDataSourceKey()+group); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected Connection createLcnConnection(Connection connection, TxTransactionLocal txTransactionLocal) { |
| 42 | + nowCount++; |
| 43 | + if(txTransactionLocal.isHasStart()){ |
| 44 | + LCNStartConnection lcnStartConnection = new LCNStartConnection(connection,subNowCount); |
| 45 | + logger.info("get new start connection - > "+txTransactionLocal.getGroupId()); |
| 46 | + pools.put(getNowDataSourceKey()+txTransactionLocal.getGroupId(), lcnStartConnection); |
| 47 | + txTransactionLocal.setHasConnection(true); |
| 48 | + return lcnStartConnection; |
| 49 | + }else { |
| 50 | + LCNDBConnection lcn = new LCNDBConnection(connection, dataSourceService, subNowCount); |
| 51 | + logger.info("get new connection ->" + txTransactionLocal.getGroupId()); |
| 52 | + pools.put(getNowDataSourceKey()+txTransactionLocal.getGroupId(), lcn); |
| 53 | + txTransactionLocal.setHasConnection(true); |
| 54 | + return lcn; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public void addDataSource(String key, DataSource dataSource){ |
| 59 | + dataSourceMap.put(key,dataSource); |
| 60 | + logger.info("add datasource of "+key); |
| 61 | + } |
| 62 | + |
| 63 | + public void setDataSource(DataSource dataSource) { |
| 64 | + super.setDataSource(dataSource); |
| 65 | + addDataSource("default",dataSource); |
| 66 | + logger.info("load default datasource."); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected DataSource getDataSource() { |
| 71 | + logger.info("getDataSource--->"); |
| 72 | + if(LCNDataSourceLocal.current()==null){ |
| 73 | + return super.getDataSource(); |
| 74 | + }else{ |
| 75 | + String key = LCNDataSourceLocal.current().getKey(); |
| 76 | + logger.info("get datasource of "+key); |
| 77 | + return dataSourceMap.get(key); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments