<commit_before>/**********************************************************************
Copyright (c) 2010 <NAME> and Universidade do Minho. All rights reserved.
(Based on datanucleus-hbase. Copyright (c) 2009 <NAME> and others.)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

**********************************************************************/

package org.datanucleus.store.cassandra;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;

public class CassandraConnectionPool
{

    private final List<CassandraManagedConnection> connections;

    private final ThreadLocal<WeakReference<CassandraManagedConnection>> connectionForCurrentThread;

    private final Timer evictorThread;

    private int timeBetweenEvictionRunsMillis = 15 * 1000; // default, 15 secs

    public CassandraConnectionPool()
    {
        connectionForCurrentThread = new ThreadLocal<WeakReference<CassandraManagedConnection>>();
        connections = new CopyOnWriteArrayList<CassandraManagedConnection>();

        evictorThread = new Timer("Cassandra Connection Evictor", true);
        startConnectionEvictorThread(evictorThread);
    }

    public void registerConnection(CassandraManagedConnection managedConnection)
    {
        connections.add(managedConnection);
        connectionForCurrentThread.set(new WeakReference<CassandraManagedConnection>(managedConnection));
    }

    public CassandraManagedConnection getPooledConnection()
    {
        WeakReference<CassandraManagedConnection> ref = connectionForCurrentThread.get();

        if (ref == null)
        {
            return null;
        }
        else
        {
            CassandraManagedConnection managedConnection = ref.get();

            if (managedConnection != null && !managedConnection.isDisposed())
            {
                return managedConnection;
            }
            else
            {
                return null;
            }
        }
    }

    public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis)
    {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    private void disposeTimedOutConnections()
    {
        List<CassandraManagedConnection> timedOutConnections = new ArrayList<CassandraManagedConnection>();

        for (CassandraManagedConnection managedConnection : connections)
        {
            if (managedConnection.isExpired())
            {
                timedOutConnections.add(managedConnection);
            }
        }

        for (CassandraManagedConnection managedConnection : timedOutConnections)
        {
            managedConnection.dispose();
            connections.remove(managedConnection);
        }

    }

    private void startConnectionEvictorThread(Timer connectionTimeoutThread)
    {
        TimerTask timeoutTask = new TimerTask()
        {

            public void run()
            {
                disposeTimedOutConnections();
            }
        };

        evictorThread.schedule(timeoutTask, timeBetweenEvictionRunsMillis, timeBetweenEvictionRunsMillis);
    }

}<commit_msg>Connection lock to avoid the use of a closing connection<commit_after>/**********************************************************************
 Copyright (c) 2010 <NAME> and Universidade do Minho. All rights reserved.
 (Based on datanucleus-hbase. Copyright (c) 2009 <NAME> and others.)
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 **********************************************************************/

package org.datanucleus.store.cassandra;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;

public class CassandraConnectionPool {

    private ReentrantLock connection_lock = new ReentrantLock();

    private final List<CassandraManagedConnection> connections;

    private final ThreadLocal<WeakReference<CassandraManagedConnection>> connectionForCurrentThread;

    private final Timer evictorThread;

    //private int timeBetweenEvictionRunsMillis = 15 * 1000; // default, 15 secs
    private int timeBetweenEvictionRunsMillis = 15 * 1000; // default, 15 secs

    public CassandraConnectionPool() {

        connectionForCurrentThread = new ThreadLocal<WeakReference<CassandraManagedConnection>>();
        connections = new CopyOnWriteArrayList<CassandraManagedConnection>();

        evictorThread = new Timer("Cassandra Connection Eviction Thread", true);
        startConnectionEvictorThread(evictorThread);

    }

    public void registerConnection(CassandraManagedConnection managedConnection) {

        connections.add(managedConnection);
        connectionForCurrentThread.set(new WeakReference<CassandraManagedConnection>(managedConnection));

    }

    public CassandraManagedConnection getPooledConnection() {
        WeakReference<CassandraManagedConnection> ref = connectionForCurrentThread.get();

        if (ref == null) {
            return null;
        } else {

            CassandraManagedConnection managedConnection = ref.get();

            connection_lock.lock();

            try {
                if (managedConnection != null && !managedConnection.isDisposed()) {
                    return managedConnection;
                } else {
                    return null;
                }

            } finally {
                connection_lock.unlock();
            }


        }
    }

    public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    private void disposeTimedOutConnections() {

        List<CassandraManagedConnection> timedOutConnections = new LinkedList<CassandraManagedConnection>();

        connection_lock.lock();


        for (CassandraManagedConnection managedConnection : connections) {
            if (managedConnection.isExpired()) {
                timedOutConnections.add(managedConnection);
                managedConnection.dispose();
            }
        }

        connection_lock.unlock();

        for (CassandraManagedConnection managedConnection : timedOutConnections) {
            connections.remove(managedConnection);
        }

    }

    private void startConnectionEvictorThread(Timer connectionTimeoutThread) {
        TimerTask timeoutTask = new TimerTask() {

            public void run() {
                disposeTimedOutConnections();
            }
        };

        evictorThread.schedule(timeoutTask, timeBetweenEvictionRunsMillis, timeBetweenEvictionRunsMillis);
    }

}
{'fraction_non_alphanumeric': 0.10312338668043367, 'fraction_numerical': 0.00619514713474445, 'mean_word_length': 3.211413043478261, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2652793', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1765, 'n_words': 477}