Pelops - 捕获连接错误?

时间:2011-06-13 23:06:47

标签: java database cassandra pelops

在Java中使用pelops时是否有办法捕获连接错误?我有以下代码,但出于某种原因,我没有进入我的catch块。

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;
    }
}

这可能很容易,但我似乎无法让它发挥作用。我看到异常即将来临,但我无法理解这一点?有人可以带领我朝正确的方向前进吗?

谢谢!

编辑 - 在Eclipse控制台中返回异常...

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

2 个答案:

答案 0 :(得分:1)

抛出异常org.apache.thrift.TTransportException是一个已检查的异常,它不会被PelopsException捕获并包装,从而扩展RuntimeException。

如果希望方法始终返回PelopsException,则必须catch(Exception e)然后在catch块中抛出新的PelopsException(e.getMessage())。

   catch(Exception e)
{
    System.out.println("SOMETHING WENT WRONG!");
    System.out.println(e.getMessage());
    throw new PelopsException(e.getMessage());
}

答案 1 :(得分:0)

可能会抛出unchecked exception或错误(异常)。 尝试将代码更改为catch (Throwable e),看看你得到了什么。