Class DatabricksConnection
- java.lang.Object
-
- com.databricks.jdbc.api.impl.DatabricksConnection
-
- All Implemented Interfaces:
IDatabricksConnection,IDatabricksConnectionInternal,AutoCloseable,Connection,Wrapper
public class DatabricksConnection extends Object implements IDatabricksConnection, IDatabricksConnectionInternal
Implementation for Databricks specific connection.
-
-
Field Summary
-
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
-
-
Constructor Summary
Constructors Constructor Description DatabricksConnection(IDatabricksConnectionContext connectionContext)Creates an instance of Databricks connection for given connection context.DatabricksConnection(IDatabricksConnectionContext connectionContext, IDatabricksClient testDatabricksClient)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort(Executor executor)voidbeginRequest()voidclearWarnings()voidclose()voidcloseStatement(IDatabricksStatement statement)Closes a statement from the connection's active set.voidcommit()Makes all changes made since the previous commit/rollback permanent.ArraycreateArrayOf(String typeName, Object[] elements)BlobcreateBlob()ClobcreateClob()NClobcreateNClob()SQLXMLcreateSQLXML()StatementcreateStatement()StatementcreateStatement(int resultSetType, int resultSetConcurrency)StatementcreateStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)StructcreateStruct(String typeName, Object[] attributes)voidendRequest()booleangetAutoCommit()Retrieves the current auto-commit mode for this connection.StringgetCatalog()PropertiesgetClientInfo()Retrieves all client and session properties as a Properties object.StringgetClientInfo(String name)Retrieves the value of the specified client info property. case-insensitiveConnectiongetConnection()Returns the corresponding sql connection objectIDatabricksConnectionContextgetConnectionContext()Returns the connection context associated with the connection.StringgetConnectionId()Retrieves the unique identifier for this connection.intgetHoldability()DatabaseMetaDatagetMetaData()intgetNetworkTimeout()StringgetSchema()IDatabricksSessiongetSession()Returns the underlying session for the connection.StatementgetStatement(String statementId)Retrieves a statement handle for a given statement ID.intgetTransactionIsolation()Map<String,Class<?>>getTypeMap()SQLWarninggetWarnings()booleanisClosed()booleanisReadOnly()booleanisValid(int timeout)booleanisWrapperFor(Class<?> iface)StringnativeSQL(String sql)voidopen()Opens the connection and initiates the underlying sessionCallableStatementprepareCall(String sql)CallableStatementprepareCall(String sql, int resultSetType, int resultSetConcurrency)CallableStatementprepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)PreparedStatementprepareStatement(String sql)PreparedStatementprepareStatement(String sql, int autoGeneratedKeys)PreparedStatementprepareStatement(String sql, int[] columnIndexes)PreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency)PreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)PreparedStatementprepareStatement(String sql, String[] columnNames)voidreleaseSavepoint(Savepoint savepoint)voidrollback()Undoes all changes made in the current transaction.voidrollback(Savepoint savepoint)voidsetAutoCommit(boolean autoCommit)Sets the auto-commit mode for this connection to the given state.voidsetCatalog(String catalog)voidsetClientInfo(String name, String value)Sets a client info property/session configvoidsetClientInfo(Properties properties)Sets multiple client info properties from the provided Properties object.voidsetHoldability(int holdability)voidsetNetworkTimeout(Executor executor, int milliseconds)voidsetReadOnly(boolean readOnly)SavepointsetSavepoint()SavepointsetSavepoint(String name)voidsetSchema(String schema)voidsetShardingKey(ShardingKey shardingKey)voidsetShardingKey(ShardingKey shardingKey, ShardingKey superShardingKey)booleansetShardingKeyIfValid(ShardingKey shardingKey, int timeout)booleansetShardingKeyIfValid(ShardingKey shardingKey, ShardingKey superShardingKey, int timeout)voidsetTransactionIsolation(int level)voidsetTypeMap(Map<String,Class<?>> map)<T> Tunwrap(Class<T> iface)
-
-
-
Constructor Detail
-
DatabricksConnection
public DatabricksConnection(IDatabricksConnectionContext connectionContext) throws DatabricksSQLException
Creates an instance of Databricks connection for given connection context.- Parameters:
connectionContext- underlying connection context- Throws:
DatabricksSQLException
-
DatabricksConnection
public DatabricksConnection(IDatabricksConnectionContext connectionContext, IDatabricksClient testDatabricksClient) throws DatabricksSQLException
- Throws:
DatabricksSQLException
-
-
Method Detail
-
open
public void open() throws DatabricksSQLExceptionDescription copied from interface:IDatabricksConnectionInternalOpens the connection and initiates the underlying session- Specified by:
openin interfaceIDatabricksConnectionInternal- Throws:
DatabricksSQLException
-
getStatement
public Statement getStatement(String statementId) throws SQLException
Description copied from interface:IDatabricksConnectionRetrieves a statement handle for a given statement ID.- Specified by:
getStatementin interfaceIDatabricksConnection- Parameters:
statementId- The unique identifier of the statement to retrieve- Returns:
- A
Statementobject representing the statement - Throws:
SQLException- if a database access error occurs or this method is called on a closed connection
-
getConnectionId
public String getConnectionId() throws SQLException
Description copied from interface:IDatabricksConnectionRetrieves the unique identifier for this connection.- Specified by:
getConnectionIdin interfaceIDatabricksConnection- Returns:
- A string representing the unique connection ID
- Throws:
SQLException- if a database access error occurs or this method is called on a closed connection
-
getSession
public IDatabricksSession getSession()
Description copied from interface:IDatabricksConnectionInternalReturns the underlying session for the connection.- Specified by:
getSessionin interfaceIDatabricksConnectionInternal
-
createStatement
public Statement createStatement() throws SQLException
- Specified by:
createStatementin interfaceConnection- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql) throws SQLException
- Specified by:
prepareStatementin interfaceConnection- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(String sql) throws SQLException
- Specified by:
prepareCallin interfaceConnection- Throws:
SQLException
-
nativeSQL
public String nativeSQL(String sql) throws SQLException
- Specified by:
nativeSQLin interfaceConnection- Throws:
SQLException
-
setAutoCommit
public void setAutoCommit(boolean autoCommit) throws SQLExceptionSets the auto-commit mode for this connection to the given state.When auto-commit is enabled (the default), each SQL statement is executed as an individual transaction and is committed immediately upon completion.
When auto-commit is disabled, SQL statements are grouped into transactions that must be explicitly committed via
commit()or rolled back viarollback(). When auto-commit is disabled, a new transaction is automatically started:- Immediately after executing SET AUTOCOMMIT = FALSE
- After each COMMIT or ROLLBACK statement
Thread Safety
This method is not thread-safe. The
Connectionobject should not be shared across multiple threads. Concurrent access may lead to undefined behavior and server-side transaction aborts due to sequence ID mismatches. Each thread should obtain its ownConnectionfrom a connection pool.Example Usage
// Disable auto-commit to start a transaction connection.setAutoCommit(false); try { // Execute multiple statements as part of one transaction statement.executeUpdate("UPDATE accounts SET balance = balance - 100 WHERE id = 1"); statement.executeUpdate("UPDATE accounts SET balance = balance + 100 WHERE id = 2"); // Commit the transaction connection.commit(); } catch (SQLException e) { // Rollback on error connection.rollback(); throw e; }- Specified by:
setAutoCommitin interfaceConnection- Parameters:
autoCommit-trueto enable auto-commit mode;falseto disable it- Throws:
DatabricksSQLException- if the connection is closedDatabricksTransactionException- if the auto-commit mode cannot be changed due to an active transaction or invalid stateSQLException- See Also:
getAutoCommit(),commit(),rollback()
-
getAutoCommit
public boolean getAutoCommit() throws SQLExceptionRetrieves the current auto-commit mode for this connection.On a newly created connection, returns
true(JDBC default) without making a server round-trip.After
setAutoCommit(boolean)is called, returns the cached value from the session.If the connection property
FetchAutoCommitFromServer=1is set, this method will query the server usingSET AUTOCOMMITSQL command to retrieve the current auto-commit state, ensuring the returned value matches the server state. This is useful for debugging or when strict state verification is needed.- Specified by:
getAutoCommitin interfaceConnection- Returns:
- true if auto-commit mode is enabled; false otherwise
- Throws:
DatabricksSQLException- if the connection is closedDatabricksSQLException- if querying the server fails (when FetchAutoCommitFromServer=1)SQLException- See Also:
setAutoCommit(boolean)
-
commit
public void commit() throws SQLExceptionMakes all changes made since the previous commit/rollback permanent.This method should be used only when auto-commit mode has been disabled.
When auto-commit is FALSE:
- Commits the current transaction
- A new transaction begins automatically
When auto-commit is TRUE:
- This operation throws
DatabricksTransactionException(if ignoreTransactions flag is not set)
- Specified by:
commitin interfaceConnection- Throws:
DatabricksSQLException- if the connection is closedDatabricksTransactionException- for transaction-specific errors such as MULTI_STATEMENT_TRANSACTION_NO_ACTIVE_TRANSACTION or MULTI_STATEMENT_TRANSACTION_ROLLBACK_REQUIRED_AFTER_ABORTSQLException- See Also:
setAutoCommit(boolean),rollback()
-
rollback
public void rollback() throws SQLExceptionUndoes all changes made in the current transaction.This method should be used only when auto-commit mode has been disabled.
When auto-commit is FALSE:
- Rolls back the current transaction
- A new transaction begins automatically (per autocommit design)
When auto-commit is TRUE:
- ROLLBACK is a safe no-op (does not throw exception)
- This is more forgiving than COMMIT, which throws an exception when there's no active transaction
Note: ROLLBACK is designed to be safe to call even when there is no active transaction. It can be used to recover from error states without needing to check transaction status first.
- Specified by:
rollbackin interfaceConnection- Throws:
DatabricksSQLException- if the connection is closedDatabricksTransactionException- for transaction-specific errors (rare - ROLLBACK is typically very forgiving)SQLException- See Also:
setAutoCommit(boolean),commit()
-
close
public void close() throws DatabricksSQLException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceConnection- Throws:
DatabricksSQLException
-
isClosed
public boolean isClosed() throws SQLException- Specified by:
isClosedin interfaceConnection- Throws:
SQLException
-
getMetaData
public DatabaseMetaData getMetaData() throws SQLException
- Specified by:
getMetaDatain interfaceConnection- Throws:
SQLException
-
setReadOnly
public void setReadOnly(boolean readOnly) throws SQLException- Specified by:
setReadOnlyin interfaceConnection- Throws:
SQLException
-
isReadOnly
public boolean isReadOnly() throws SQLException- Specified by:
isReadOnlyin interfaceConnection- Throws:
SQLException
-
setCatalog
public void setCatalog(String catalog) throws SQLException
- Specified by:
setCatalogin interfaceConnection- Throws:
SQLException
-
getCatalog
public String getCatalog() throws SQLException
- Specified by:
getCatalogin interfaceConnection- Throws:
SQLException
-
setTransactionIsolation
public void setTransactionIsolation(int level) throws SQLException- Specified by:
setTransactionIsolationin interfaceConnection- Throws:
SQLException
-
getTransactionIsolation
public int getTransactionIsolation() throws SQLException- Specified by:
getTransactionIsolationin interfaceConnection- Throws:
SQLException
-
getWarnings
public SQLWarning getWarnings() throws SQLException
- Specified by:
getWarningsin interfaceConnection- Throws:
SQLException
-
clearWarnings
public void clearWarnings() throws SQLException- Specified by:
clearWarningsin interfaceConnection- Throws:
SQLException
-
createStatement
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
- Specified by:
createStatementin interfaceConnection- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
- Specified by:
prepareStatementin interfaceConnection- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
- Specified by:
prepareCallin interfaceConnection- Throws:
SQLException
-
getTypeMap
public Map<String,Class<?>> getTypeMap()
- Specified by:
getTypeMapin interfaceConnection
-
setTypeMap
public void setTypeMap(Map<String,Class<?>> map) throws SQLException
- Specified by:
setTypeMapin interfaceConnection- Throws:
SQLException
-
setHoldability
public void setHoldability(int holdability) throws SQLException- Specified by:
setHoldabilityin interfaceConnection- Throws:
SQLException
-
getHoldability
public int getHoldability() throws SQLException- Specified by:
getHoldabilityin interfaceConnection- Throws:
SQLException
-
setSavepoint
public Savepoint setSavepoint() throws SQLException
- Specified by:
setSavepointin interfaceConnection- Throws:
SQLException
-
setSavepoint
public Savepoint setSavepoint(String name) throws SQLException
- Specified by:
setSavepointin interfaceConnection- Throws:
SQLException
-
rollback
public void rollback(Savepoint savepoint) throws SQLException
- Specified by:
rollbackin interfaceConnection- Throws:
SQLException
-
releaseSavepoint
public void releaseSavepoint(Savepoint savepoint) throws SQLException
- Specified by:
releaseSavepointin interfaceConnection- Throws:
SQLException
-
createStatement
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
- Specified by:
createStatementin interfaceConnection- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
- Specified by:
prepareStatementin interfaceConnection- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
- Specified by:
prepareCallin interfaceConnection- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
- Specified by:
prepareStatementin interfaceConnection- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
- Specified by:
prepareStatementin interfaceConnection- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
- Specified by:
prepareStatementin interfaceConnection- Throws:
SQLException
-
createClob
public Clob createClob() throws SQLException
- Specified by:
createClobin interfaceConnection- Throws:
SQLException
-
createBlob
public Blob createBlob() throws SQLException
- Specified by:
createBlobin interfaceConnection- Throws:
SQLException
-
createNClob
public NClob createNClob() throws SQLException
- Specified by:
createNClobin interfaceConnection- Throws:
SQLException
-
createSQLXML
public SQLXML createSQLXML() throws SQLException
- Specified by:
createSQLXMLin interfaceConnection- Throws:
SQLException
-
isValid
public boolean isValid(int timeout) throws SQLException- Specified by:
isValidin interfaceConnection- Throws:
SQLException
-
setClientInfo
public void setClientInfo(String name, String value) throws SQLClientInfoException
Sets a client info property/session config- Specified by:
setClientInfoin interfaceConnection- Parameters:
name- The name of the property to setvalue- The value to set- Throws:
SQLClientInfoException- If the property cannot be set due to validation errors or if the property name is not recognized
-
setClientInfo
public void setClientInfo(Properties properties) throws SQLClientInfoException
Sets multiple client info properties from the provided Properties object.- Specified by:
setClientInfoin interfaceConnection- Parameters:
properties- The properties containing client info to set- Throws:
SQLClientInfoException- If any property cannot be set
-
getClientInfo
public String getClientInfo(String name) throws SQLException
Retrieves the value of the specified client info property. case-insensitive- Specified by:
getClientInfoin interfaceConnection- Parameters:
name- The name of the client info property to retrieve- Returns:
- The value of the specified client info property, or null if not found
- Throws:
SQLException- If a database access error occurs
-
getClientInfo
public Properties getClientInfo() throws SQLException
Retrieves all client and session properties as a Properties object. Keys are in lower case.The returned Properties object contains default session configurations, user-defined session configurations, and client info properties.
- Specified by:
getClientInfoin interfaceConnection- Returns:
- A Properties object containing all client info properties
- Throws:
SQLException- If a database access error occurs
-
createArrayOf
public Array createArrayOf(String typeName, Object[] elements) throws SQLException
- Specified by:
createArrayOfin interfaceConnection- Throws:
SQLException
-
createStruct
public Struct createStruct(String typeName, Object[] attributes) throws SQLException
- Specified by:
createStructin interfaceConnection- Throws:
SQLException
-
setSchema
public void setSchema(String schema) throws SQLException
- Specified by:
setSchemain interfaceConnection- Throws:
SQLException
-
getSchema
public String getSchema() throws SQLException
- Specified by:
getSchemain interfaceConnection- Throws:
SQLException
-
abort
public void abort(Executor executor) throws SQLException
- Specified by:
abortin interfaceConnection- Throws:
SQLException
-
setNetworkTimeout
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException
- Specified by:
setNetworkTimeoutin interfaceConnection- Throws:
SQLException
-
getNetworkTimeout
public int getNetworkTimeout() throws SQLException- Specified by:
getNetworkTimeoutin interfaceConnection- Throws:
SQLException
-
unwrap
public <T> T unwrap(Class<T> iface) throws SQLException
- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
isWrapperFor
public boolean isWrapperFor(Class<?> iface) throws SQLException
- Specified by:
isWrapperForin interfaceWrapper- Throws:
SQLException
-
closeStatement
public void closeStatement(IDatabricksStatement statement)
Description copied from interface:IDatabricksConnectionInternalCloses a statement from the connection's active set.- Specified by:
closeStatementin interfaceIDatabricksConnectionInternal- Parameters:
statement-IDatabricksStatementto be closed
-
beginRequest
public void beginRequest()
- Specified by:
beginRequestin interfaceConnection
-
endRequest
public void endRequest()
- Specified by:
endRequestin interfaceConnection
-
setShardingKeyIfValid
public boolean setShardingKeyIfValid(ShardingKey shardingKey, ShardingKey superShardingKey, int timeout) throws DatabricksSQLFeatureNotImplementedException
- Specified by:
setShardingKeyIfValidin interfaceConnection- Throws:
DatabricksSQLFeatureNotImplementedException
-
setShardingKeyIfValid
public boolean setShardingKeyIfValid(ShardingKey shardingKey, int timeout) throws DatabricksSQLFeatureNotImplementedException
- Specified by:
setShardingKeyIfValidin interfaceConnection- Throws:
DatabricksSQLFeatureNotImplementedException
-
setShardingKey
public void setShardingKey(ShardingKey shardingKey, ShardingKey superShardingKey) throws DatabricksSQLFeatureNotImplementedException
- Specified by:
setShardingKeyin interfaceConnection- Throws:
DatabricksSQLFeatureNotImplementedException
-
setShardingKey
public void setShardingKey(ShardingKey shardingKey) throws DatabricksSQLFeatureNotImplementedException
- Specified by:
setShardingKeyin interfaceConnection- Throws:
DatabricksSQLFeatureNotImplementedException
-
getConnection
public Connection getConnection()
Description copied from interface:IDatabricksConnectionInternalReturns the corresponding sql connection object- Specified by:
getConnectionin interfaceIDatabricksConnectionInternal
-
getConnectionContext
public IDatabricksConnectionContext getConnectionContext()
Description copied from interface:IDatabricksConnectionInternalReturns the connection context associated with the connection.- Specified by:
getConnectionContextin interfaceIDatabricksConnectionInternal
-
-