Skip to content

Commit 6518d36

Browse files
author
Stephen Boesch
committed
SPARK-2689 Fix tiny bug in JdbcRdd for closing jdbc connection
1 parent 3fb23ed commit 6518d36

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

core/src/main/scala/org/apache/spark/rdd/JdbcRDD.scala

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.spark.rdd
1919

20-
import java.sql.{PreparedStatement, Connection, ResultSet}
20+
import java.sql.{Connection, ResultSet}
2121

2222
import scala.reflect.ClassTag
2323

@@ -70,31 +70,21 @@ class JdbcRDD[T: ClassTag](
7070
override def compute(thePart: Partition, context: TaskContext) = new NextIterator[T] {
7171
context.addOnCompleteCallback{ () => closeIfNeeded() }
7272
val part = thePart.asInstanceOf[JdbcPartition]
73-
var conn : Connection = _
74-
var stmt : PreparedStatement = _
75-
try {
76-
conn = getConnection()
77-
stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
73+
val conn = getConnection()
74+
val stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
7875

79-
// setFetchSize(Integer.MIN_VALUE) is a mysql driver specific way to force streaming results,
80-
// rather than pulling entire resultset into memory.
81-
// see http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-implementation-notes.html
82-
if (conn.getMetaData.getURL.matches("jdbc:mysql:.*")) {
83-
stmt.setFetchSize(Integer.MIN_VALUE)
84-
logInfo("statement fetch size set to: " + stmt.getFetchSize + " to force MySQL streaming ")
85-
}
86-
87-
stmt.setLong(1, part.lower)
88-
stmt.setLong(2, part.upper)
89-
val rs = stmt.executeQuery()
90-
91-
} catch {
92-
case e: Exception =>
93-
close()
94-
logError("Exception occurred on creating connection/preparedStatement", e)
95-
throw e // Is it correct to throw Exception, or what is preferred cleanup here?
76+
// setFetchSize(Integer.MIN_VALUE) is a mysql driver specific way to force streaming results,
77+
// rather than pulling entire resultset into memory.
78+
// see http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-implementation-notes.html
79+
if (conn.getMetaData.getURL.matches("jdbc:mysql:.*")) {
80+
stmt.setFetchSize(Integer.MIN_VALUE)
81+
logInfo("statement fetch size set to: " + stmt.getFetchSize + " to force MySQL streaming ")
9682
}
9783

84+
stmt.setLong(1, part.lower)
85+
stmt.setLong(2, part.upper)
86+
val rs = stmt.executeQuery()
87+
9888
override def getNext: T = {
9989
if (rs.next()) {
10090
mapRow(rs)
@@ -116,7 +106,7 @@ class JdbcRDD[T: ClassTag](
116106
case e: Exception => logWarning("Exception closing statement", e)
117107
}
118108
try {
119-
if (null != conn && ! stmt.isClosed()) conn.close()
109+
if (null != conn && ! conn.isClosed()) conn.close()
120110
logInfo("closed connection")
121111
} catch {
122112
case e: Exception => logWarning("Exception closing connection", e)

0 commit comments

Comments
 (0)