summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/thinkaurelius/titan/diskstorage/cassandra/thrift/thriftpool/CTConnectionPool.java
blob: 1af5630913138631e7679500d466383952fb41dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.thinkaurelius.titan.diskstorage.cassandra.thrift.thriftpool;

import org.apache.commons.pool.KeyedPoolableObjectFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class extends Apache Commons Pool's GenericKeyedObjectPool, adding
 * two methods that support Java 5 generic type safety.  However, a
 * programmer can still cause RuntimeExceptions related to type errors
 * by mixing calls to these additional methods with calls to the legacy
 * "Object"-typed methods.
 * <p/>
 * <p/>
 * Unfortunately, GenericKeyedObjectPool is not actually generic in the
 * type-system sense.  All of its methods are typed to Object, forcing the
 * client programmer to sprinkle code with casts.  This class centralizes
 * that casting to a single method.
 * <p/>
 * <p/>
 * As a corollary, this class is slightly less flexible than
 * GenericKeyedObjectPool, as this class can only store keys and pooled
 * objects each of a single type, whereas GenericKeyedObjectPool could
 * theoretically contain heterogeneous types of each.  However, I do not
 * need the flexibility of heterogeneous types for pooling Thrift
 * connections, the original work that precipitated writing this class.
 *
 * @param <K> Key type
 * @param <V> Pooled object type
 * @author Dan LaRocque <dalaro@hopcount.org>
 */
public class CTConnectionPool extends GenericKeyedObjectPool<String, CTConnection> {
    
    private static final Logger log =
            LoggerFactory.getLogger(CTConnectionPool.class);
    
    public CTConnectionPool(KeyedPoolableObjectFactory<String, CTConnection> factory) {
        super(factory);
    }

    /**
     * If {@code conn} is non-null and is still open, then call
     * {@link GenericKeyedObjectPool#returnObject(String, CTConnection),
     * catching and logging and Exception that method might generate. 
     * This method does not emit any exceptions.
     * 
     * @param keyspace The key of the pooled object being returned
     * @param conn The pooled object being returned, or null to do nothing
     */
    public void returnObjectUnsafe(String keyspace, CTConnection conn) {
        if (conn != null && conn.isOpen()) {
            try {
                returnObject(keyspace, conn);
            } catch (Exception e) {
                log.warn("Failed to return Cassandra connection to pool", e);
                log.warn(
                        "Failure context: keyspace={}, pool={}, connection={}",
                        new Object[] { keyspace, this, conn });
            }
        }
    }
}