net.dataforte.cassandra.pool
Class ConnectionPool

java.lang.Object
  extended by net.dataforte.cassandra.pool.ConnectionPool

public class ConnectionPool
extends Object

An implementation of a connection pool for Cassandra Thrift connections. Supports reaping of abandoned connections Derived from org.apache.tomcat.jdbc.pool.ConnectionPool by fhanik

Author:
Tristan Tarrant

Nested Class Summary
protected  class ConnectionPool.PoolMaintenance
           
 
Field Summary
protected  ConnectionPoolMBean jmxPool
          reference to the JMX mbean
static String POOL_JMX_PREFIX
          Prefix type for JMX registration
static String POOL_JMX_TYPE_PREFIX
           
 
Constructor Summary
ConnectionPool(PoolConfiguration prop)
          Instantiate a connection pool.
 
Method Summary
protected  void abandon(PooledConnection con)
          thread safe way to abandon a connection signals a connection to be abandoned. this will disconnect the connection, and log the stack trace if logAbanded=true
protected  PooledConnection borrowConnection(long now, PooledConnection con)
          Validates and configures a previously idle connection
 void checkAbandoned()
          Iterates through all the busy connections and checks for connections that have timed out
 void checkIdle()
          Iterates through the idle connections and resizes the idle pool based on parameters PoolProperties.maxIdle, PoolProperties.minIdle , PoolProperties.minEvictableIdleTimeMillis
 void close()
           
 void close(boolean force)
          Closes the pool and all disconnects all idle connections Active connections will be closed upon the close method is called on the underlying connection instead of being returned to the pool
protected  PooledConnection create()
          Create a new pooled connection object.
protected  PooledConnection createConnection(long now, PooledConnection con)
          Creates a Cassandra connection and tries to connect to the database.
protected  void createMBean()
          Create MBean object that can be registered.
protected  void disconnectEvent(PooledConnection con, boolean finalizing)
          Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded
protected  void finalize(PooledConnection con)
          Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded
 int getActive()
          Returns the number of connections that are in use
 CassandraRing getCassandraRing()
           
 org.apache.cassandra.thrift.Cassandra.Client getConnection()
          Borrows a connection from the pool.
 int getIdle()
          Returns the number of idle connections
 ConnectionPoolMBean getJmxPool()
          Return the object that is potentially registered in JMX for notifications
 String getName()
          Returns the name of this pool
 PoolConfiguration getPoolProperties()
          Returns the pool properties associated with this connection pool
 int getSize()
          Returns the total size of this pool, this includes both busy and idle connections
static String getStackTrace(Throwable x)
          Convert an exception into a String
protected static String getThreadDump()
          Creates a stack trace representing the existing thread's current state.
 int getWaitCount()
          Return the number of threads waiting for a connection
protected  void init(PoolConfiguration properties)
          Initialize the connection pool - called from the constructor
 boolean isClosed()
          Returns true if close has been called, and the connection pool is unusable
 void refreshRing()
          Refreshes the ring information
 void release(org.apache.cassandra.thrift.Cassandra.Client connection)
           
protected  void release(PooledConnection con)
          thread safe way to release a connection
protected  void returnConnection(PooledConnection con)
          Returns a connection to the pool If the pool is closed, the connection will be released If the connection is not part of the busy queue, it will be released.
protected  boolean shouldAbandon()
          Determines if a connection should be abandoned based on PoolProperties.abandonWhenPercentageFull setting.
protected  boolean shouldClose(PooledConnection con, int action)
          Determines if a connection should be closed upon return to the pool.
protected  void suspect(PooledConnection con)
          thread safe way to abandon a connection signals a connection to be abandoned. this will disconnect the connection, and log the stack trace if logAbanded=true
 void testAllIdle()
          Forces a validation of all idle connections if PoolProperties.testWhileIdle is set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POOL_JMX_PREFIX

public static final String POOL_JMX_PREFIX
Prefix type for JMX registration

See Also:
Constant Field Values

POOL_JMX_TYPE_PREFIX

public static final String POOL_JMX_TYPE_PREFIX
See Also:
Constant Field Values

jmxPool

protected ConnectionPoolMBean jmxPool
reference to the JMX mbean

Constructor Detail

ConnectionPool

public ConnectionPool(PoolConfiguration prop)
               throws org.apache.thrift.TException
Instantiate a connection pool. This will create connections if initialSize is larger than 0. The PoolProperties should not be reused for another connection pool.

Parameters:
prop - PoolProperties - all the properties for this connection pool
Throws:
Exception
SQLException
org.apache.thrift.TException
Method Detail

getConnection

public org.apache.cassandra.thrift.Cassandra.Client getConnection()
                                                           throws org.apache.thrift.TException
Borrows a connection from the pool. If a connection is available (in the idle queue) or the pool has not reached maxActive connections a connection is returned immediately. If no connection is available, the pool will attempt to fetch a connection for maxWait milliseconds.

Returns:
Connection - a java.sql.Connection/javax.sql.PooledConnection reflection proxy, wrapping the underlying object.
Throws:
org.apache.cassandra.thrift.InvalidRequestException
org.apache.cassandra.thrift.AuthorizationException
org.apache.cassandra.thrift.AuthenticationException
SQLException - - if the wait times out or a failure occurs creating a connection
org.apache.thrift.TException

