Commit 180f0cce authored by 马晓光's avatar 马晓光
Browse files

fix connection leak

parent 52a34cab
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import io.shardingsphere.core.constant.ConnectionMode;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.hint.HintManagerHolder;
import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager;
import io.shardingsphere.shardingjdbc.jdbc.adapter.executor.ForceExecuteCallback;
@@ -56,6 +57,7 @@ import java.util.Map.Entry;
 * @author zhangliang
 * @author panjuan
 * @author zhaojun
 * @author maxiaoguang
 */
@Getter
public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOperationConnection {
@@ -152,7 +154,14 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
    private List<Connection> createConnections(final DataSource dataSource, final int connectionSize) throws SQLException {
        List<Connection> result = new ArrayList<>(connectionSize);
        for (int i = 0; i < connectionSize; i++) {
            try {
                result.add(createConnection(dataSource));
            } catch (final SQLException ex) {
                for (Connection each : result) {
                    each.close();
                }
                throw new ShardingException(String.format("Could't get %d connections one time, partition succeed connection(%d) have released!", connectionSize, result.size()));
            }
        }
        return result;
    }
+6 −10
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import java.util.Map.Entry;
 * @author zhaojun
 * @author zhangliang
 * @author panjuan
 * @author maxiaoguang
 */
@Slf4j
@NoArgsConstructor
@@ -125,24 +126,19 @@ public final class JDBCBackendDataSource implements BackendDataSource, AutoClose
    }
    
    private List<Connection> createConnections(final DataSource dataSource, final int connectionSize) throws SQLException {
        boolean hasException = false;
        List<Connection> result = new ArrayList<>(connectionSize);
        for (int i = 0; i < connectionSize; i++) {
            try {
                result.add(dataSource.getConnection());
            } catch (final SQLException ignored) {
                hasException = true;
            }
        }
        if (hasException) {
                for (Connection each : result) {
                    each.close();
                }
                throw new ShardingException(String.format("Could't get %d connections one time, partition succeed connection(%d) have released!", connectionSize, result.size()));
        } else {
            return result;
            }
        }
        return result;
    }
    
    @Override
    public void close() {