代码抛出java.net.SocketExpection

时间:2016-01-16 23:39:03

标签: java c3p0 socketexception

我正在使用c3p0连接池方法从远程数据库请求一些数据。我可以很好地检索我需要的所有数据,但由于某种原因,显然似乎与网络套接字相关,我根据此堆栈跟踪得到java.net.SocketException

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1137)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3697)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3586)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4131)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2597)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2758)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2820)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2769)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1569)
at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(NewProxyStatement.java:327)
at MemsqlRequester.run(MemsqlRequester.java:28)
Caused by: java.net.SocketException: Connection timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:112)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:159)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:187)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3140)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3597)
... 9 more
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
at java.util.Vector.get(Vector.java:748)
at GraphTransformer$$anonfun$getTxInformationList$1.apply$mcVI$sp(GraphTransformer.scala:38)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:141)
at GraphTransformer.getTxInformationList(GraphTransformer.scala:37)
at GraphTransformer.getFeaturesTx(GraphTransformer.scala:62)
at GraphTransformer.getFeatures(GraphTransformer.scala:327)
at GraphDatasetBuilder.lambda$publishGraphFeaturesFromMemsql$13(GraphDatasetBuilder.java:62)
at java.util.Vector.forEach(Vector.java:1249)
at GraphDatasetBuilder.publishGraphFeaturesFromMemsql(GraphDatasetBuilder.java:59)
at GraphDatasetBuilder.main(GraphDatasetBuilder.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:674)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:180)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:205)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:120)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)

基本上我在语句调用中从远程数据集中检索数据的代码片段如下:

try {
   ...
   Statement stmt = connection.createStatement();
   rs = stmt.executeQuery(this.queryList.get(queryIndex)); // According to the stack trace, here is where I'm getting the SocketException
   ...
} catch (SQLException ex) {
    ex.printStackTrace();
}

问题是我可以添加围绕此代码的任何catch子句,因为它告诉我没有抛出SocketException。基本上我的问题是这里出了什么问题,考虑到显然我无法处理堆栈跟踪中所述的代码中的SocketException

1 个答案:

答案 0 :(得分:1)

您发布的堆栈跟踪不完整,我们无法查看抛出的异常。

如果您无法捕获SocketException,我认为您收到的异常是另一个异常。

SocketException被驱动程序捕获并作为不同的异常重新抛出。您可以在堆栈跟踪中看到的SocketException只是"由"引起的,这是已捕获的原始异常。

<强>更新

您无法捕获SocketException,因为它不是您收到的异常。请粘贴完整的堆栈跟踪。

似乎ArrayIndexOutOfBoundsException是一个新的异常。我可能是错的,但我认为驱动程序不会抛出ArrayIndexOutOfBoundsException。这个例外似乎被火花抛出。

我不是很确定,但在我看来:

1)存在网络问题,您无法访问数据库并打开连接;

2)在驱动程序中抛出SocketException;

3)此异常被捕获并作为不同的异常重新抛出(堆栈跟踪不完整,抛出的异常尚未显示,但我猜这是一个CommunicationsException);

4)因此,试图读取图形的spark代码失败并抛出ArrayIndexOutOfBoundsException;