getName

public String getName()
Returns the name of this pool

Returns:
String - the name of the pool

getWaitCount

public int getWaitCount()
Return the number of threads waiting for a connection

Returns:
number of threads waiting for a connection

getPoolProperties

public PoolConfiguration getPoolProperties()
Returns the pool properties associated with this connection pool

Returns:
PoolProperties

getSize

public int getSize()
Returns the total size of this pool, this includes both busy and idle connections

Returns:
int - number of established connections to the database

getActive

public int getActive()
Returns the number of connections that are in use

Returns:
int - number of established connections that are being used by the application

getIdle

public int getIdle()
Returns the number of idle connections

Returns:
int - number of established connections not being used

isClosed

public boolean isClosed()
Returns true if close has been called, and the connection pool is unusable

Returns:
boolean

close

public void close()

close

public void close(boolean force)
Closes the pool and all disconnects all idle connections Active connections will be closed upon the close method is called on the underlying connection instead of being returned to the pool

Parameters:
force - - true to even close the active connections

init

protected void init(PoolConfiguration properties)
             throws org.apache.thrift.TException
Initialize the connection pool - called from the constructor

Parameters:
properties - PoolProperties - properties used to initialize the pool with
Throws:
Exception
SQLException - if initialization fails
org.apache.thrift.TException

abandon

protected void abandon(PooledConnection con)
thread safe way to abandon a connection signals a connection to be abandoned. this will disconnect the connection, and log the stack trace if logAbanded=true

Parameters:
con - PooledConnection

suspect

protected void suspect(PooledConnection con)
thread safe way to abandon a connection signals a connection to be abandoned. this will disconnect the connection, and log the stack trace if logAbanded=true

Parameters:
con - PooledConnection

release

protected void release(PooledConnection con)
thread safe way to release a connection

Parameters:
con - PooledConnection

createConnection

protected PooledConnection createConnection(long now,
                                            PooledConnection con)
                                     throws org.apache.thrift.TException
Creates a Cassandra connection and tries to connect to the database.

Parameters:
now - timestamp of when this was called
con - the previous pooled connection - argument not used
Returns:
a PooledConnection that has been connected
Throws:
SQLException
org.apache.thrift.TException

borrowConnection

protected PooledConnection borrowConnection(long now,
                                            PooledConnection con)
                                     throws org.apache.thrift.TException
Validates and configures a previously idle connection

Parameters:
now - - timestamp
con - - the connection to validate and configure
Returns:
con
Throws:
org.apache.cassandra.thrift.InvalidRequestException
org.apache.cassandra.thrift.AuthorizationException
org.apache.cassandra.thrift.AuthenticationException
SQLException - if a validation error happens
org.apache.thrift.TException

shouldClose

protected boolean shouldClose(PooledConnection con,
                              int action)
Determines if a connection should be closed upon return to the pool.

Parameters:
con - - the connection
action - - the validation action that should be performed
Returns:
true if the connection should be closed

release

public void release(org.apache.cassandra.thrift.Cassandra.Client connection)

returnConnection

protected void returnConnection(PooledConnection con)
Returns a connection to the pool If the pool is closed, the connection will be released If the connection is not part of the busy queue, it will be released. If PoolProperties.testOnReturn is set to true it will be validated

Parameters:
con - PooledConnection to be returned to the pool

shouldAbandon

protected boolean shouldAbandon()
Determines if a connection should be abandoned based on PoolProperties.abandonWhenPercentageFull setting.

Returns:
true if the connection should be abandoned

checkAbandoned

public void checkAbandoned()
Iterates through all the busy connections and checks for connections that have timed out


checkIdle

public void checkIdle()
Iterates through the idle connections and resizes the idle pool based on parameters PoolProperties.maxIdle, PoolProperties.minIdle , PoolProperties.minEvictableIdleTimeMillis


testAllIdle

public void testAllIdle()
Forces a validation of all idle connections if PoolProperties.testWhileIdle is set.


refreshRing

public void refreshRing()
Refreshes the ring information


getThreadDump

protected static String getThreadDump()
Creates a stack trace representing the existing thread's current state.

Returns:
a string object representing the current state. TODO investigate if we simply should store Thread.getStackTrace() elements

getStackTrace

public static String getStackTrace(Throwable x)
Convert an exception into a String

Parameters:
x - - the throwable
Returns:
a string representing the stack trace

create

protected PooledConnection create()
Create a new pooled connection object. Not connected nor validated.

Returns:
a pooled connection object

finalize

protected void finalize(PooledConnection con)
Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded

Parameters:
con -

disconnectEvent

protected void disconnectEvent(PooledConnection con,
                               boolean finalizing)
Hook to perform final actions on a pooled connection object once it has been disconnected and will be discarded

Parameters:
con -

getJmxPool

public ConnectionPoolMBean getJmxPool()
Return the object that is potentially registered in JMX for notifications

Returns:
the object implementing the ConnectionPoolMBean interface

getCassandraRing

public CassandraRing getCassandraRing()

createMBean

protected void createMBean()
Create MBean object that can be registered.



Copyright © 2011. All Rights Reserved.