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, XIDFields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE -
Constructor Summary
ConstructorsConstructorDescriptionJdbcConnection(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 voidINTERNAL.voidClears all warnings.voidclose()Closes this connection.voidcommit()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.ValueTimestampTimeZoneReturns the current timestamp with maximum resolution.org.h2.util.TimeZoneProviderReturns the current time zone.booleanGets the current setting for auto commit.Gets the current catalog name.Get the client properties.getClientInfo(String name) Get a client property.intReturns 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.INTERNALINTERNALintReturns the current transaction isolation level.Gets the type map.Gets the first warning reported by calls on this object.booleanisClosed()Returns true if this connection has been closed.booleanReturns true if the database is read-only.booleanisValid(int timeout) Returns true if this connection is still valid.booleanisWrapperFor(Class<?> iface) Checks if unwrap can return an object of this class.protected final voidlock()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.voidreleaseSavepoint(Savepoint savepoint) Releases a savepoint.voidrollback()Rolls back the current transaction.voidRolls back to a savepoint.voidsetAutoCommit(boolean autoCommit) Switches auto commit on or off.voidsetCatalog(String catalog) Set the default catalog name.voidsetClientInfo(String name, String value) Set a client property.voidsetClientInfo(Properties properties) Set the client properties.voidsetHoldability(int holdability) Changes the current result set holdability.voidsetNetworkTimeout(Executor executor, int milliseconds) [Not supported]voidsetReadOnly(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.voidSets the given schema name to access.voidsetTransactionIsolation(int level) Changes the current transaction isolation level.voidsetTypeMap(Map<String, Class<?>> map) [Partially supported] Sets the type map.toString()INTERNALprotected final voidunlock()Unlocks this connection.<T> TReturn an object of this class if possible.booleanReturns 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, unsupportedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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:
createStatementin 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:
createStatementin 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:
createStatementin 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:
prepareStatementin 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:
getMetaDatain 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceConnection- Throws:
SQLException
-
setAutoCommit
Switches auto commit on or off. Enabling it commits an uncommitted transaction, if there is one.- Specified by:
setAutoCommitin 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:
getAutoCommitin 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:
commitin 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:
rollbackin interfaceConnection- Throws:
SQLException- if the connection is closed
-
isClosed
Returns true if this connection has been closed.- Specified by:
isClosedin interfaceConnection- Returns:
- true if close was called
- Throws:
SQLException
-
nativeSQL
Translates a SQL statement into the database grammar.- Specified by:
nativeSQLin 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:
setReadOnlyin interfaceConnection- Parameters:
readOnly- ignored- Throws:
SQLException- if the connection is closed
-
isReadOnly
Returns true if the database is read-only.- Specified by:
isReadOnlyin 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:
setCatalogin interfaceConnection- Parameters:
catalog- ignored- Throws:
SQLException- if the connection is closed
-
getCatalog
Gets the current catalog name.- Specified by:
getCatalogin 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:
getWarningsin interfaceConnection- Returns:
- null
- Throws:
SQLException
-
clearWarnings
Clears all warnings.- Specified by:
clearWarningsin 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:
prepareStatementin 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:
setTransactionIsolationin 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:
getTransactionIsolationin interfaceConnection- Returns:
- the isolation level
- Throws:
SQLException- if the connection is closed
-
setHoldability
Changes the current result set holdability.- Specified by:
setHoldabilityin 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:
getHoldabilityin interfaceConnection- Returns:
- the holdability
- Throws:
SQLException- if the connection is closed
-
getTypeMap
Gets the type map.- Specified by:
getTypeMapin 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:
setTypeMapin interfaceConnection- Throws:
SQLException
-
prepareCall
Creates a new callable statement.- Specified by:
prepareCallin 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:
prepareCallin 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:
prepareCallin 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:
setSavepointin interfaceConnection- Returns:
- the new savepoint
- Throws:
SQLException
-
setSavepoint
Creates a new named savepoint.- Specified by:
setSavepointin interfaceConnection- Parameters:
name- the savepoint name- Returns:
- the new savepoint
- Throws:
SQLException
-
rollback
Rolls back to a savepoint.- Specified by:
rollbackin interfaceConnection- Parameters:
savepoint- the savepoint- Throws:
SQLException
-
releaseSavepoint
Releases a savepoint.- Specified by:
releaseSavepointin 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:
prepareStatementin 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:
prepareStatementin interfaceConnection- Parameters:
sql- the SQL statementautoGeneratedKeys-Statement.RETURN_GENERATED_KEYSif generated keys should be available for retrieval,Statement.NO_GENERATED_KEYSif 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:
prepareStatementin 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:
prepareStatementin 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:
createClobin interfaceConnection- Returns:
- the object
- Throws:
SQLException
-
createBlob
Create a new empty Blob object.- Specified by:
createBlobin interfaceConnection- Returns:
- the object
- Throws:
SQLException
-
createNClob
Create a new empty NClob object.- Specified by:
createNClobin interfaceConnection- Returns:
- the object
- Throws:
SQLException
-
createSQLXML
Create a new SQLXML object with no data.- Specified by:
createSQLXMLin interfaceConnection- Returns:
- the object
- Throws:
SQLException
-
createArrayOf
Create a new Array object.- Specified by:
createArrayOfin interfaceConnection- Parameters:
typeName- the type nameelements- the values- Returns:
- the array
- Throws:
SQLException
-
createStruct
[Not supported] Create a new empty Struct object.- Specified by:
createStructin interfaceConnection- Throws:
SQLException
-
isValid
public boolean isValid(int timeout) Returns true if this connection is still valid.- Specified by:
isValidin 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:
setClientInfoin 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:
setClientInfoin interfaceConnection- Parameters:
properties- the properties (ignored)- Throws:
SQLClientInfoException
-
getClientInfo
Get the client properties.- Specified by:
getClientInfoin interfaceConnection- Returns:
- the property list
- Throws:
SQLException
-
getClientInfo
Get a client property.- Specified by:
getClientInfoin 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:
unwrapin interfaceWrapper- Parameters:
iface- the class- Returns:
- this
- Throws:
SQLException
-
isWrapperFor
Checks if unwrap can return an object of this class.- Specified by:
isWrapperForin 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:
setSchemain interfaceConnection- Parameters:
schema- the schema name- Throws:
SQLException
-
getSchema
Retrieves this current schema name for this connection.- Specified by:
getSchemain interfaceConnection- Returns:
- current schema name
- Throws:
SQLException
-
abort
[Not supported]- Specified by:
abortin interfaceConnection- Parameters:
executor- the executor used by this method
-
setNetworkTimeout
[Not supported]- Specified by:
setNetworkTimeoutin interfaceConnection- Parameters:
executor- the executor used by this methodmilliseconds- the TCP connection timeout
-
getNetworkTimeout
public int getNetworkTimeout()[Not supported]- Specified by:
getNetworkTimeoutin interfaceConnection
-
toString
INTERNAL -
getMode
Description copied from interface:CastDataProviderReturns the database mode.- Specified by:
getModein interfaceCastDataProvider- Returns:
- the database mode
-
getStaticSettings
INTERNAL- Returns:
- StaticSettings
-
currentTimestamp
public org.h2.value.ValueTimestampTimeZone currentTimestamp()Description copied from interface:CastDataProviderReturns the current timestamp with maximum resolution. The value must be the same within a transaction or within execution of a command.- Specified by:
currentTimestampin interfaceCastDataProvider- Returns:
- the current timestamp for CURRENT_TIMESTAMP(9)
-
currentTimeZone
public org.h2.util.TimeZoneProvider currentTimeZone()Description copied from interface:CastDataProviderReturns the current time zone.- Specified by:
currentTimeZonein interfaceCastDataProvider- Returns:
- the current time zone
-
getJavaObjectSerializer
Description copied from interface:CastDataProviderReturns the custom Java object serializer, ornull.- Specified by:
getJavaObjectSerializerin interfaceCastDataProvider- Returns:
- the custom Java object serializer, or
null
-
zeroBasedEnums
public boolean zeroBasedEnums()Description copied from interface:CastDataProviderReturns are ENUM values 0-based.- Specified by:
zeroBasedEnumsin interfaceCastDataProvider- Returns:
- are ENUM values 0-based
-