<commit_before>/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * 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 com.proofpoint.dbpool;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.proofpoint.discovery.client.ServiceDescriptor;
import com.proofpoint.discovery.client.ServiceSelector;

import javax.sql.PooledConnection;
import java.sql.SQLException;
import java.util.UUID;

public class MySqlDataSource extends ManagedDataSource
{
    private final ServiceSelector serviceSelector;
    private final int defaultFetchSize;
    private UUID currentServer;
    private MysqlConnectionPoolDataSource dataSource;

    public MySqlDataSource(ServiceSelector serviceSelector, MySqlDataSourceConfig config)
    {
        super(config.getMaxConnections(), config.getMaxConnectionWait());

        this.serviceSelector = serviceSelector;
        this.defaultFetchSize = config.getDefaultFetchSize();
    }

    protected PooledConnection createConnectionInternal()
            throws SQLException
    {
        // attempt to get a connection from the current datasource if we have one
        SQLException lastException = null;
        if (dataSource != null) {
            try {
                return dataSource.getPooledConnection();
            }
            catch (SQLException e) {
                lastException = e;
            }
        }

        // drop reference to current datasource
        dataSource = null;

        // attempt to create a connection to each mysql server (except for the one that we know is bad)
        for (ServiceDescriptor serviceDescriptor : serviceSelector.selectAllServices()) {
            // skip the current server since it is having problems
            if (serviceDescriptor.getId().equals(currentServer)) {
                continue;
            }

            // skip bogus announcements
            String jdbcUrl = serviceDescriptor.getProperties().get("jdbc");
            if (jdbcUrl == null) {
                continue;
            }

            try {
                MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
                dataSource.setUrl(jdbcUrl);
                dataSource.setConnectTimeout(getLoginTimeout());
                dataSource.setInitialTimeout(getLoginTimeout());
                dataSource.setDefaultFetchSize(defaultFetchSize);<commit_msg>Timeouts are in MS not seconds<commit_after>/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * 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 com.proofpoint.dbpool;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.proofpoint.discovery.client.ServiceDescriptor;
import com.proofpoint.discovery.client.ServiceSelector;

import javax.sql.PooledConnection;
import java.sql.SQLException;
import java.util.UUID;

public class MySqlDataSource extends ManagedDataSource
{
    private final ServiceSelector serviceSelector;
    private final int defaultFetchSize;
    private UUID currentServer;
    private MysqlConnectionPoolDataSource dataSource;

    public MySqlDataSource(ServiceSelector serviceSelector, MySqlDataSourceConfig config)
    {
        super(config.getMaxConnections(), config.getMaxConnectionWait());

        this.serviceSelector = serviceSelector;
        this.defaultFetchSize = config.getDefaultFetchSize();
    }

    protected PooledConnection createConnectionInternal()
            throws SQLException
    {
        // attempt to get a connection from the current datasource if we have one
        SQLException lastException = null;
        if (dataSource != null) {
            try {
                return dataSource.getPooledConnection();
            }
            catch (SQLException e) {
                lastException = e;
            }
        }

        // drop reference to current datasource
        dataSource = null;

        // attempt to create a connection to each mysql server (except for the one that we know is bad)
        for (ServiceDescriptor serviceDescriptor : serviceSelector.selectAllServices()) {
            // skip the current server since it is having problems
            if (serviceDescriptor.getId().equals(currentServer)) {
                continue;
            }

            // skip bogus announcements
            String jdbcUrl = serviceDescriptor.getProperties().get("jdbc");
            if (jdbcUrl == null) {
                continue;
            }

            try {
                MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource();
                dataSource.setUrl(jdbcUrl);
                // these are in MS *not* seconds like everything else in JDBC
                dataSource.setConnectTimeout(getMaxConnectionWaitMillis());
                dataSource.setInitialTimeout(getMaxConnectionWaitMillis());
                dataSource.setDefaultFetchSize(defaultFetchSize);
{'fraction_non_alphanumeric': 0.06862578562935281, 'fraction_numerical': 0.0030575845082384916, 'mean_word_length': 2.707808564231738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4669138', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1339, 'n_words': 453}