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

com.lithium.dbi.rdbi.Handle Maven / Gradle / Ivy

There is a newer version: 0.106
Show newest version
package com.lithium.dbi.rdbi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

import javax.annotation.concurrent.NotThreadSafe;
import java.io.Closeable;

@NotThreadSafe
public class Handle implements Closeable {

    private final Pool pool;
    private final Jedis jedis;
    private JedisWrapperDoNotUse jedisWrapper;
    private boolean closed = false;
    private static final Logger logger = LoggerFactory.getLogger(Handle.class);

    private final ProxyFactory proxyFactory;

    public Handle(Pool pool, Jedis jedis, ProxyFactory proxyFactory) {
        this.pool = pool;
        this.jedis = jedis;
        this.proxyFactory = proxyFactory;
    }

    public Jedis jedis() {

        if (jedisWrapper == null) {
            jedisWrapper = proxyFactory.attachJedis(jedis);
        }

        return jedisWrapper;
    }

    public  T attach(Class type) {
        return proxyFactory.createInstance(jedis(), type);
    }

    @Override
    public void close() {

        if (closed) {
            return;
        }

        boolean isBusted;
        try {
            if (jedisWrapper != null) {
                isBusted = jedisWrapper.__rdbi_isJedisBusted__();
            } else {
                isBusted = false;
            }
        } catch (Exception e) {
            logger.error("Exception caught while checking isJedisBusted!", e);
            isBusted = true;
        }

        if (isBusted) {
            pool.returnBrokenResource(jedis);
        } else {
            try {
                pool.returnResource(jedis);
            } catch (Exception ex) {
                logger.error("Exception caught while trying to return to pool. Returning as broken.", ex);
                pool.returnBrokenResource(jedis);
            }
        }
        closed = true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy