Skip to content

Commit 63cb5d7

Browse files
committed
支持多数据源切换功能
1 parent f1c6ecd commit 63cb5d7

6 files changed

Lines changed: 141 additions & 14 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.codingapi.tx.datasource.bean;
2+
3+
/**
4+
* create by lorne on 2017/12/7
5+
*/
6+
public class LCNDataSourceLocal {
7+
8+
private final static ThreadLocal<LCNDataSourceLocal> currentLocal = new ThreadLocal<LCNDataSourceLocal>();
9+
10+
public static LCNDataSourceLocal current() {
11+
return currentLocal.get();
12+
}
13+
14+
public static void setCurrent(LCNDataSourceLocal current) {
15+
currentLocal.set(current);
16+
}
17+
18+
private String key;
19+
20+
21+
public String getKey() {
22+
return key;
23+
}
24+
25+
public void setKey(String key) {
26+
this.key = key;
27+
}
28+
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.codingapi.tx.datasource.relational;
2+
3+
import com.codingapi.tx.datasource.ILCNResource;
4+
5+
import java.sql.Connection;
6+
7+
/**
8+
* create by lorne on 2017/12/7
9+
*/
10+
public interface LCNConnection extends Connection,ILCNResource<Connection> {
11+
}

tx-plugins-db/src/main/java/com/codingapi/tx/datasource/relational/LCNDBConnection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* create by lorne on 2017/7/29
2323
*/
2424

25-
public class LCNDBConnection extends AbstractTransactionThread implements Connection,ILCNResource<Connection> {
25+
public class LCNDBConnection extends AbstractTransactionThread implements LCNConnection {
2626

2727

2828
private Logger logger = LoggerFactory.getLogger(LCNDBConnection.class);
@@ -178,7 +178,9 @@ public TxTask getWaitTask() {
178178

179179
@Override
180180
public void setAutoCommit(boolean autoCommit) throws SQLException {
181-
connection.setAutoCommit(false);
181+
if(connection!=null) {
182+
connection.setAutoCommit(false);
183+
}
182184
}
183185

184186
@Override
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

tx-plugins-db/src/main/java/com/codingapi/tx/datasource/relational/LCNStartConnection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* create by lorne on 2017/12/1
1919
*/
20-
public class LCNStartConnection extends AbstractTransactionThread implements Connection,ILCNResource<Connection>{
20+
public class LCNStartConnection extends AbstractTransactionThread implements LCNConnection{
2121

2222
private Logger logger = LoggerFactory.getLogger(LCNStartConnection.class);
2323

@@ -151,7 +151,9 @@ protected void closeConnection() throws SQLException{
151151

152152
@Override
153153
public void setAutoCommit(boolean autoCommit) throws SQLException {
154-
connection.setAutoCommit(false);
154+
if(connection!=null) {
155+
connection.setAutoCommit(false);
156+
}
155157
}
156158

157159

tx-plugins-db/src/main/java/com/codingapi/tx/datasource/relational/LCNTransactionDataSource.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ public class LCNTransactionDataSource extends AbstractResourceProxy<Connection,L
2323
private org.slf4j.Logger logger = LoggerFactory.getLogger(LCNTransactionDataSource.class);
2424

2525

26-
private DataSource dataSource;
26+
protected DataSource dataSource;
2727

2828

29+
protected DataSource getDataSource() {
30+
return dataSource;
31+
}
2932

3033
public void setDataSource(DataSource dataSource) {
3134
this.dataSource = dataSource;
@@ -70,7 +73,7 @@ public Connection getConnection() throws SQLException {
7073

7174
Connection connection =(Connection)loadConnection();
7275
if(connection==null) {
73-
connection = initLCNConnection(dataSource.getConnection());
76+
connection = initLCNConnection(getDataSource().getConnection());
7477
if(connection==null){
7578
throw new SQLException("connection was overload");
7679
}
@@ -87,7 +90,7 @@ public Connection getConnection(String username, String password) throws SQLExce
8790

8891
Connection connection = (Connection)loadConnection();
8992
if(connection==null) {
90-
connection = initLCNConnection(dataSource.getConnection(username, password));
93+
connection = initLCNConnection(getDataSource().getConnection(username, password));
9194
if(connection==null){
9295
throw new SQLException("connection was overload");
9396
}
@@ -102,36 +105,36 @@ public Connection getConnection(String username, String password) throws SQLExce
102105

103106
@Override
104107
public PrintWriter getLogWriter() throws SQLException {
105-
return dataSource.getLogWriter();
108+
return getDataSource().getLogWriter();
106109
}
107110

108111
@Override
109112
public void setLogWriter(PrintWriter out) throws SQLException {
110-
dataSource.setLogWriter(out);
113+
getDataSource().setLogWriter(out);
111114
}
112115

113116
@Override
114117
public void setLoginTimeout(int seconds) throws SQLException {
115-
dataSource.setLoginTimeout(seconds);
118+
getDataSource().setLoginTimeout(seconds);
116119
}
117120

118121
@Override
119122
public int getLoginTimeout() throws SQLException {
120-
return dataSource.getLoginTimeout();
123+
return getDataSource().getLoginTimeout();
121124
}
122125

123126
@Override
124127
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
125-
return dataSource.getParentLogger();
128+
return getDataSource().getParentLogger();
126129
}
127130

128131
@Override
129132
public <T> T unwrap(Class<T> iface) throws SQLException {
130-
return dataSource.unwrap(iface);
133+
return getDataSource().unwrap(iface);
131134
}
132135

133136
@Override
134137
public boolean isWrapperFor(Class<?> iface) throws SQLException {
135-
return dataSource.isWrapperFor(iface);
138+
return getDataSource().isWrapperFor(iface);
136139
}
137140
}

0 commit comments

Comments
 (0)