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

refactor ConnectionManager

parent 5cc4531c
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -36,10 +36,10 @@ import java.util.Map;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ConnectionManager {
    
    private static final ThreadLocal<Map<DataSource, Connection>> RESOURCE = new ThreadLocal<Map<DataSource, Connection>>() {
    private static final ThreadLocal<Map<String, Connection>> RESOURCE = new ThreadLocal<Map<String, Connection>>() {
        
        @Override
        protected Map<DataSource, Connection> initialValue() {
        protected Map<String, Connection> initialValue() {
            return new HashMap<>();
        }
    };
@@ -47,19 +47,20 @@ public final class ConnectionManager {
    /**
     * Get connection of current thread datasource.
     *
     * @param dataSource Datasource
     * @return Connection
     * @param dataSourceName data source name
     * @return connection
     * @throws SQLException SQL exception
     */
    public static Connection getConnection(final DataSource dataSource) throws SQLException {
    public static Connection getConnection(final String dataSourceName) throws SQLException {
        DataSource dataSource = RuleRegistry.getInstance().getDataSourceMap().get(dataSourceName);
        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());
        return RESOURCE.get().containsKey(dataSourceName) ? RESOURCE.get().get(dataSourceName) : RESOURCE.get().put(dataSourceName, dataSource.getConnection());
    }
    
    /**
     * Clear Connection resource for current thread-local.
     * Clear connection resource for current thread-local.
     */
    public static void clear() {
        RESOURCE.remove();
+1 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ public final class JDBCStatementBackendHandler extends JDBCBackendHandler {
    
    @Override
    protected PreparedStatement prepareResource(final SQLExecutionUnit sqlExecutionUnit, final SQLStatement sqlStatement) throws SQLException {
        Connection connection = ConnectionManager.getConnection(ruleRegistry.getDataSourceMap().get(sqlExecutionUnit.getDataSource()));
        Connection connection = ConnectionManager.getConnection(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++) {
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public final class JDBCTextBackendHandler extends JDBCBackendHandler {
    
    @Override
    protected Statement prepareResource(final SQLExecutionUnit sqlExecutionUnit, final SQLStatement sqlStatement) throws SQLException {
        Connection connection = ConnectionManager.getConnection(ruleRegistry.getDataSourceMap().get(sqlExecutionUnit.getDataSource()));
        Connection connection = ConnectionManager.getConnection(sqlExecutionUnit.getDataSource());
        Statement result = connection.createStatement();
        ProxyJDBCResource proxyJDBCResource = (ProxyJDBCResource) getJdbcResource();
        proxyJDBCResource.addConnection(connection);