Establishing a Connection
Creating a JDBC Data Source
You can create a JDBC data source to connect from your Java application. Creating a JDBC data source based on the CData JDBC Driver for Lakebase consists of three basic steps:
- Add the driver JAR file to the classpath. The JAR file is located in the lib subfolder of the installation directory. Note that the .lic file must be located in the same folder as the JAR file.
- Provide the driver class. For example:
cdata.jdbc.lakebase.LakebaseDriver
- Provide the JDBC URL. For example:
jdbc:lakebase:User=lakebase;Password=admin;Database=lakebase;Server=127.0.0.1;Port=5432 or jdbc:cdata:lakebase:User=lakebase;Password=admin;Database=lakebase;Server=127.0.0.1;Port=5432
You can use the second format above whenever there is a conflict in your application between drivers using the same URL format to ensure you are using the CData driver. The URL must start with either "jdbc:lakebase:" or "jdbc:cdata:lakebase:" and can include any of the connection properties in name-value pairs separated with semicolons.
Connecting to Lakebase
To connect to Lakebase, set these properties:- DatabricksInstance: The Databricks instance or server hostname, provided in the format instance-abcdef12-3456-7890-abcd-abcdef123456.database.cloud.databricks.com .
- Server: The host name or IP address of the server hosting the Lakebase database.
- Port (optional): The port of the server hosting the Lakebase database, set to 5432 by default.
- Database (optional): The database to connect to after authenticating to the Lakebase Server, set to the authenticating user's default database by default.
Authentication Schemes for Lakebase
Lakebase supports two types of OAuth-based authentication schemes for calling workspace-level APIs: OAuthClient and OAuthPKCE.
OAuthClient
OAuthClient uses the OAuth client credentials grant type. This authentication scheme requires you to perform additional configuration on your service principal in order to connect, as described in Setting Up OAuthClient Authentication.Authentication is handled via the OAuth Client Credentials flow. This flow does not involve direct user authentication; rather, it uses credentials that apply only to the application itself. The CData JDBC Driver for Lakebase uses the service principal itself to authenticate with the associated permissions being defined in the service principal.
Since no embedded OAuth credentials are provided, specifying the OAuthClient authentication scheme requires you to perform extra configuration of your service principal, as described in Setting Up OAuthClient Authentication.
Set these configuration parameters:
- AuthScheme: OAuthClient.
- OAuthClientId: The client ID (also known as the consumer key) that was assigned when you configured the service principal. This ID is required to identify the application to the OAuth authorization server during authentication.
- OAuthClientSecret: The client secret (also known as the application secret or consumer secret) that was assigned when you configured the service principal. This confidential value is used to authenticate the application to the OAuth authorization server.
OAuthPKCE
OAuthPKCE uses the OAuth code grant type with PKCE (Proof Key for Code Exchange) to guard against cross-site request forgery and authorization code injection attacks.In this authentication scheme, authentication is handled via the use of a temporary code that the client exchanges for an access token. The code itself is obtained from the authorization server, where the user can see what information the client is requesting, and can either approve or deny the request.
Set these configuration parameters:
- AuthScheme: OAuthPKCE.
- User: The authenticating user's user ID.
Authenticating to Lakebase
Once you have set the configuration parameters required to connect to Lakebase, and the configuration parameters required for your chosen form of authentication, you can authenticate to Lakebase as described in the following sections.
Desktop Applications
The first time you authenticate to Lakebase from a desktop application you must set InitiateOAuth twice in the course of the OAuth flow:
- At initial login, you must set InitiateOAuth to GETANDREFRESH. This launches the login page and saves tokens.
- Once you have obtained valid access and refresh tokens, you can re-set InitiateOAuth to REFRESH. This reuses stored tokens without prompting the user again, which can be useful on unattended machines.
After authentication, tokens are saved to OAuthSettingsLocation. These values persist across sessions and are used to automatically refresh the access token when it expires. This means that upon subsequent connections you will not need to log in again.
Web Applications
When connecting via a Web application, you use the driver to acquire and manage the OAuth token values.Get an OAuth access token:
Once you have set OAuthClientId and OAuthClientSecret as described above, you obtain the OAuthAccessToken.
- For Authscheme=OAuthClient, set InitiateOAuth to GETANDREFRESH.
- For the user-interactive flow of Authscheme=OAuthPKCE, to complete the OAuth exchange, call stored procedures as follows:
- Call the GetOAuthAuthorizationUrl stored procedure. Set the CallbackURL input to the callback URL you specified in your application settings.
The stored procedure returns the URL to the OAuth endpoint. - Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL.
- Call the GetOAuthAuthorizationUrl stored procedure. Set the CallbackURL input to the callback URL you specified in your application settings.
Once you have obtained the access and refresh tokens, you can connect to data and refresh the OAuth access token either automatically or manually.
Automatic refresh of the OAuth access token:
To have the driver automatically refresh the OAuth access token, the first time you authenticate to Lakebase set the following connection parameters:
- InitiateOAuth: REFRESH.
- OAuthAccessToken: The access token returned by GetOAuthAccessToken.
- OAuthRefreshToken: The refresh token returned by GetOAuthAccessToken.
- OAuthSettingsLocation: The location where the driver saves the OAuth token values, which persist across connections.
On all subsequent data connections, the values for OAuthAccessToken and OAuthRefreshToken are taken from OAuthSettingsLocation.
Manual refresh of the OAuth access token:
The only value needed to manually refresh the OAuth access token when connecting to data is the OAuth refresh token.
First, use the RefreshOAuthAccessToken stored procedure to manually refresh the OAuthAccessToken.
Then call RefreshOAuthAccessToken with OAuthRefreshToken set to the OAuth refresh token returned by GetOAuthAccessToken. After the new tokens have been retrieved, open a new connection by setting the OAuthAccessToken property to the value returned by RefreshOAuthAccessToken.
Finally, store the OAuth refresh token so that you can use it to manually refresh the OAuth access token after it has expired.
Headless Machines
Headless environments like CI/CD pipelines, background services, or server-based integrations do not have an interactive browser. To authenticate using OAuthClient, you must complete the OAuth flow on a separate device with a browser and transfer the authentication result to the headless system.
Note: The following procedures are meant for use with the OAuthPKCE authscheme. Since the OAuthClient authscheme does not require browser interaction, it can make use of InitiateOAuth=GETANDREFRESH on a headless machine as well.
Setup options:
- Obtain and exchange a verifier code: Use another device to sign in and retrieve a verifier code, which the headless system uses to request tokens.
- Transfer an OAuth settings file: Authenticate on another device, then copy the stored token file to the headless environment.
Using a Verifier Code
- On a device with a browser:
- Set InitiateOAuth to OFF.
- Call the GetOAuthAuthorizationUrl stored procedure to generate a sign-in URL.
- Open the returned URL in a browser. Sign in and grant grant permissions to the driver.
You are redirected to the callback URL, which contains the verifier code. - After signing in, save the value of the code parameter from the redirect URL. You will use this later to set the OAuthVerifier connection property.
- At the headless machine:
- Set the following properties:
- InitiateOAuth: REFRESH
- OAuthClientId: The client Id that was generated when you configured the service principal.
- OAuthClientSecret: The client secret that was generated when you configured the service principal.
- After tokens are saved, reuse them by setting:
- InitiateOAuth: REFRESH
- OAuthSettingsLocation: Make sure this location grants read and write permissions to the driver to enable the automatic refreshing of the access token.
- Set the following properties:
Transferring OAuth Settings
- On a device with a browser, connect using the instructions in the Desktop Applications section.
After connecting, tokens are saved to the file path in OAuthSettingsLocation. The default filename is OAuthSettings.txt.
- On the headless machine:
- Copy the OAuth settings file to the machine.
- Set the following properties:
- InitiateOAuth: REFRESH
- OAuthSettingsLocation: Make sure this location grants read and write permissions to the driver to enable the automatic refreshing of the access token.
After setup, the driver uses the stored tokens to refresh the access token automatically. No browser or manual login is required.