Skip to content

[SPARK-18413][SQL] Add maxConnections JDBCOption #15868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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.get(JDBC_MAX_CONNECTIONS).map(_.toInt)
require(maxConnections.isEmpty || maxConnections.get > 0,
s"Invalid value `${maxConnections.get}` 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.isDefined && maxConnections.get < df.rdd.getNumPartitions) {
df.coalesce(maxConnections.get)
} 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"))
}
}