Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/sql-programming-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,13 @@ the following case-sensitive options:
</td>
</tr>

<tr>
<td><code>maxConnections</code></td>
<td>
The maximum number of concurrent JDBC connections that can be used, if set. Only applies when writing. It works by limiting the operation's parallelism, which depends on the input's partition count. If its partition count exceeds this limit, the operation will coalesce the input to fewer partitions before writing.
</td>
</tr>

<tr>
<td><code>isolationLevel</code></td>
<td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class JDBCOptions(
case "REPEATABLE_READ" => Connection.TRANSACTION_REPEATABLE_READ
case "SERIALIZABLE" => Connection.TRANSACTION_SERIALIZABLE
}
// the maximum number of connections
val maxConnections = parameters.getOrElse(JDBC_MAX_CONNECTIONS, null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should avoid using null in scala, how about make maxConnections Option[Int]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! No problem. I'll update this PR again.

require(maxConnections == null || maxConnections.toInt > 0,
s"Invalid value `$maxConnections` for parameter `$JDBC_MAX_CONNECTIONS`. " +
"The minimum value is 1.")
}

object JDBCOptions {
Expand All @@ -144,4 +149,5 @@ object JDBCOptions {
val JDBC_CREATE_TABLE_OPTIONS = newOption("createTableOptions")
val JDBC_BATCH_INSERT_SIZE = newOption("batchsize")
val JDBC_TXN_ISOLATION_LEVEL = newOption("isolationLevel")
val JDBC_MAX_CONNECTIONS = newOption("maxConnections")
}
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,14 @@ object JdbcUtils extends Logging {
val getConnection: () => Connection = createConnectionFactory(options)
val batchSize = options.batchSize
val isolationLevel = options.isolationLevel
df.foreachPartition(iterator => savePartition(
val maxConnections = options.maxConnections
val repartitionedDF =
if (maxConnections != null && maxConnections.toInt < df.rdd.getNumPartitions) {
Copy link
Member

@gatorsmile gatorsmile Nov 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the input is less than 1, we should detect it at the beginning, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I'll add require like batchsize.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done.

df.coalesce(maxConnections.toInt)
} else {
df
}
repartitionedDF.foreachPartition(iterator => savePartition(
getConnection, table, iterator, rddSchema, nullTypes, batchSize, dialect, isolationLevel)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,16 @@ class JDBCWriteSuite extends SharedSQLContext with BeforeAndAfter {
.options(properties.asScala)
.save()
}

test("SPARK-18413: Add `maxConnections` JDBCOption") {
val df = spark.createDataFrame(sparkContext.parallelize(arr2x2), schema2)
val e = intercept[IllegalArgumentException] {
df.write.format("jdbc")
.option("dbtable", "TEST.SAVETEST")
.option("url", url1)
.option(s"${JDBCOptions.JDBC_MAX_CONNECTIONS}", "0")
.save()
}.getMessage
assert(e.contains("Invalid value `0` for parameter `maxConnections`. The minimum value is 1"))
}
}