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

org.zodiac.datasource.jdbc.connection.ConnectionFactory Maven / Gradle / Ivy

The newest version!
package org.zodiac.datasource.jdbc.connection;

import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConnectionFactory {

    private static final ThreadLocal> CONNECTION_HOLDER =
        new ThreadLocal>() {
            @Override
            protected Map initialValue() {
                return new ConcurrentHashMap<>();
            }
        };

    public static void putConnection(String ds, ConnectionProxy connection) {
        Map concurrentHashMap = CONNECTION_HOLDER.get();
        if (!concurrentHashMap.containsKey(ds)) {
            try {
                connection.setAutoCommit(false);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            concurrentHashMap.put(ds, connection);
        }
    }

    public static ConnectionProxy getConnection(String ds) {
        return CONNECTION_HOLDER.get().get(ds);
    }

    public static void notify(Boolean state) {
        try {
            Map concurrentHashMap = CONNECTION_HOLDER.get();
            for (ConnectionProxy connectionProxy : concurrentHashMap.values()) {
                connectionProxy.notify(state);
            }
        } finally {
            CONNECTION_HOLDER.remove();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy