2011-06-13 117 views
1

有沒有辦法在Java中使用pelops時發現連接錯誤?我有下面的代碼,但由於某種原因,我沒有去我的catch塊。Pelops - 連接錯誤?

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 
+0

當你說異常正在通過,他們在哪裏通過? – Kal 2011-06-13 23:09:21

+0

那麼在我的控制檯上,在eclipse中,我可以看到連接異常通過,但我無法捕捉它...編輯上面的示例,以便您可以看到異常。 – 2011-06-13 23:10:32

回答

1

拋出的異常org.apache.thrift.TTransportException是一個檢查異常,它沒有被捕獲,並且被擴展RuntimeException的PelopsException包裝。

如果你希望你的方法總是返回一個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()); 
} 
+0

也沒有工作。它實際上不會拋出PelopsException,因爲我從來沒有把它放入catch塊? – 2011-06-13 23:28:25

+0

哦,等等..函數返回一個真值? – Kal 2011-06-13 23:31:35

+0

是的,它會返回真值,但我無法捕捉它在eclipse控制檯中打印的錯誤。這就像它打印出來,但繼續嘗試。然後運行關機並返回true。 – 2011-06-13 23:35:12

0

它可以拋出unchecked exception或錯誤(這是不一個異常) -

EDIT。

嘗試將您的代碼更改爲catch (Throwable e),看看您得到了什麼。

+0

是的,試過了,沒有發生。我甚至試圖捕獲java.net.ConnectException和org.apache.thrift.transport.TTransportException,沒有骰子。 :P – 2011-06-13 23:12:20

+0

@kevin - 我編輯了包含錯誤的答案 - 試試catch Throwable – Bohemian 2011-06-13 23:14:52

+0

嘗試了throwable,但仍然沒有骰子。這就像函數正在運行,它試圖設置連接,然後實際的關閉過程正在發生,並且函數返回true。記錄器正在報告錯誤,但它仍然完成了try代碼塊? – 2011-06-13 23:27:33