Skip to content

Commit

Permalink
connectionMap => cachedConnections
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 2, 2017
1 parent 8b02997 commit a5cb635
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.dangdang.ddframe.rdb.sharding.jdbc.core.statement.MasterSlaveStatement;
import com.dangdang.ddframe.rdb.sharding.parsing.SQLJudgeEngine;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.sql.SQLStatement;
import com.google.common.base.Optional;
import lombok.RequiredArgsConstructor;

import javax.sql.DataSource;
Expand All @@ -50,7 +49,7 @@ public final class MasterSlaveConnection extends AbstractConnectionAdapter {

private final MasterSlaveDataSource masterSlaveDataSource;

private final Map<String, Connection> connectionMap = new HashMap<>();
private final Map<String, Connection> cachedConnections = new HashMap<>();

/**
* Get database connections via SQL.
Expand All @@ -67,24 +66,19 @@ public Collection<Connection> getConnection(final String sql) throws SQLExceptio
Collection<Connection> result = new LinkedList<>();
for (Entry<String, DataSource> each : dataSources.entrySet()) {
String dataSourceName = each.getKey();
Optional<Connection> cachedConnection = getCachedConnection(dataSourceName);
if (cachedConnection.isPresent()) {
result.add(cachedConnection.get());
if (cachedConnections.containsKey(dataSourceName)) {
result.add(cachedConnections.get(dataSourceName));
continue;
}
Connection connection = each.getValue().getConnection();
connectionMap.put(dataSourceName, connection);
cachedConnections.put(dataSourceName, connection);
result.add(connection);
replayMethodsInvocation(connection);

}
return result;
}

private Optional<Connection> getCachedConnection(final String dataSourceName) {
return Optional.fromNullable(connectionMap.get(dataSourceName));
}

@Override
public DatabaseMetaData getMetaData() throws SQLException {
return masterSlaveDataSource.getDataSource(SQLType.DML).getDataSource().getConnection().getMetaData();
Expand Down Expand Up @@ -137,7 +131,7 @@ public PreparedStatement prepareStatement(final String sql, final String[] colum

@Override
public Collection<Connection> getCachedConnections() throws SQLException {
return connectionMap.values();
return cachedConnections.values();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
@Getter
private final ShardingContext shardingContext;

private final Map<String, Connection> connectionMap = new HashMap<>();
private final Map<String, Connection> cachedConnections = new HashMap<>();

/**
* Get all database connections via data source name.
Expand Down Expand Up @@ -101,14 +101,14 @@ public Connection getConnection(final String dataSourceName, final SQLType sqlTy
realDataSourceName = dataSourceName;
}
Connection result = dataSource.getConnection();
connectionMap.put(realDataSourceName, result);
cachedConnections.put(realDataSourceName, result);
replayMethodsInvocation(result);
return result;
}

private Optional<Connection> getCachedConnection(final String dataSourceName, final SQLType sqlType) {
String key = connectionMap.containsKey(dataSourceName) ? dataSourceName : MasterSlaveDataSource.getDataSourceName(dataSourceName, sqlType);
return Optional.fromNullable(connectionMap.get(key));
String key = cachedConnections.containsKey(dataSourceName) ? dataSourceName : MasterSlaveDataSource.getDataSourceName(dataSourceName, sqlType);
return Optional.fromNullable(cachedConnections.get(key));
}

/**
Expand All @@ -117,7 +117,7 @@ private Optional<Connection> getCachedConnection(final String dataSourceName, fi
* @param connection to be released connection
*/
public void release(final Connection connection) {
connectionMap.values().remove(connection);
cachedConnections.values().remove(connection);
try {
connection.close();
} catch (final SQLException ignored) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public Statement createStatement(final int resultSetType, final int resultSetCon

@Override
public Collection<Connection> getCachedConnections() throws SQLException {
return connectionMap.values();
return cachedConnections.values();
}

@Override
Expand Down

0 comments on commit a5cb635

Please sign in to comment.