1

Is there a way to catch a connection error when using pelops in Java? I have the following code but for some reason, I'm not getting to my catch block.

public static Boolean testDBConnection() throws PelopsException {
    try{
        Cluster cluster = new Cluster("127.0.0.1", 9160);

        Pelops.addPool(pool, cluster, keyspace);

        Pelops.shutdown();

        return true;
    }
    catch(PelopsException e)
    {
        System.out.println("SOMETHING WENT WRONG!");
        System.out.println(e.getMessage());
        return false;
    }
}

This is probably something really easy, but I can't seem to get it to work. I see the exceptions coming through, but there is just no way I can catch this? Could someone please lead me in the right direction?

Thanks!

EDIT - Exception being returned in Eclipse Console...

15:47:33.545 [main] DEBUG o.s.c.pelops.pool.CommonsBackedPool - Made new connection 'Connection[Testing][127.0.0.1:9160][724408050]'
15:47:34.545 [main] ERROR o.scale7.cassandra.pelops.Connection - Failed to open transport.  See cause for details...
org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused: connect
    at org.apache.thrift.transport.TSocket.open(TSocket.java:185) ~[libthrift-0.5.0.jar:na]
    at org.apache.thrift.transport.TFramedTransport.open(TFramedTransport.java:81) ~[libthrift-0.5.0.jar:na]
    at org.scale7.cassandra.pelops.Connection.open(Connection.java:70) ~[scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool$ConnectionFactory.makeObject(CommonsBackedPool.java:785) [scale7-pelops-0.913.jar:na]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.addObject(GenericKeyedObjectPool.java:1685) [commons-pool-1.5.5.jar:1.5.5]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.ensureMinIdle(GenericKeyedObjectPool.java:2058) [commons-pool-1.5.5.jar:1.5.5]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.preparePool(GenericKeyedObjectPool.java:1722) [commons-pool-1.5.5.jar:1.5.5]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.addNode(CommonsBackedPool.java:373) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:104) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:64) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:52) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.Pelops.addPool(Pelops.java:25) [scale7-pelops-0.913.jar:na]
    at libraries.cassandra.testDBConnection(cassandra.java:192) [classes/:na]
    at libraries.cassandra.main(cassandra.java:38) [classes/:na]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.doConnect(Unknown Source) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.6.0_25]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.6.0_25]
    at java.net.Socket.connect(Unknown Source) ~[na:1.6.0_25]
    at org.apache.thrift.transport.TSocket.open(TSocket.java:180) ~[libthrift-0.5.0.jar:na]
    ... 13 common frames omitted
sdolgy
  • 6,963
  • 3
  • 41
  • 61
Kevin Mansel
  • 2,351
  • 1
  • 16
  • 15
  • when you say exceptions are coming through, where are they coming through? – Kal Jun 13 '11 at 23:09
  • Well on my console, in eclipse, I can see the connection exception come through, yet I can't catch it...edited the above example so you can see the exception. – Kevin Mansel Jun 13 '11 at 23:10

2 Answers2

1

The exception being thrown org.apache.thrift.TTransportException is a checked exception that is not caught and wrapped by PelopsException which extends RuntimeException.

If you want your method to always return a PelopsException, you must catch(Exception e) and then throw new PelopsException(e.getMessage()) inside your catch block.

   catch(Exception e)
{
    System.out.println("SOMETHING WENT WRONG!");
    System.out.println(e.getMessage());
    throw new PelopsException(e.getMessage());
}
Kal
  • 24,724
  • 7
  • 65
  • 65
  • that didn't work either. It won't actually throw the PelopsException because i'm never making it into the catch block? – Kevin Mansel Jun 13 '11 at 23:28
  • oh wait.. the function returns with a true value ? – Kal Jun 13 '11 at 23:31
  • yes, it will return the true value, yet I can't catch the error it's printing in the eclipse console. It's like it prints it out, but continues to try. then runs the shutdown and returns true. – Kevin Mansel Jun 13 '11 at 23:35
  • 1
    Ok. Looks like he does not throw any exceptions out of that method and what you are seeing is just logging. – Kal Jun 13 '11 at 23:46
  • thanks for your help. I'll contact the group that runs the project. – Kevin Mansel Jun 14 '11 at 15:15
0

It may be throwing an unchecked exception or an Error (which is not an Exception).

Try changing your code to catch (Throwable e) and see what you get.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Yeah, tried that, not happening. I've even tried to catch the java.net.ConnectException and the org.apache.thrift.transport.TTransportException, no dice. :P – Kevin Mansel Jun 13 '11 at 23:12
  • @kevin - i edited the answer to include Errors - try catch Throwable – Bohemian Jun 13 '11 at 23:14
  • tried the throwable, and still no dice. It's like the functions are running, and it's trying to set a connection, then the actual shutdown procedure is happening, and the function returns true. The error is being reported by the logger, yet it still completes the block of try code? – Kevin Mansel Jun 13 '11 at 23:27