All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.despegar.jdbc.galera.consistency.ConsistencyLevelSupport Maven / Gradle / Ivy

Go to download

A simple Java client for MariaDB Galera Cluster and Percona XtraDB Cluster. It is designed to be an alternative to connect JVM app to MariaDB/Percona galera nodes without HAProxy

There is a newer version: 1.0.20
Show newest version
package com.despegar.jdbc.galera.consistency;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ConsistencyLevelSupport {
    private static final Logger LOG = LoggerFactory.getLogger(ConsistencyLevelSupport.class);

    private static final String SET_SESSION_SYNC_WAIT = "SET SESSION wsrep_sync_wait = ?";
    private static final String SET_SESSION_CAUSAL_READS = "SET SESSION wsrep_causal_reads = ?";

    public static void set(Connection connection, String consistencyLevel, boolean supportsSyncWait) throws SQLException {
        PreparedStatement preparedStatement = null;

        try {
            if (supportsSyncWait) {
                LOG.info("Setting wsrep_sync_wait to {}", consistencyLevel);
                preparedStatement = connection.prepareStatement(SET_SESSION_SYNC_WAIT);
                preparedStatement.setInt(1, Integer.valueOf(consistencyLevel));
            } else {
                LOG.info("Setting wsrep_causal_reads to {}", consistencyLevel);
                preparedStatement = connection.prepareStatement(SET_SESSION_CAUSAL_READS);
                preparedStatement.setString(1, consistencyLevel);
            }

            preparedStatement.execute();

        } finally {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy