Package org.h2.jdbc
Class JdbcConnection
java.lang.Object
org.h2.message.TraceObject
org.h2.jdbc.JdbcConnection
- All Implemented Interfaces:
AutoCloseable
,Connection
,Wrapper
,CastDataProvider
public class JdbcConnection
extends org.h2.message.TraceObject
implements Connection, CastDataProvider
Represents a connection (session) to a database.
Thread safety: the connection is thread-safe. Different statements from the same connection may try to execute their commands in parallel, but they will be executed sequentially. If real concurrent execution of these commands is needed, different connections should be used.
-
Field Summary
Fields inherited from class org.h2.message.TraceObject
ARRAY, BLOB, CALLABLE_STATEMENT, CLOB, CONNECTION, DATA_SOURCE, DATABASE_META_DATA, PARAMETER_META_DATA, PREPARED_STATEMENT, RESULT_SET, RESULT_SET_META_DATA, SAVEPOINT, SQLXML, STATEMENT, trace, XA_DATA_SOURCE, XID
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE
-
Constructor Summary
ConstructorDescriptionJdbcConnection
(String url, Properties info, String user, Object password, boolean forbidCreation) INTERNAL the session closable object does not leak as Eclipse warns - due to the CloseWatcher.JdbcConnection
(Session session, String user, String url) INTERNALJdbcConnection
(JdbcConnection clone) INTERNAL -
Method Summary
Modifier and TypeMethodDescriptionvoid
[Not supported]protected void
INTERNAL.void
Clears all warnings.void
close()
Closes this connection.void
commit()
Commits the current transaction.createArrayOf
(String typeName, Object[] elements) Create a new Array object.Create a new empty Blob object.Create a new empty Clob object.Create a new empty NClob object.Create a new SQLXML object with no data.Creates a new statement.createStatement
(int resultSetType, int resultSetConcurrency) Creates a statement with the specified result set type and concurrency.createStatement
(int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a statement with the specified result set type, concurrency, and holdability.createStruct
(String typeName, Object[] attributes) [Not supported] Create a new empty Struct object.org.h2.value.ValueTimestampTimeZone
Returns the current timestamp with maximum resolution.org.h2.util.TimeZoneProvider
Returns the current time zone.boolean
Gets the current setting for auto commit.Gets the current catalog name.Get the client properties.getClientInfo
(String name) Get a client property.int
Returns the current result set holdability.Returns the custom Java object serializer, ornull
.Gets the database meta data for this database.getMode()
Returns the database mode.int
[Not supported]Retrieves this current schema name for this connection.INTERNALINTERNALint
Returns the current transaction isolation level.Gets the type map.Gets the first warning reported by calls on this object.boolean
isClosed()
Returns true if this connection has been closed.boolean
Returns true if the database is read-only.boolean
isValid
(int timeout) Returns true if this connection is still valid.boolean
isWrapperFor
(Class<?> iface) Checks if unwrap can return an object of this class.protected final void
lock()
Locks this connection with a reentrant lock.Translates a SQL statement into the database grammar.prepareCall
(String sql) Creates a new callable statement.prepareCall
(String sql, int resultSetType, int resultSetConcurrency) Creates a callable statement with the specified result set type and concurrency.prepareCall
(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a callable statement with the specified result set type, concurrency, and holdability.prepareStatement
(String sql) Creates a new prepared statement.prepareStatement
(String sql, int autoGeneratedKeys) Creates a new prepared statement.prepareStatement
(String sql, int[] columnIndexes) Creates a new prepared statement.prepareStatement
(String sql, int resultSetType, int resultSetConcurrency) Creates a prepared statement with the specified result set type and concurrency.prepareStatement
(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a prepared statement with the specified result set type, concurrency, and holdability.prepareStatement
(String sql, String[] columnNames) Creates a new prepared statement.void
releaseSavepoint
(Savepoint savepoint) Releases a savepoint.void
rollback()
Rolls back the current transaction.void
Rolls back to a savepoint.void
setAutoCommit
(boolean autoCommit) Switches auto commit on or off.void
setCatalog
(String catalog) Set the default catalog name.void
setClientInfo
(String name, String value) Set a client property.void
setClientInfo
(Properties properties) Set the client properties.void
setHoldability
(int holdability) Changes the current result set holdability.void
setNetworkTimeout
(Executor executor, int milliseconds) [Not supported]void
setReadOnly
(boolean readOnly) According to the JDBC specs, this setting is only a hint to the database to enable optimizations - it does not cause writes to be prohibited.Creates a new unnamed savepoint.setSavepoint
(String name) Creates a new named savepoint.void
Sets the given schema name to access.void
setTransactionIsolation
(int level) Changes the current transaction isolation level.void
setTypeMap
(Map<String, Class<?>> map) [Partially supported] Sets the type map.toString()
INTERNALprotected final void
unlock()
Unlocks this connection.<T> T
Return an object of this class if possible.boolean
Returns are ENUM values 0-based.Methods inherited from class org.h2.message.TraceObject
debugCode, debugCodeAssign, debugCodeCall, debugCodeCall, debugCodeCall, getNextId, getTraceId, getTraceObjectName, isDebugEnabled, isInfoEnabled, logAndConvert, quote, quoteArray, quoteBigDecimal, quoteBytes, quoteDate, quoteIntArray, quoteMap, quoteTime, quoteTimestamp, setTrace, unsupported
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.sql.Connection
beginRequest, endRequest, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid
-
Constructor Details
-
JdbcConnection
public JdbcConnection(String url, Properties info, String user, Object password, boolean forbidCreation) throws SQLException INTERNAL the session closable object does not leak as Eclipse warns - due to the CloseWatcher.- Parameters:
url
- of this connectioninfo
- of this connectionuser
- of this connectionpassword
- for the userforbidCreation
- whether database creation is forbidden- Throws:
SQLException
- on failure
-
JdbcConnection
INTERNAL- Parameters:
clone
- connection to clone
-
JdbcConnection
INTERNAL- Parameters:
session
- of this connectionuser
- of this connectionurl
- of this connection
-
-
Method Details
-
lock
protected final void lock()Locks this connection with a reentrant lock.lock(); try { ... } finally { unlock(); }
-
unlock
protected final void unlock()Unlocks this connection.- See Also:
-
createStatement
Creates a new statement.- Specified by:
createStatement
in interfaceConnection
- Returns:
- the new statement
- Throws:
SQLException
- if the connection is closed
-
createStatement
Creates a statement with the specified result set type and concurrency.- Specified by:
createStatement
in interfaceConnection
- Parameters:
resultSetType
- the result set type (ResultSet.TYPE_*)resultSetConcurrency
- the concurrency (ResultSet.CONCUR_*)- Returns:
- the statement
- Throws:
SQLException
- if the connection is closed or the result set type or concurrency are not supported
-
createStatement
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Creates a statement with the specified result set type, concurrency, and holdability.- Specified by:
createStatement
in interfaceConnection
- Parameters:
resultSetType
- the result set type (ResultSet.TYPE_*)resultSetConcurrency
- the concurrency (ResultSet.CONCUR_*)resultSetHoldability
- the holdability (ResultSet.HOLD* / CLOSE*)- Returns:
- the statement
- Throws:
SQLException
- if the connection is closed or the result set type, concurrency, or holdability are not supported
-
prepareStatement
Creates a new prepared statement.- Specified by:
prepareStatement
in interfaceConnection
- Parameters:
sql
- the SQL statement- Returns:
- the prepared statement
- Throws:
SQLException
- if the connection is closed
-
getMetaData
Gets the database meta data for this database.- Specified by:
getMetaData
in interfaceConnection
- Returns:
- the database meta data
- Throws:
SQLException
- if the connection is closed
-
getSession
INTERNAL- Returns:
- session
-
close
Closes this connection. All open statements, prepared statements and result sets that where created by this connection become invalid after calling this method. If there is an uncommitted transaction, it will be rolled back.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceConnection
- Throws:
SQLException
-
setAutoCommit
Switches auto commit on or off. Enabling it commits an uncommitted transaction, if there is one.- Specified by:
setAutoCommit
in interfaceConnection
- Parameters:
autoCommit
- true for auto commit on, false for off- Throws:
SQLException
- if the connection is closed
-
getAutoCommit
Gets the current setting for auto commit.- Specified by:
getAutoCommit
in interfaceConnection
- Returns:
- true for on, false for off
- Throws:
SQLException
- if the connection is closed
-
commit
Commits the current transaction. This call has only an effect if auto commit is switched off.- Specified by:
commit
in interfaceConnection
- Throws:
SQLException
- if the connection is closed
-
rollback
Rolls back the current transaction. This call has only an effect if auto commit is switched off.- Specified by:
rollback
in interfaceConnection
- Throws:
SQLException
- if the connection is closed
-
isClosed
Returns true if this connection has been closed.- Specified by:
isClosed
in interfaceConnection
- Returns:
- true if close was called
- Throws:
SQLException
-
nativeSQL
Translates a SQL statement into the database grammar.- Specified by:
nativeSQL
in interfaceConnection
- Parameters:
sql
- the SQL statement with or without JDBC escape sequences- Returns:
- the translated statement
- Throws:
SQLException
- if the connection is closed
-
setReadOnly
According to the JDBC specs, this setting is only a hint to the database to enable optimizations - it does not cause writes to be prohibited.- Specified by:
setReadOnly
in interfaceConnection
- Parameters:
readOnly
- ignored- Throws:
SQLException
- if the connection is closed
-
isReadOnly
Returns true if the database is read-only.- Specified by:
isReadOnly
in interfaceConnection
- Returns:
- if the database is read-only
- Throws:
SQLException
- if the connection is closed
-
setCatalog
Set the default catalog name. This call is ignored.- Specified by:
setCatalog
in interfaceConnection
- Parameters:
catalog
- ignored- Throws:
SQLException
- if the connection is closed
-
getCatalog
Gets the current catalog name.- Specified by:
getCatalog
in interfaceConnection
- Returns:
- the catalog name
- Throws:
SQLException
- if the connection is closed
-
getWarnings
Gets the first warning reported by calls on this object.- Specified by:
getWarnings
in interfaceConnection
- Returns:
- null
- Throws:
SQLException
-
clearWarnings
Clears all warnings.- Specified by:
clearWarnings
in interfaceConnection
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException Creates a prepared statement with the specified result set type and concurrency.- Specified by:
prepareStatement
in interfaceConnection
- Parameters:
sql
- the SQL statementresultSetType
- the result set type (ResultSet.TYPE_*)resultSetConcurrency
- the concurrency (ResultSet.CONCUR_*)- Returns:
- the prepared statement
- Throws:
SQLException
- if the connection is closed or the result set type or concurrency are not supported
-
setTransactionIsolation
Changes the current transaction isolation level. Calling this method will commit an open transaction, even if the new level is the same as the old one.- Specified by:
setTransactionIsolation
in interfaceConnection
- Parameters:
level
- the new transaction isolation level: Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, 6 (SNAPSHOT), or Connection.TRANSACTION_SERIALIZABLE- Throws:
SQLException
- if the connection is closed or the isolation level is not valid
-
getTransactionIsolation
Returns the current transaction isolation level.- Specified by:
getTransactionIsolation
in interfaceConnection
- Returns:
- the isolation level
- Throws:
SQLException
- if the connection is closed
-
setHoldability
Changes the current result set holdability.- Specified by:
setHoldability
in interfaceConnection
- Parameters:
holdability
- ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT;- Throws:
SQLException
- if the connection is closed or the holdability is not supported
-
getHoldability
Returns the current result set holdability.- Specified by:
getHoldability
in interfaceConnection
- Returns:
- the holdability
- Throws:
SQLException
- if the connection is closed
-
getTypeMap
Gets the type map.- Specified by:
getTypeMap
in interfaceConnection
- Returns:
- null
- Throws:
SQLException
- if the connection is closed
-
setTypeMap
[Partially supported] Sets the type map. This is only supported if the map is empty or null.- Specified by:
setTypeMap
in interfaceConnection
- Throws:
SQLException
-
prepareCall
Creates a new callable statement.- Specified by:
prepareCall
in interfaceConnection
- Parameters:
sql
- the SQL statement- Returns:
- the callable statement
- Throws:
SQLException
- if the connection is closed or the statement is not valid
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException Creates a callable statement with the specified result set type and concurrency.- Specified by:
prepareCall
in interfaceConnection
- Parameters:
sql
- the SQL statementresultSetType
- the result set type (ResultSet.TYPE_*)resultSetConcurrency
- the concurrency (ResultSet.CONCUR_*)- Returns:
- the callable statement
- Throws:
SQLException
- if the connection is closed or the result set type or concurrency are not supported
-
prepareCall
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Creates a callable statement with the specified result set type, concurrency, and holdability.- Specified by:
prepareCall
in interfaceConnection
- Parameters:
sql
- the SQL statementresultSetType
- the result set type (ResultSet.TYPE_*)resultSetConcurrency
- the concurrency (ResultSet.CONCUR_*)resultSetHoldability
- the holdability (ResultSet.HOLD* / CLOSE*)- Returns:
- the callable statement
- Throws:
SQLException
- if the connection is closed or the result set type, concurrency, or holdability are not supported
-
setSavepoint
Creates a new unnamed savepoint.- Specified by:
setSavepoint
in interfaceConnection
- Returns:
- the new savepoint
- Throws:
SQLException
-
setSavepoint
Creates a new named savepoint.- Specified by:
setSavepoint
in interfaceConnection
- Parameters:
name
- the savepoint name- Returns:
- the new savepoint
- Throws:
SQLException
-
rollback
Rolls back to a savepoint.- Specified by:
rollback
in interfaceConnection
- Parameters:
savepoint
- the savepoint- Throws:
SQLException
-
releaseSavepoint
Releases a savepoint.- Specified by:
releaseSavepoint
in interfaceConnection
- Parameters:
savepoint
- the savepoint to release- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Creates a prepared statement with the specified result set type, concurrency, and holdability.- Specified by:
prepareStatement
in interfaceConnection
- Parameters:
sql
- the SQL statementresultSetType
- the result set type (ResultSet.TYPE_*)resultSetConcurrency
- the concurrency (ResultSet.CONCUR_*)resultSetHoldability
- the holdability (ResultSet.HOLD* / CLOSE*)- Returns:
- the prepared statement
- Throws:
SQLException
- if the connection is closed or the result set type, concurrency, or holdability are not supported
-
prepareStatement
Creates a new prepared statement.- Specified by:
prepareStatement
in interfaceConnection
- Parameters:
sql
- the SQL statementautoGeneratedKeys
-Statement.RETURN_GENERATED_KEYS
if generated keys should be available for retrieval,Statement.NO_GENERATED_KEYS
if generated keys should not be available- Returns:
- the prepared statement
- Throws:
SQLException
- if the connection is closed
-
prepareStatement
Creates a new prepared statement.- Specified by:
prepareStatement
in interfaceConnection
- Parameters:
sql
- the SQL statementcolumnIndexes
- an array of column indexes indicating the columns with generated keys that should be returned from the inserted row- Returns:
- the prepared statement
- Throws:
SQLException
- if the connection is closed
-
prepareStatement
Creates a new prepared statement.- Specified by:
prepareStatement
in interfaceConnection
- Parameters:
sql
- the SQL statementcolumnNames
- an array of column names indicating the columns with generated keys that should be returned from the inserted row- Returns:
- the prepared statement
- Throws:
SQLException
- if the connection is closed
-
checkClosed
protected void checkClosed()INTERNAL. Check if this connection is closed.- Throws:
org.h2.message.DbException
- if the connection or session is closed
-
createClob
Create a new empty Clob object.- Specified by:
createClob
in interfaceConnection
- Returns:
- the object
- Throws:
SQLException
-
createBlob
Create a new empty Blob object.- Specified by:
createBlob
in interfaceConnection
- Returns:
- the object
- Throws:
SQLException
-
createNClob
Create a new empty NClob object.- Specified by:
createNClob
in interfaceConnection
- Returns:
- the object
- Throws:
SQLException
-
createSQLXML
Create a new SQLXML object with no data.- Specified by:
createSQLXML
in interfaceConnection
- Returns:
- the object
- Throws:
SQLException
-
createArrayOf
Create a new Array object.- Specified by:
createArrayOf
in interfaceConnection
- Parameters:
typeName
- the type nameelements
- the values- Returns:
- the array
- Throws:
SQLException
-
createStruct
[Not supported] Create a new empty Struct object.- Specified by:
createStruct
in interfaceConnection
- Throws:
SQLException
-
isValid
public boolean isValid(int timeout) Returns true if this connection is still valid.- Specified by:
isValid
in interfaceConnection
- Parameters:
timeout
- the number of seconds to wait for the database to respond (ignored)- Returns:
- true if the connection is valid.
-
setClientInfo
Set a client property. This method always throws a SQLClientInfoException in standard mode. In compatibility mode the following properties are supported:- DB2: The properties: ApplicationName, ClientAccountingInformation, ClientUser and ClientCorrelationToken are supported.
- MySQL: All property names are supported.
- Oracle: All properties in the form <namespace>.<key name> are supported.
- PostgreSQL: The ApplicationName property is supported.
- Specified by:
setClientInfo
in interfaceConnection
- Parameters:
name
- the name of the propertyvalue
- the value- Throws:
SQLClientInfoException
-
setClientInfo
Set the client properties. This replaces all existing properties. This method always throws a SQLClientInfoException in standard mode. In compatibility mode some properties may be supported (see setProperty(String, String) for details).- Specified by:
setClientInfo
in interfaceConnection
- Parameters:
properties
- the properties (ignored)- Throws:
SQLClientInfoException
-
getClientInfo
Get the client properties.- Specified by:
getClientInfo
in interfaceConnection
- Returns:
- the property list
- Throws:
SQLException
-
getClientInfo
Get a client property.- Specified by:
getClientInfo
in interfaceConnection
- Parameters:
name
- the client info name- Returns:
- the property value or null if the property is not found or not supported.
- Throws:
SQLException
-
unwrap
Return an object of this class if possible.- Specified by:
unwrap
in interfaceWrapper
- Parameters:
iface
- the class- Returns:
- this
- Throws:
SQLException
-
isWrapperFor
Checks if unwrap can return an object of this class.- Specified by:
isWrapperFor
in interfaceWrapper
- Parameters:
iface
- the class- Returns:
- whether or not the interface is assignable from this class
- Throws:
SQLException
-
setSchema
Sets the given schema name to access. Current implementation is case sensitive, i.e. requires schema name to be passed in correct case.- Specified by:
setSchema
in interfaceConnection
- Parameters:
schema
- the schema name- Throws:
SQLException
-
getSchema
Retrieves this current schema name for this connection.- Specified by:
getSchema
in interfaceConnection
- Returns:
- current schema name
- Throws:
SQLException
-
abort
[Not supported]- Specified by:
abort
in interfaceConnection
- Parameters:
executor
- the executor used by this method
-
setNetworkTimeout
[Not supported]- Specified by:
setNetworkTimeout
in interfaceConnection
- Parameters:
executor
- the executor used by this methodmilliseconds
- the TCP connection timeout
-
getNetworkTimeout
public int getNetworkTimeout()[Not supported]- Specified by:
getNetworkTimeout
in interfaceConnection
-
toString
INTERNAL -
getMode
Description copied from interface:CastDataProvider
Returns the database mode.- Specified by:
getMode
in interfaceCastDataProvider
- Returns:
- the database mode
-
getStaticSettings
INTERNAL- Returns:
- StaticSettings
-
currentTimestamp
public org.h2.value.ValueTimestampTimeZone currentTimestamp()Description copied from interface:CastDataProvider
Returns the current timestamp with maximum resolution. The value must be the same within a transaction or within execution of a command.- Specified by:
currentTimestamp
in interfaceCastDataProvider
- Returns:
- the current timestamp for CURRENT_TIMESTAMP(9)
-
currentTimeZone
public org.h2.util.TimeZoneProvider currentTimeZone()Description copied from interface:CastDataProvider
Returns the current time zone.- Specified by:
currentTimeZone
in interfaceCastDataProvider
- Returns:
- the current time zone
-
getJavaObjectSerializer
Description copied from interface:CastDataProvider
Returns the custom Java object serializer, ornull
.- Specified by:
getJavaObjectSerializer
in interfaceCastDataProvider
- Returns:
- the custom Java object serializer, or
null
-
zeroBasedEnums
public boolean zeroBasedEnums()Description copied from interface:CastDataProvider
Returns are ENUM values 0-based.- Specified by:
zeroBasedEnums
in interfaceCastDataProvider
- Returns:
- are ENUM values 0-based
-