提交 2f5bf2e4 编写于 作者: T terrymanu

pull member up for AbstractConnectionAdapter.cachedConnections

上级 de89878b
......@@ -18,13 +18,16 @@
package com.dangdang.ddframe.rdb.sharding.jdbc.adapter;
import com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationConnection;
import lombok.Getter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* Adapter for {@code Connection}.
......@@ -33,6 +36,9 @@ import java.util.LinkedList;
*/
public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOperationConnection {
@Getter
private final Map<String, Connection> cachedConnections = new HashMap<>();
private boolean autoCommit = true;
private boolean readOnly = true;
......@@ -41,8 +47,6 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
private int transactionIsolation = TRANSACTION_READ_UNCOMMITTED;
protected abstract Collection<Connection> getCachedConnections() throws SQLException;
@Override
public final boolean getAutoCommit() throws SQLException {
return autoCommit;
......@@ -51,11 +55,11 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
@Override
public final void setAutoCommit(final boolean autoCommit) throws SQLException {
this.autoCommit = autoCommit;
if (getCachedConnections().isEmpty()) {
if (cachedConnections.isEmpty()) {
recordMethodInvocation(Connection.class, "setAutoCommit", new Class[] {boolean.class}, new Object[] {autoCommit});
return;
}
for (Connection each : getCachedConnections()) {
for (Connection each : cachedConnections.values()) {
each.setAutoCommit(autoCommit);
}
}
......@@ -63,7 +67,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
@Override
public final void commit() throws SQLException {
Collection<SQLException> exceptions = new LinkedList<>();
for (Connection each : getCachedConnections()) {
for (Connection each : cachedConnections.values()) {
try {
each.commit();
} catch (final SQLException ex) {
......@@ -76,7 +80,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
@Override
public final void rollback() throws SQLException {
Collection<SQLException> exceptions = new LinkedList<>();
for (Connection each : getCachedConnections()) {
for (Connection each : cachedConnections.values()) {
try {
each.rollback();
} catch (final SQLException ex) {
......@@ -90,7 +94,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
public void close() throws SQLException {
closed = true;
Collection<SQLException> exceptions = new LinkedList<>();
for (Connection each : getCachedConnections()) {
for (Connection each : cachedConnections.values()) {
try {
each.close();
} catch (final SQLException ex) {
......@@ -113,11 +117,11 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
@Override
public final void setReadOnly(final boolean readOnly) throws SQLException {
this.readOnly = readOnly;
if (getCachedConnections().isEmpty()) {
if (cachedConnections.isEmpty()) {
recordMethodInvocation(Connection.class, "setReadOnly", new Class[] {boolean.class}, new Object[] {readOnly});
return;
}
for (Connection each : getCachedConnections()) {
for (Connection each : cachedConnections.values()) {
each.setReadOnly(readOnly);
}
}
......@@ -130,11 +134,11 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
@Override
public final void setTransactionIsolation(final int level) throws SQLException {
transactionIsolation = level;
if (getCachedConnections().isEmpty()) {
if (cachedConnections.isEmpty()) {
recordMethodInvocation(Connection.class, "setTransactionIsolation", new Class[] {int.class}, new Object[] {level});
return;
}
for (Connection each : getCachedConnections()) {
for (Connection each : cachedConnections.values()) {
each.setTransactionIsolation(level);
}
}
......
......@@ -32,7 +32,6 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
......@@ -47,8 +46,6 @@ public final class MasterSlaveConnection extends AbstractConnectionAdapter {
private final MasterSlaveDataSource masterSlaveDataSource;
private final Map<String, Connection> cachedConnections = new HashMap<>();
/**
* Get database connections via SQL.
*
......@@ -63,12 +60,12 @@ public final class MasterSlaveConnection extends AbstractConnectionAdapter {
Collection<Connection> result = new LinkedList<>();
for (Entry<String, DataSource> each : dataSources.entrySet()) {
String dataSourceName = each.getKey();
if (cachedConnections.containsKey(dataSourceName)) {
result.add(cachedConnections.get(dataSourceName));
if (getCachedConnections().containsKey(dataSourceName)) {
result.add(getCachedConnections().get(dataSourceName));
continue;
}
Connection connection = each.getValue().getConnection();
cachedConnections.put(dataSourceName, connection);
getCachedConnections().put(dataSourceName, connection);
result.add(connection);
replayMethodsInvocation(connection);
......@@ -126,11 +123,6 @@ public final class MasterSlaveConnection extends AbstractConnectionAdapter {
return new MasterSlavePreparedStatement(this, sql, columnNames);
}
@Override
public Collection<Connection> getCachedConnections() throws SQLException {
return cachedConnections.values();
}
@Override
public void close() throws SQLException {
HintManagerHolder.clear();
......
......@@ -37,9 +37,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* Connection that support sharding.
......@@ -53,8 +51,6 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
@Getter
private final ShardingContext shardingContext;
private final Map<String, Connection> cachedConnections = new HashMap<>();
/**
* Get all database connections via data source name.
*
......@@ -87,8 +83,8 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
* @throws SQLException SQL exception
*/
public Connection getConnection(final String dataSourceName, final SQLType sqlType) throws SQLException {
if (cachedConnections.containsKey(dataSourceName)) {
return cachedConnections.get(dataSourceName);
if (getCachedConnections().containsKey(dataSourceName)) {
return getCachedConnections().get(dataSourceName);
}
DataSource dataSource = shardingContext.getShardingRule().getDataSourceRule().getDataSource(dataSourceName);
Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
......@@ -96,15 +92,15 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
if (dataSource instanceof MasterSlaveDataSource) {
NamedDataSource namedDataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType);
realDataSourceName = namedDataSource.getName();
if (cachedConnections.containsKey(realDataSourceName)) {
return cachedConnections.get(realDataSourceName);
if (getCachedConnections().containsKey(realDataSourceName)) {
return getCachedConnections().get(realDataSourceName);
}
dataSource = namedDataSource.getDataSource();
} else {
realDataSourceName = dataSourceName;
}
Connection result = dataSource.getConnection();
cachedConnections.put(realDataSourceName, result);
getCachedConnections().put(realDataSourceName, result);
replayMethodsInvocation(result);
return result;
}
......@@ -115,7 +111,7 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
* @param connection to be released connection
*/
public void release(final Connection connection) {
cachedConnections.values().remove(connection);
getCachedConnections().values().remove(connection);
try {
connection.close();
} catch (final SQLException ignored) {
......@@ -172,11 +168,6 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
return new ShardingStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability);
}
@Override
public Collection<Connection> getCachedConnections() throws SQLException {
return cachedConnections.values();
}
@Override
public void close() throws SQLException {
HintManagerHolder.clear();
......
......@@ -56,7 +56,7 @@ public final class ConnectionAdapterTest extends AbstractShardingJDBCDatabaseAnd
private void assertAutoCommit(final ShardingConnection actual, final boolean autoCommit) throws SQLException {
assertThat(actual.getAutoCommit(), is(autoCommit));
assertThat(actual.getCachedConnections().size(), is(2));
for (Connection each : actual.getCachedConnections()) {
for (Connection each : actual.getCachedConnections().values()) {
assertThat(each.getAutoCommit(), is(autoCommit));
}
}
......@@ -94,7 +94,7 @@ public final class ConnectionAdapterTest extends AbstractShardingJDBCDatabaseAnd
private void assertClose(final ShardingConnection actual, final boolean closed) throws SQLException {
assertThat(actual.isClosed(), is(closed));
assertThat(actual.getCachedConnections().size(), is(2));
for (Connection each : actual.getCachedConnections()) {
for (Connection each : actual.getCachedConnections().values()) {
assertThat(each.isClosed(), is(closed));
}
}
......@@ -116,7 +116,7 @@ public final class ConnectionAdapterTest extends AbstractShardingJDBCDatabaseAnd
private void assertReadOnly(final ShardingConnection actual, final boolean readOnly, final DatabaseType type) throws SQLException {
assertThat(actual.isReadOnly(), is(readOnly));
assertThat(actual.getCachedConnections().size(), is(2));
for (Connection each : actual.getCachedConnections()) {
for (Connection each : actual.getCachedConnections().values()) {
// H2数据库未实现setReadOnly方法
if (DatabaseType.H2 == type) {
assertFalse(each.isReadOnly());
......@@ -143,7 +143,7 @@ public final class ConnectionAdapterTest extends AbstractShardingJDBCDatabaseAnd
private void assertTransactionIsolation(final ShardingConnection actual, final int transactionIsolation) throws SQLException {
assertThat(actual.getTransactionIsolation(), is(transactionIsolation));
assertThat(actual.getCachedConnections().size(), is(2));
for (Connection each : actual.getCachedConnections()) {
for (Connection each : actual.getCachedConnections().values()) {
assertThat(each.getTransactionIsolation(), is(transactionIsolation));
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册