Commit 5cc4531c authored by terrymanu's avatar terrymanu
Browse files

move JDBCBackendHandler.getConnection => ConnectionManager

parent 4a3f1a58
Loading
Loading
Loading
Loading
+12 −15
Original line number Diff line number Diff line
@@ -15,23 +15,26 @@
 * </p>
 */

package io.shardingsphere.proxy.backend.common;
package io.shardingsphere.proxy.backend.common.jdbc;

import io.shardingsphere.proxy.backend.common.ProxyMode;
import io.shardingsphere.proxy.config.RuleRegistry;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Hold the connection when proxy mode is CONNECTION_STRICTLY.
 * Connection manager.
 *
 * @author zhaojun
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ProxyConnectionHolder {
public final class ConnectionManager {
    
    private static final ThreadLocal<Map<DataSource, Connection>> RESOURCE = new ThreadLocal<Map<DataSource, Connection>>() {
        
@@ -41,24 +44,18 @@ public final class ProxyConnectionHolder {
        }
    };
    
    /**
     * Set connection associate with datasource into Thread.
     *
     * @param dataSource DataSource
     * @param connection Connection
     */
    public static void setConnection(final DataSource dataSource, final Connection connection) {
        RESOURCE.get().put(dataSource, connection);
    }
    
    /**
     * Get connection of current thread datasource.
     *
     * @param dataSource Datasource
     * @return Connection
     * @throws SQLException SQL exception
     */
    public static Connection getConnection(final DataSource dataSource) {
        return RESOURCE.get().get(dataSource);
    public static Connection getConnection(final DataSource dataSource) throws SQLException {
        if (ProxyMode.MEMORY_STRICTLY == RuleRegistry.getInstance().getProxyMode()) {
            return dataSource.getConnection();
        }
        return RESOURCE.get().containsKey(dataSource) ? RESOURCE.get().get(dataSource) : RESOURCE.get().put(dataSource, dataSource.getConnection());
    }
    
    /**
+0 −16
Original line number Diff line number Diff line
@@ -31,8 +31,6 @@ import io.shardingsphere.core.routing.SQLRouteResult;
import io.shardingsphere.core.routing.SQLUnit;
import io.shardingsphere.core.routing.router.masterslave.MasterSlaveRouter;
import io.shardingsphere.proxy.backend.common.BackendHandler;
import io.shardingsphere.proxy.backend.common.ProxyConnectionHolder;
import io.shardingsphere.proxy.backend.common.ProxyMode;
import io.shardingsphere.proxy.backend.common.ResultList;
import io.shardingsphere.proxy.backend.resource.BaseJDBCResource;
import io.shardingsphere.proxy.config.RuleRegistry;
@@ -50,10 +48,8 @@ import io.shardingsphere.transaction.xa.AtomikosUserTransaction;
import lombok.Getter;
import lombok.Setter;

import javax.sql.DataSource;
import javax.transaction.Status;
import javax.transaction.SystemException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
@@ -260,16 +256,4 @@ public abstract class JDBCBackendHandler implements BackendHandler {
    }
    
    protected abstract DatabaseProtocolPacket newDatabaseProtocolPacket(int sequenceId, List<Object> data);
    
    protected final Connection getConnection(final DataSource dataSource) throws SQLException {
        if (ProxyMode.MEMORY_STRICTLY == ruleRegistry.getProxyMode()) {
            return dataSource.getConnection();
        }
        Connection result = ProxyConnectionHolder.getConnection(dataSource);
        if (null == result) {
            result = dataSource.getConnection();
            ProxyConnectionHolder.setConnection(dataSource, result);
        }
        return result;
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import io.shardingsphere.core.parsing.parser.sql.dml.insert.InsertStatement;
import io.shardingsphere.core.routing.PreparedStatementRoutingEngine;
import io.shardingsphere.core.routing.SQLExecutionUnit;
import io.shardingsphere.core.routing.SQLRouteResult;
import io.shardingsphere.proxy.backend.common.jdbc.ConnectionManager;
import io.shardingsphere.proxy.backend.common.ProxyMode;
import io.shardingsphere.proxy.backend.common.jdbc.JDBCBackendHandler;
import io.shardingsphere.proxy.backend.mysql.MySQLPacketStatementExecuteQueryResult;
@@ -38,7 +39,6 @@ import io.shardingsphere.proxy.transport.mysql.packet.command.statement.execute.
import io.shardingsphere.proxy.transport.mysql.packet.command.statement.execute.PreparedStatementParameter;
import lombok.Getter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@@ -95,8 +95,7 @@ public final class JDBCStatementBackendHandler extends JDBCBackendHandler {
    
    @Override
    protected PreparedStatement prepareResource(final SQLExecutionUnit sqlExecutionUnit, final SQLStatement sqlStatement) throws SQLException {
        DataSource dataSource = ruleRegistry.getDataSourceMap().get(sqlExecutionUnit.getDataSource());
        Connection connection = getConnection(dataSource);
        Connection connection = ConnectionManager.getConnection(ruleRegistry.getDataSourceMap().get(sqlExecutionUnit.getDataSource()));
        PreparedStatement result = sqlStatement instanceof InsertStatement
                ? connection.prepareStatement(sqlExecutionUnit.getSqlUnit().getSql(), Statement.RETURN_GENERATED_KEYS) : connection.prepareStatement(sqlExecutionUnit.getSqlUnit().getSql());
        for (int i = 0; i < preparedStatementParameters.size(); i++) {
+2 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.routing.SQLExecutionUnit;
import io.shardingsphere.core.routing.SQLRouteResult;
import io.shardingsphere.core.routing.StatementRoutingEngine;
import io.shardingsphere.proxy.backend.common.jdbc.ConnectionManager;
import io.shardingsphere.proxy.backend.common.ProxyMode;
import io.shardingsphere.proxy.backend.common.jdbc.JDBCBackendHandler;
import io.shardingsphere.proxy.backend.mysql.MySQLPacketQueryResult;
@@ -67,7 +68,7 @@ public final class JDBCTextBackendHandler extends JDBCBackendHandler {
    
    @Override
    protected Statement prepareResource(final SQLExecutionUnit sqlExecutionUnit, final SQLStatement sqlStatement) throws SQLException {
        Connection connection = getConnection(ruleRegistry.getDataSourceMap().get(sqlExecutionUnit.getDataSource()));
        Connection connection = ConnectionManager.getConnection(ruleRegistry.getDataSourceMap().get(sqlExecutionUnit.getDataSource()));
        Statement result = connection.createStatement();
        ProxyJDBCResource proxyJDBCResource = (ProxyJDBCResource) getJdbcResource();
        proxyJDBCResource.addConnection(connection);
+2 −2
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ package io.shardingsphere.proxy.frontend.mysql;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager;
import io.shardingsphere.proxy.backend.common.ProxyConnectionHolder;
import io.shardingsphere.proxy.backend.common.jdbc.ConnectionManager;
import io.shardingsphere.proxy.frontend.common.FrontendHandler;
import io.shardingsphere.proxy.frontend.common.executor.ExecutorGroup;
import io.shardingsphere.proxy.transport.common.packet.DatabaseProtocolPacket;
@@ -92,7 +92,7 @@ public final class MySQLFrontendHandler extends FrontendHandler {
                    }
                } finally {
                    MasterVisitedManager.clear();
                    ProxyConnectionHolder.clear();
                    ConnectionManager.clear();
                }
            }
        });