What you will be able to do
- Decide when a JDBC Unity Catalog connection beats query federation for reading an external database
- Set up the driver volume, privileges and option allowlist a JDBC connection needs
- Push filtering to the source database with the query option instead of filtering in Spark
- Parallelise a large JDBC read with partitionColumn, lowerBound, upperBound and numPartitions
- Keep database credentials out of notebook source and job configuration
Key concept
JDBC Unity Catalog connection — A governed Unity Catalog object that holds the JDBC driver, the database URL and the credentials. A notebook reads through it by name with the Spark Data Source API, and the user running the notebook never sees the credentials.
1.When a notebook should read over JDBC
Reading an operational database from a notebook is the do-it-yourself end of ingestion. You write the Spark read, choose the SQL, and decide where the rows land. Databricks offers two governed ways to reach the same database, and they suit different jobs. Query federation exposes the database as a foreign catalog with table-level permissions and automatic pushdown. A JDBC Unity Catalog connection gives a notebook a named connection to read through with the full Spark Data Source API. If federation supports your database, Databricks recommends federation.
A JDBC connection is the better choice in a specific set of cases:
- the database is not supported by query federation
- you need a particular JDBC driver
- you need to write back to the source, which federation cannot do
- you want control over parallelism through Spark options
- you want to push a hand-written SQL statement to the source with the
queryoption
The trade-off is governance granularity. Federation can grant access table by table. A JDBC connection is governed only as a whole: a user who may use the connection can reach whatever its credentials can reach. A JDBC connection is also not a copy of the data. Rows you read sit in Spark memory only for the life of the job, so a notebook that ingests must write them to a Unity Catalog table itself. Databricks recommends addressing those tables by name rather than by storage path.
| Feature | Spark Data Source API | Lakehouse Federation | Lakeflow Connect |
|---|---|---|---|
| Primary use case | Complex ETL, custom Spark logic, pass-through queries | Ad-hoc queries, BI reporting | High-scale, automated ingestion |
| Data movement | Loaded into Spark memory (ephemeral) | Loaded into Spark memory (ephemeral) | Copied to Delta Lake (persistent) |
| Query execution | Manual pushdown using the native query option | Automatic pushdown of Spark and SQL filters, joins, and aggregations | Not applicable (full table replication) |
| Governance | Unity Catalog connection (JDBC) or secret scopes | Unity Catalog (federated catalog) | Unity Catalog (managed pipeline) |
The landing target decides how the rows are governed afterwards. Tabular results belong in a Unity Catalog table addressed by its three-part name, <catalog>.<schema>.<table>; Databricks recommends reading and writing tables by name rather than by cloud storage path. Raw or non-tabular output, such as JSON or CSV extracts, belongs in a volume under /Volumes/<catalog>/<schema>/<volume>, which Databricks recommends for all access to non-tabular data in cloud object storage. Underneath both sits an external location, a storage path paired with a storage credential. Grant users the table or volume, not the bucket: direct storage access bypasses Unity Catalog permissions, auditing and lineage.
Checkpoint 1 of 9· Check yourself
A notebook reads orders from a PostgreSQL database, which query federation supports. It enriches the rows in Spark and must then write a status value back to the same PostgreSQL table. Which way of reaching the database fits this job?
Databricks normally recommends federation for a supported database, but federation cannot write. Writing back to the source is one of the cases where a JDBC Unity Catalog connection is the right choice.
“You need to write to the data source using Spark (query federation doesn't support writes).”Source: docs.databricks.com
2.Creating the connection: driver, privileges and allowlist
The connection installs its driver from a Unity Catalog volume. You upload the JDBC driver JAR to a volume, and the connection loads it into an isolated sandbox on the compute. That arrangement creates three permission requirements. The creator needs the CREATE CONNECTION privilege on the metastore. The creator also needs CREATE or MANAGE access to the volume. Every user who queries the connection needs read access to that volume, which is granted with GRANT READ VOLUME ... TO the querying group. A user who has the connection but not the volume cannot load the driver.
Compute matters too. The connection works on serverless, and on Databricks Runtime 17.3 LTS or above in standard or dedicated access mode. It is generally available from Runtime 19. This is the route for serverless and standard clusters: they do not accept custom third-party Spark JARs, so a driver installed on a cluster is not an option there.
You create the connection in Catalog Explorer (Connect, then Connections, then connection type JDBC) or with CREATE CONNECTION in SQL. Only the URL and credentials are required. The externalOptionsAllowList setting controls which Spark options a querying user may add at read time. Its default is dbtable,query,partitionColumn,lowerBound,upperBound,numPartitions. Setting it to an empty string locks users to the options defined on the connection, and users can never override url or host. For SQL Server, where the URL does not pin the database, add database to the allowlist so that readers can choose it.
df = (spark.read
.format("jdbc")
.option("databricks.connection", "<connection-name>")
.option("query", "SELECT * FROM external_table")
.load())Checkpoint 2 of 9· Fill the gap
This notebook reads through a JDBC Unity Catalog connection by name, so it has no URL, user or password in it. Which option key completes the read?
df = (spark.read
.format("jdbc")
.option(" ? ", "<connection-name>")
.option("query", "SELECT * FROM external_table")
.load())The databricks.connection option names the Unity Catalog connection, which supplies the driver, URL and credentials. Users can never set url themselves, and dbtable names a source table, not a connection.
Source: docs.databricks.comCheckpoint 3 of 9· Check yourself
An analyst has been granted use of a JDBC Unity Catalog connection. When their serverless notebook reads through it, the JDBC driver fails to load. What fixes this?
The connection installs its driver from a Unity Catalog volume, so everyone who queries the connection needs read access to that volume. CREATE CONNECTION is only needed to create the connection, and serverless compute does not accept custom third-party JARs.
“Grant read access on the volume to the users who query the connection”Source: docs.databricks.com
3.Pushing filters to the source with the query option
With the Spark Data Source API, pushdown is your job. Federation rewrites Spark filters, joins and aggregations into source SQL automatically. A JDBC read is described as manual pushdown, done through the native query option. A read that loads a whole table and then calls .filter() and .select() in Spark gives no such guarantee; the one placement that is certain to run at the source is the SQL inside query.
The query option carries a SQL statement that runs entirely on the external database. Spark receives only that statement's result and does not push any transformation into the string. If a notebook needs four columns of yesterday's rows from a 50-column, 80-million-row table, write that projection and that WHERE clause into the SQL the source runs. The database then does the filtering, and only the narrow result travels to Databricks.
df = (spark.read
.format("sqlserver")
.option("host", "<your-sql-server-instance>.database.windows.net")
.option("user", dbutils.secrets.get(scope="<scope>", key="<user>"))
.option("password", dbutils.secrets.get(scope="<scope>", key="<password>"))
.option("database", "<database-name>")
.option("query", "SELECT id, name FROM users WHERE active = 1")
.load())Checkpoint 4 of 9· Check yourself
A notebook reads one 50-column table over JDBC and needs only four columns of yesterday's rows. Network transfer is the bottleneck. Which change makes sure the source database does the narrowing?
The query option sends a SQL statement that runs entirely on the external database, so filter logic executes there before data reaches Spark. Spark does not transform the query string, and Lakeflow Connect replicates whole tables.
“Using the query option ensures that filter and join logic executes on the source database before data reaches Spark.”Source: docs.databricks.com
Checkpoint 5 of 9· Exam question
A notebook currently reads an entire 50-column, 80 million-row transactions table over JDBC into a DataFrame, then immediately filters it in Spark to keep only rows from the last 24 hours and only 4 of the columns before writing the result to a Unity Catalog table. Networking between Databricks and the source database is the bottleneck. Which change reduces the amount of data pulled over the JDBC connection itself?
Correct answer: A — Replace the dbtable option with a parenthesized subquery that selects only the four needed columns and filters transaction_ts to the last 24 hours, so the source database performs the filtering before crossing the network.
- A. Passing a parenthesized subquery as
dbtablemakes the source database itself execute the column selection and date filter, so only the reduced result set is transferred over the JDBC connection instead of all 50 columns and every historical row. - B. JDBC pushdown of column and predicate pruning is not guaranteed for arbitrary sources, and relying on it after
.load()still risks pulling the full table across the network if pushdown does not occur for this configuration. - C. Adding partitions changes how the same full scan is split across connections; it does not reduce the total number of columns or rows read from the source, so total network transfer stays the same.
- D. A smaller
fetchSizeonly changes batching per round trip for the same full result set; it does not reduce the number of columns or rows pulled from the source database.
Sources5
4.Splitting a large read into parallel range queries
A JDBC read is serial unless you tell Spark how to split it. The split is given by a numeric or orderable column plus its range. Spark divides the range from lowerBound to upperBound into numPartitions slices and issues one range query per slice over separate connections. An evenly distributed, monotonically increasing key such as order_id is the ideal column, because every slice gets a similar share of rows. A skewed column leaves one task with most of the work.
| Option | Meaning | Note |
|---|---|---|
| partitionColumn (column in the jdbc method) | The column used to split the read | Requires lowerBound, upperBound and numPartitions |
| lowerBound | Minimum value of the column for partitioning | Required when the column is specified |
| upperBound | Maximum value of the column for partitioning | Required when the column is specified |
| numPartitions | Number of partitions, and therefore parallel queries | Too many on a large cluster can crash the source database |
| predicates | A list of WHERE expressions, one per partition | Ignored if a column is specified |
Choosing numPartitions sets how hard you hit the source. Every partition is a concurrent session on a database that is usually also serving production traffic. Raising the number shortens the read until the database becomes the bottleneck, and past that point it can take the database down. Start small and raise it only while the source stays healthy; values in the hundreds are too high. These four option names are in the connection's default allowlist, so a notebook reading through a JDBC Unity Catalog connection can set them without an administrator changing anything.
Partitioning and the query option do not mix. partitionColumn cannot be used with query, so a partitioned read names its source with dbtable instead, and all four partition options must be set together. A read that needs a hand-written SQL statement runs as a single query; a read that needs parallel range queries gives up the query option.
Checkpoint 6 of 9· Match them up
Match each JDBC read option to the role it plays when Spark splits one large table read into parallel queries.
Tap a term, then the definition that fits it.
Spark splits the range from lowerBound to upperBound on partitionColumn into numPartitions range queries. Each one is a concurrent query against the source. A predicates list sets the partitions with explicit WHERE clauses instead, and Spark ignores it when a column is specified.
“Partitions of the table are retrieved in parallel if either column or predicates is specified. If both column and predicates are specified, column takes precedence.”Source: docs.databricks.com
Checkpoint 7 of 9· Exam question
A data engineer runs a nightly notebook task in a Lakeflow Job that reads a 200 million-row orders table from an on-premises PostgreSQL database using spark.read.format("jdbc"). Without any partitioning options set, the job opens a single connection and the read consistently takes over three hours before timing out. The order_id column is a monotonically increasing integer primary key with values evenly distributed across the full range. Which change is most likely to reduce the read time while keeping the load on the source database controlled?
Correct answer: A — Set partitionColumn to order_id, provide lowerBound and upperBound matching the observed key range, and set numPartitions to a modest value such as 8 so Spark splits the scan into parallel range queries.
- A. Setting
partitionColumn,lowerBound,upperBound, and a modestnumPartitionslets Spark split the scan into several range-bounded queries that run in parallel, which is the documented way to speed up a large JDBC read without opening an unbounded number of connections. - B. A very large
fetchSizeonly changes how many rows are buffered per round trip on a single connection; it does not add parallelism, so it will not meaningfully shrink a three-hour single-connection scan of 200 million rows. - C. A bare
SELECT *subquery does not aggregate or filter anything, so it still pulls the full table across a single connection; it does not reduce data volume or add parallelism. - D. ODBC is a different client protocol, not an automatic parallelism mechanism, and it still requires explicit partitioning configuration to split a scan across multiple connections.
5.Keeping credentials out of notebooks and jobs
A database password written as a literal in an .option() call is readable by anyone who can open the notebook or the job that runs it. There are two supported places for credentials, and both keep the value out of the source code.
The first is the connection itself. A JDBC Unity Catalog connection stores the credentials, and querying users never see them. Do not embed credentials in the connection URL, because logs or error messages can expose them. Use the connection's dedicated credential fields instead. The second, for reads that do not go through a connection, is a secret scope. The notebook calls dbutils.secrets.get(scope=..., key=...) at run time, as in the SQL Server example above. Databricks makes an effort to redact secret values displayed in notebooks.
Two common fixes do not work. Base64 encoding is not protection, since anyone can decode it. Locking down the folder still leaves the plaintext in the code. A job parameter is not a secret store either. Parameters are plain configuration values that the job hands to its tasks by key; a default value is not a security control, and anyone with CAN MANAGE RUN permission or higher can override it for a manual run. The job definition itself can be opened as JSON or YAML, so a password placed there is readable by whoever can view the job.
Checkpoint 8 of 9· Check yourself
Your team runs a nightly Lakeflow job whose notebook has a SQL Server password written into an .option() call. You are moving the read onto a JDBC Unity Catalog connection so that people who can view the notebook or the job never see the password. Where should the password go?
A JDBC Unity Catalog connection keeps credentials hidden from querying users when they are stored in its dedicated credential fields. Logs or error messages can expose a password embedded in the URL. Anyone who can open the job definition can read a job parameter, and anyone can decode base64.
“Don't embed credentials in the URL, because logs or errors can expose them. Use the dedicated credential options for your chosen authentication method.”Source: docs.databricks.com
Checkpoint 9 of 9· Exam question
During a code review, a data engineer notices that a coworker's notebook task builds a JDBC connection string that includes the literal database username and password directly in the .option("password", ...) call, and this notebook is scheduled to run daily as part of a Lakeflow Job that several other engineers can view. What is the recommended way to fix this before the job is promoted to production?
Correct answer: A — Store the username and password in a Databricks secret scope and retrieve them at runtime with dbutils.secrets.get(), so the plaintext values never appear in the notebook source or job configuration.
- A. Secret scopes are the recommended mechanism for JDBC credentials: values retrieved with
dbutils.secrets.get()are redacted in notebook output and never stored as plaintext in the source or job configuration. - B. Ordinary Lakeflow Job task parameters are not encrypted or hidden by default, so putting a password in a plain parameter still exposes it in the job's configuration to anyone who can view the job.
- C. Base64 is a reversible encoding, not encryption, so anyone who can view the notebook can trivially decode the value; it provides no real credential protection.
- D. The engineers who can already run the scheduled job can still see the notebook contents, so folder-level restriction to admins does not stop the people who need to operate the job from seeing the plaintext password.
Sources
(10)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“If your database is supported, Databricks recommends using query federation instead of a JDBC connection.”
↩︎ When a notebook should read over JDBC“You need to write to the data source using Spark (query federation doesn't support writes).”
↩︎ When a notebook should read over JDBC“JDBC Unity Catalog connection provides governance only at the connection level.”
↩︎ When a notebook should read over JDBC“The JDBC connection reads and installs the JDBC driver JAR from a Unity Catalog volume.”
↩︎ Creating the connection: driver, privileges and allowlist“To create a connection, you must have the CREATE CONNECTION privilege on the metastore attached to the workspace.”
↩︎ Creating the connection: driver, privileges and allowlist“Volume access by the user querying the connection.”
↩︎ Creating the connection: driver, privileges and allowlist“Databricks compute must use serverless, or Databricks Runtime 17.3 LTS or above on standard mode or dedicated access mode.”
↩︎ Creating the connection: driver, privileges and allowlist“Set it to an empty string to restrict users to only the options defined in the connection. Users can never specify url or host.”
↩︎ Creating the connection: driver, privileges and allowlist“If not specified, the default is 'dbtable,query,partitionColumn,lowerBound,upperBound,numPartitions'.”
↩︎ Splitting a large read into parallel range queries“Connection credentials are hidden from the querying user.”
↩︎ Keeping credentials out of notebooks and jobs“Don't embed credentials in the URL, because logs or errors can expose them.”
↩︎ Keeping credentials out of notebooks and jobs“The JDBC connection is a securable object in Unity Catalog that specifies the JDBC driver, the URL path, and credentials”
↩︎ Key concept“Grant read access on the volume to the users who query the connection”
↩︎ Checkpoint“Don't embed credentials in the URL, because logs or errors can expose them. Use the dedicated credential options for your chosen authentication method.”
↩︎ Checkpoint - 2.
“Databricks recommends that users read and write all Unity Catalog tables using table names and access data in volumes using /Volumes paths.”
↩︎ When a notebook should read over JDBC“The only identity that should have access to data managed by Unity Catalog is the identity used by Unity Catalog.”
↩︎ When a notebook should read over JDBC - 3.
“Databricks recommends using volumes for managing all access to non-tabular data in cloud object storage”
↩︎ When a notebook should read over JDBC - 4.https://docs.databricks.com/aws/en/pyspark/basicsOfficial docs
“identifying the table using the format <catalog-name>.<schema-name>.<table-name>”
↩︎ When a notebook should read over JDBC - 5.
“Custom third-party Spark JARs are not supported on Databricks SQL, serverless, or standard access mode compute.”
↩︎ Creating the connection: driver, privileges and allowlist“When using the query option, the specified SQL statement runs entirely on the external data source.”
↩︎ Pushing filters to the source with the query option“Spark fetches the results without performing transformation pushdown on the query string.”
↩︎ Pushing filters to the source with the query option“Using the query option ensures that filter and join logic executes on the source database before data reaches Spark.”
↩︎ Pushing filters to the source with the query option“Manual pushdown using the native query option”
↩︎ Exam trap 1 - 6.
“Partitions of the table are retrieved in parallel if either column or predicates is specified.”
↩︎ Splitting a large read into parallel range queries“Avoid creating too many partitions in parallel on a large cluster, as this can crash external database systems.”
↩︎ Splitting a large read into parallel range queries“Avoid creating too many partitions in parallel on a large cluster, as this can crash external database systems.”
↩︎ Exam trap 2“Partitions of the table are retrieved in parallel if either column or predicates is specified. If both column and predicates are specified, column takes precedence.”
↩︎ Checkpoint - 7.
“you must use the dbtable option instead of query”
↩︎ Splitting a large read into parallel range queries“Do not set this too high (hundreds).”
↩︎ Splitting a large read into parallel range queries - 8.
“The secrets utility module contains commands to store and access sensitive credential information without making them visible in notebooks.”
↩︎ Keeping credentials out of notebooks and jobs“The secrets utility module contains commands to store and access sensitive credential information without making them visible in notebooks.”
↩︎ Exam trap 3 - 9.
“You can also view the JSON definitions for use with REST API get, create, and reset endpoints.”
↩︎ Keeping credentials out of notebooks and jobs“To view a job as YAML, click the kebab menu to the left of Run now for the job”
↩︎ Keeping credentials out of notebooks and jobs - 10.https://docs.databricks.com/aws/en/jobs/parametersOfficial docs
“A default parameter value is not a security control.”
↩︎ Keeping credentials out of notebooks and jobs