thobbs / phpcassa

PHP client library for Apache Cassandra
thobbs.github.com/phpcassa
MIT License
248 stars 78 forks source link

!!! Question !!! about catching Exceptions #94

Closed sarral closed 12 years ago

sarral commented 12 years ago

Trying to trap when I can't connect to the cluster.

Thank you for your help.

I have looked everywhere (except where an example is, haha) to find an example I'm kinda new to PHP, but experienced programmer.

I'm going to try this as a script, without using a Class

class CassCore { ...

function __construct() { try { $this->rCONN = new ConnectionPool(self::KS, $this->servers); } catch( NoServerAvailable $e ) { echo "No Connection to Cluster\n"; die("FOUND EXCEPTION\n"); } catch( phpcassa\Connection\NoServerAvailable $e ) { echo "No Connection to Cluster\n"; die("FOUND EXCEPTION\n"); } // Connect to CFs ...... return; }

and what i receive:

Error connecting to 191.168.1.50: TException: TSocket: Could not connect to 191.168.1.50:9160 (Connection timed out [110]) Error connecting to 191.168.1.101: TException: TSocket: Could not connect to 191.168.1.101:9160 (Connection timed out [110]) Error connecting to 191.168.1.50: TException: TSocket: Could not connect to 191.168.1.50:9160 (Connection timed out [110]) Error connecting to 191.168.1.101: TException: TSocket: Could not connect to 191.168.1.101:9160 (Connection timed out [110])

Fatal error: Uncaught exception 'phpcassa\Connection\NoServerAvailable' with message 'An attempt was made to connect to every server twice, but all attempts failed. The last error was: TException:TSocket: Could not connect to 191.168.1.101:9160 (Connection timed out [110])' in /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php:138

Stack trace:

0 /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php(163): phpcassa\Connection\ConnectionPool->make_conn()

1 /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php(257): phpcassa\Connection\ConnectionPool->get()

2 /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php(192): phpcassa\Connection\ConnectionPool->call('describe_keyspa...', 'ip_store')

3 /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/ColumnFamily.php(193): phpcassa\Connection\ConnectionPool->describe_keyspace()

4 /home/shaun/Documents/Cassandra/CassCore.php(67): phpcassa\ColumnFamily->__const in /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php on line 138

sarral commented 12 years ago

nevermind.... figured it out.

try { $this->rCF_ip = new ColumnFamily($this->rCONN, self::CF_ip); $this->rCF_det = new ColumnFamily($this->rCONN, self::CF_det); }

catch( phpcassa\Connection\NoServerAvailable $e ) { echo "4: No Connection to Cluster\n"; die("FOUND EXCEPTION\n"); }

sarral commented 12 years ago

FYI for other users.

thobbs commented 12 years ago

Are you using the autoloader (autoload.php)? If so, try adding 'use phpcassa\Connection\NoServerAvailable;' to the top of your script, and your first catch block should work.

On Mon, Jul 23, 2012 at 5:06 PM, Shaun Arral < reply@reply.github.com

wrote:

Trying to trap when I can't connect to the cluster.

Thank you for your help.

I have looked everywhere (except where an example is, haha) to find an example I'm kinda new to PHP, but experienced programmer.

I'm going to try this as a script, without using a Class

class CassCore { ...

function __construct() { try { $this->rCONN = new ConnectionPool(self::KS, $this->servers); } catch( NoServerAvailable $e ) { echo "No Connection to Cluster\n"; die("FOUND EXCEPTION\n"); } catch( phpcassa\Connection\NoServerAvailable $e ) { echo "No Connection to Cluster\n"; die("FOUND EXCEPTION\n"); } // Connect to CFs ...... return; }

and what i receive:

Error connecting to 191.168.1.50: TException: TSocket: Could not connect to 191.168.1.50:9160 (Connection timed out [110]) Error connecting to 191.168.1.101: TException: TSocket: Could not connect to 191.168.1.101:9160 (Connection timed out [110]) Error connecting to 191.168.1.50: TException: TSocket: Could not connect to 191.168.1.50:9160 (Connection timed out [110]) Error connecting to 191.168.1.101: TException: TSocket: Could not connect to 191.168.1.101:9160 (Connection timed out [110])

Fatal error: Uncaught exception 'phpcassa\Connection\NoServerAvailable' with message 'An attempt was made to connect to every server twice, but all attempts failed. The last error was: TException:TSocket: Could not connect to 191.168.1.101:9160 (Connection timed out [110])' in /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php:138

Stack trace:

0

/home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php(163): phpcassa\Connection\ConnectionPool->make_conn()

1

/home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php(257): phpcassa\Connection\ConnectionPool->get()

2

/home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php(192): phpcassa\Connection\ConnectionPool->call('describe_keyspa...', 'ip_store')

3

/home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/ColumnFamily.php(193): phpcassa\Connection\ConnectionPool->describe_keyspace()

4 /home/shaun/Documents/Cassandra/CassCore.php(67):

phpcassa\ColumnFamily->__const in /home/shaun/Documents/Cassandra/phpcass/lib/phpcassa/Connection/ConnectionPool.php on line 138


Reply to this email directly or view it on GitHub: https://github.com/thobbs/phpcassa/issues/94

Tyler Hobbs DataStax http://datastax.com/

sarral commented 12 years ago

Yes.

require_once('../phpcass/lib/autoload.php');

It's probably better to catch early (ConnectionPool) rather than what I figured out to do: Catch on ColumnFamily init.

my second comment did work. Going to do your suggestion.

Thanks.