<commit_before>package org.rakam.util.lock;

import com.google.common.base.Throwables;
import org.rakam.analysis.JDBCPoolDataSource;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.util.BooleanMapper;

import java.sql.SQLException;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

public class MysqlLockService
        implements LockService {
    private final DBI dbi;
    private Handle currentHandle;
    private Set<String> locks;

    public MysqlLockService(JDBCPoolDataSource poolDataSource) {
        this.dbi = new DBI(() -> {
            return poolDataSource.getConnection(true);
        });
        this.currentHandle = dbi.open();
        locks = new ConcurrentSkipListSet<>();
    }

    @Override
    public Lock tryLock(String name) {
        if (!locks.add(name)) {
            return null;
        }
        try {
            return tryLock(name, 4);
        } catch (Exception e) {
            locks.remove(name);
            throw Throwables.propagate(e);
        }
    }

    public Lock tryLock(String name, int tryCount) {
        try {
            if (currentHandle.getConnection().isClosed()) {
                synchronized (this) {
                    currentHandle = dbi.open();
                }
            }

            Boolean first = currentHandle.createQuery("select get_lock(:name, 1)")
                    .bind("name", name)
                    .map(BooleanMapper.FIRST)
                    .first();

            if (!Boolean.TRUE.equals(first)) {
                locks.remove(name);
                return null;
            }

            return () -> {
                locks.remove(name);
                currentHandle.createQuery("select release_lock(:name)")
                        .bind("name", name)
                        .map(BooleanMapper.FIRST)
                        .first();
            };
        } catch (SQLException e) {
            try {
                if (currentHandle.getConnection().isClosed()) {
                    synchronized (this) {
                        currentHandle = dbi.open();
                    }
                }
            } catch (SQLException e1) {
                synchronized (this) {
                    currentHandle = dbi.open();
                }
            }

            if (tryCount == 0) {
                throw Throwables.propagate(e);
            }
            return tryLock(name, tryCount - 1);
        }
    }
}<commit_msg>Fix bug in Mysql lock service<commit_after>package org.rakam.util.lock;

import com.google.common.base.Throwables;
import org.rakam.analysis.JDBCPoolDataSource;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.util.BooleanMapper;

import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

public class MysqlLockService
        implements LockService {
    private final DBI dbi;
    private Handle currentHandle;
    private Set<String> locks;

    public MysqlLockService(JDBCPoolDataSource poolDataSource) {
        this.dbi = new DBI(() -> {
            return poolDataSource.getConnection(true);
        });
        this.currentHandle = dbi.open();
        locks = new ConcurrentSkipListSet<>();
    }

    @Override
    public Lock tryLock(String name) {
        if (!locks.add(name)) {
            return null;
        }
        try {
            return tryLock(name, 4);
        } catch (Exception e) {
            locks.remove(name);
            throw Throwables.propagate(e);
        }
    }

    public Lock tryLock(String name, int tryCount) {
        Boolean first;
        try {
            first = currentHandle.createQuery("select get_lock(:name, 1)")
                    .bind("name", name)
                    .map(BooleanMapper.FIRST)
                    .first();
        } catch (Exception e) {
            synchronized (this) {
                currentHandle = dbi.open();
            }

            if (tryCount == 0) {
                throw Throwables.propagate(e);
            }

            return tryLock(name, tryCount - 1);
        }

        if (!Boolean.TRUE.equals(first)) {
            locks.remove(name);
            return null;
        }

        return () -> {
            locks.remove(name);
            currentHandle.createQuery("select release_lock(:name)")
                    .bind("name", name)
                    .map(BooleanMapper.FIRST)
                    .first();
        };
    }
}
{'fraction_non_alphanumeric': 0.10679177837354781, 'fraction_numerical': 0.003351206434316354, 'mean_word_length': 1.553907586993725, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6202448', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1185, 'n_words': 240}