Package org.h2.jdbc

Class JdbcConnection

java.lang.Object
org.h2.message.TraceObject
org.h2.jdbc.JdbcConnection
All Implemented Interfaces:
AutoCloseable, Connection, Wrapper, CastDataProvider, JdbcConnectionBackwardsCompat
Direct Known Subclasses:
JdbcXAConnection.PooledJdbcConnection

public class JdbcConnection extends TraceObject implements Connection, JdbcConnectionBackwardsCompat, CastDataProvider
Represents a connection (session) to a database.

Thread safety: the connection is thread-safe, because access is synchronized. 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 Details

  • 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 connection
      info - of this connection
      user - of this connection
      password - for the user
      forbidCreation - whether database creation is forbidden
      Throws:
      SQLException - on failure
    • JdbcConnection

      public JdbcConnection(JdbcConnection clone)
      INTERNAL
      Parameters:
      clone - connection to clone
    • JdbcConnection

      public JdbcConnection(Session session, String user, String url)
      INTERNAL
      Parameters:
      session - of this connection
      user - of this connection
      url - of this connection
  • Method Details

    • closeOld

      private void closeOld()
    • createStatement

      public Statement createStatement() throws SQLException
      Creates a new statement.
      Specified by:
      createStatement in interface Connection
      Returns:
      the new statement
      Throws:
      SQLException - if the connection is closed
    • createStatement

      public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
      Creates a statement with the specified result set type and concurrency.
      Specified by:
      createStatement in interface Connection
      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 interface Connection
      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

      public PreparedStatement prepareStatement(String sql) throws SQLException
      Creates a new prepared statement.
      Specified by:
      prepareStatement in interface Connection
      Parameters:
      sql - the SQL statement
      Returns:
      the prepared statement
      Throws:
      SQLException - if the connection is closed
    • getMetaData

      public DatabaseMetaData getMetaData() throws SQLException
      Gets the database meta data for this database.
      Specified by:
      getMetaData in interface Connection
      Returns:
      the database meta data
      Throws:
      SQLException - if the connection is closed
    • getSession

      public Session getSession()
      INTERNAL
      Returns:
      session
    • close

      public void close() throws SQLException
      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 interface AutoCloseable
      Specified by:
      close in interface Connection
      Throws:
      SQLException
    • closePreparedCommands

      private void closePreparedCommands()
    • closeAndSetNull

      private static CommandInterface closeAndSetNull(CommandInterface command)
    • setAutoCommit

      public void setAutoCommit(boolean autoCommit) throws SQLException
      Switches auto commit on or off. Enabling it commits an uncommitted transaction, if there is one.
      Specified by:
      setAutoCommit in interface Connection
      Parameters:
      autoCommit - true for auto commit on, false for off
      Throws:
      SQLException - if the connection is closed
    • getAutoCommit

      public boolean getAutoCommit() throws SQLException
      Gets the current setting for auto commit.
      Specified by:
      getAutoCommit in interface Connection
      Returns:
      true for on, false for off
      Throws:
      SQLException - if the connection is closed
    • commit

      public void commit() throws SQLException
      Commits the current transaction. This call has only an effect if auto commit is switched off.
      Specified by:
      commit in interface Connection
      Throws:
      SQLException - if the connection is closed
    • rollback

      public void rollback() throws SQLException
      Rolls back the current transaction. This call has only an effect if auto commit is switched off.
      Specified by:
      rollback in interface Connection
      Throws:
      SQLException - if the connection is closed
    • isClosed

      public boolean isClosed() throws SQLException
      Returns true if this connection has been closed.
      Specified by:
      isClosed in interface Connection
      Returns:
      true if close was called
      Throws:
      SQLException
    • nativeSQL

      public String nativeSQL(String sql) throws SQLException
      Translates a SQL statement into the database grammar.
      Specified by:
      nativeSQL in interface Connection
      Parameters:
      sql - the SQL statement with or without JDBC escape sequences
      Returns:
      the translated statement
      Throws:
      SQLException - if the connection is closed
    • setReadOnly

      public void setReadOnly(boolean readOnly) throws SQLException
      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 interface Connection
      Parameters:
      readOnly - ignored
      Throws:
      SQLException - if the connection is closed
    • isReadOnly

      public boolean isReadOnly() throws SQLException
      Returns true if the database is read-only.
      Specified by:
      isReadOnly in interface Connection
      Returns:
      if the database is read-only
      Throws:
      SQLException - if the connection is closed
    • setCatalog

      public void setCatalog(String catalog) throws SQLException
      Set the default catalog name. This call is ignored.
      Specified by:
      setCatalog in interface Connection
      Parameters:
      catalog - ignored
      Throws:
      SQLException - if the connection is closed
    • getCatalog

      public String getCatalog() throws SQLException
      Gets the current catalog name.
      Specified by:
      getCatalog in interface Connection
      Returns:
      the catalog name
      Throws:
      SQLException - if the connection is closed
    • getWarnings

      public SQLWarning getWarnings() throws SQLException
      Gets the first warning reported by calls on this object.
      Specified by:
      getWarnings in interface Connection
      Returns:
      null
      Throws:
      SQLException
    • clearWarnings

      public void clearWarnings() throws SQLException
      Clears all warnings.
      Specified by:
      clearWarnings in interface Connection
      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 interface Connection
      Parameters:
      sql - the SQL statement
      resultSetType - 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

      public void setTransactionIsolation(int level) throws SQLException
      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 interface Connection
      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
    • setQueryTimeout

      void setQueryTimeout(int seconds) throws SQLException
      INTERNAL
      Throws:
      SQLException
    • getQueryTimeout

      int getQueryTimeout() throws SQLException
      INTERNAL
      Throws:
      SQLException
    • getTransactionIsolation

      public int getTransactionIsolation() throws SQLException
      Returns the current transaction isolation level.
      Specified by:
      getTransactionIsolation in interface Connection
      Returns:
      the isolation level
      Throws:
      SQLException - if the connection is closed
    • setHoldability

      public void setHoldability(int holdability) throws SQLException
      Changes the current result set holdability.
      Specified by:
      setHoldability in interface Connection
      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

      public int getHoldability() throws SQLException
      Returns the current result set holdability.
      Specified by:
      getHoldability in interface Connection
      Returns:
      the holdability
      Throws:
      SQLException - if the connection is closed
    • getTypeMap

      public Map<String,Class<?>> getTypeMap() throws SQLException
      Gets the type map.
      Specified by:
      getTypeMap in interface Connection
      Returns:
      null
      Throws:
      SQLException - if the connection is closed
    • setTypeMap

      public void setTypeMap(Map<String,Class<?>> map) throws SQLException
      [Partially supported] Sets the type map. This is only supported if the map is empty or null.
      Specified by:
      setTypeMap in interface Connection
      Throws:
      SQLException
    • prepareCall

      public CallableStatement prepareCall(String sql) throws SQLException
      Creates a new callable statement.
      Specified by:
      prepareCall in interface Connection
      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 interface Connection
      Parameters:
      sql - the SQL statement
      resultSetType - 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 interface Connection
      Parameters:
      sql - the SQL statement
      resultSetType - 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

      public Savepoint setSavepoint() throws SQLException
      Creates a new unnamed savepoint.
      Specified by:
      setSavepoint in interface Connection
      Returns:
      the new savepoint
      Throws:
      SQLException
    • setSavepoint

      public Savepoint setSavepoint(String name) throws SQLException
      Creates a new named savepoint.
      Specified by:
      setSavepoint in interface Connection
      Parameters:
      name - the savepoint name
      Returns:
      the new savepoint
      Throws:
      SQLException
    • rollback

      public void rollback(Savepoint savepoint) throws SQLException
      Rolls back to a savepoint.
      Specified by:
      rollback in interface Connection
      Parameters:
      savepoint - the savepoint
      Throws:
      SQLException
    • releaseSavepoint

      public void releaseSavepoint(Savepoint savepoint) throws SQLException
      Releases a savepoint.
      Specified by:
      releaseSavepoint in interface Connection
      Parameters:
      savepoint - the savepoint to release
      Throws:
      SQLException
    • convertSavepoint

      private static JdbcSavepoint convertSavepoint(Savepoint savepoint)
    • 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 interface Connection
      Parameters:
      sql - the SQL statement
      resultSetType - 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

      public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
      Creates a new prepared statement.
      Specified by:
      prepareStatement in interface Connection
      Parameters:
      sql - the SQL statement
      autoGeneratedKeys - 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

      public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
      Creates a new prepared statement.
      Specified by:
      prepareStatement in interface Connection
      Parameters:
      sql - the SQL statement
      columnIndexes - 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

      public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
      Creates a new prepared statement.
      Specified by:
      prepareStatement in interface Connection
      Parameters:
      sql - the SQL statement
      columnNames - 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
    • prepareCommand

      CommandInterface prepareCommand(String sql, int fetchSize)
      Prepare an command. This will parse the SQL statement.
      Parameters:
      sql - the SQL statement
      fetchSize - the fetch size (used in remote connections)
      Returns:
      the command
    • prepareCommand

      private CommandInterface prepareCommand(String sql, CommandInterface old)
    • translateGetEnd

      private static int translateGetEnd(String sql, int i, char c)
    • translateSQL

      private static String translateSQL(String sql)
      Convert JDBC escape sequences in the SQL statement. This method throws an exception if the SQL statement is null.
      Parameters:
      sql - the SQL statement with or without JDBC escape sequences
      Returns:
      the SQL statement without JDBC escape sequences
    • translateSQL

      static String translateSQL(String sql, boolean escapeProcessing)
      Convert JDBC escape sequences in the SQL statement if required. This method throws an exception if the SQL statement is null.
      Parameters:
      sql - the SQL statement with or without JDBC escape sequences
      escapeProcessing - whether escape sequences should be replaced
      Returns:
      the SQL statement without JDBC escape sequences
    • translateSQLImpl

      private static String translateSQLImpl(String sql)
    • checkRunOver

      private static void checkRunOver(int i, int len, String sql)
    • found

      private static boolean found(String sql, int start, String other)
    • checkTypeConcurrency

      private static void checkTypeConcurrency(int resultSetType, int resultSetConcurrency)
    • checkHoldability

      private static void checkHoldability(int resultSetHoldability)
    • checkClosed

      protected void checkClosed()
      INTERNAL. Check if this connection is closed.
      Throws:
      DbException - if the connection or session is closed
    • getURL

      String getURL()
    • getUser

      String getUser()
    • rollbackInternal

      private void rollbackInternal()
    • setExecutingStatement

      void setExecutingStatement(Statement stat)
      INTERNAL
    • createClob

      public Clob createClob() throws SQLException
      Create a new empty Clob object.
      Specified by:
      createClob in interface Connection
      Returns:
      the object
      Throws:
      SQLException
    • createBlob

      public Blob createBlob() throws SQLException
      Create a new empty Blob object.
      Specified by:
      createBlob in interface Connection
      Returns:
      the object
      Throws:
      SQLException
    • createNClob

      public NClob createNClob() throws SQLException
      Create a new empty NClob object.
      Specified by:
      createNClob in interface Connection
      Returns:
      the object
      Throws:
      SQLException
    • createSQLXML

      public SQLXML createSQLXML() throws SQLException
      Create a new SQLXML object with no data.
      Specified by:
      createSQLXML in interface Connection
      Returns:
      the object
      Throws:
      SQLException
    • createArrayOf

      public Array createArrayOf(String typeName, Object[] elements) throws SQLException
      Create a new Array object.
      Specified by:
      createArrayOf in interface Connection
      Parameters:
      typeName - the type name
      elements - the values
      Returns:
      the array
      Throws:
      SQLException
    • createStruct

      public Struct createStruct(String typeName, Object[] attributes) throws SQLException
      [Not supported] Create a new empty Struct object.
      Specified by:
      createStruct in interface Connection
      Throws:
      SQLException
    • isValid

      public boolean isValid(int timeout)
      Returns true if this connection is still valid.
      Specified by:
      isValid in interface Connection
      Parameters:
      timeout - the number of seconds to wait for the database to respond (ignored)
      Returns:
      true if the connection is valid.
    • setClientInfo

      public void setClientInfo(String name, String value) throws SQLClientInfoException
      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.
      For unsupported properties a SQLClientInfoException is thrown.
      Specified by:
      setClientInfo in interface Connection
      Parameters:
      name - the name of the property
      value - the value
      Throws:
      SQLClientInfoException
    • isInternalProperty

      private static boolean isInternalProperty(String name)
    • convertToClientInfoException

      private static SQLClientInfoException convertToClientInfoException(SQLException x)
    • setClientInfo

      public void setClientInfo(Properties properties) throws SQLClientInfoException
      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 interface Connection
      Parameters:
      properties - the properties (ignored)
      Throws:
      SQLClientInfoException
    • getClientInfo

      public Properties getClientInfo() throws SQLException
      Get the client properties.
      Specified by:
      getClientInfo in interface Connection
      Returns:
      the property list
      Throws:
      SQLException
    • getClientInfo

      public String getClientInfo(String name) throws SQLException
      Get a client property.
      Specified by:
      getClientInfo in interface Connection
      Parameters:
      name - the client info name
      Returns:
      the property value or null if the property is not found or not supported.
      Throws:
      SQLException
    • unwrap

      public <T> T unwrap(Class<T> iface) throws SQLException
      Return an object of this class if possible.
      Specified by:
      unwrap in interface Wrapper
      Parameters:
      iface - the class
      Returns:
      this
      Throws:
      SQLException
    • isWrapperFor

      public boolean isWrapperFor(Class<?> iface) throws SQLException
      Checks if unwrap can return an object of this class.
      Specified by:
      isWrapperFor in interface Wrapper
      Parameters:
      iface - the class
      Returns:
      whether or not the interface is assignable from this class
      Throws:
      SQLException
    • createClob

      Value createClob(Reader x, long length)
      Create a Clob value from this reader.
      Parameters:
      x - the reader
      length - the length (if smaller or equal than 0, all data until the end of file is read)
      Returns:
      the value
    • createBlob

      Value createBlob(InputStream x, long length)
      Create a Blob value from this input stream.
      Parameters:
      x - the input stream
      length - the length (if smaller or equal than 0, all data until the end of file is read)
      Returns:
      the value
    • setSchema

      public void setSchema(String schema) throws SQLException
      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 interface Connection
      Parameters:
      schema - the schema name
      Throws:
      SQLException
    • getSchema

      public String getSchema() throws SQLException
      Retrieves this current schema name for this connection.
      Specified by:
      getSchema in interface Connection
      Returns:
      current schema name
      Throws:
      SQLException
    • abort

      public void abort(Executor executor)
      [Not supported]
      Specified by:
      abort in interface Connection
      Parameters:
      executor - the executor used by this method
    • setNetworkTimeout

      public void setNetworkTimeout(Executor executor, int milliseconds)
      [Not supported]
      Specified by:
      setNetworkTimeout in interface Connection
      Parameters:
      executor - the executor used by this method
      milliseconds - the TCP connection timeout
    • getNetworkTimeout

      public int getNetworkTimeout()
      [Not supported]
      Specified by:
      getNetworkTimeout in interface Connection
    • checkMap

      static void checkMap(Map<String,Class<?>> map)
      Check that the given type map is either null or empty.
      Parameters:
      map - the type map
      Throws:
      DbException - if the map is not empty
    • toString

      public String toString()
      INTERNAL
      Overrides:
      toString in class Object
    • getCompareMode

      CompareMode getCompareMode()
    • getMode

      public Mode getMode()
      Description copied from interface: CastDataProvider
      Returns the database mode.
      Specified by:
      getMode in interface CastDataProvider
      Returns:
      the database mode
    • getStaticSettings

      public Session.StaticSettings getStaticSettings()
      INTERNAL
      Returns:
      StaticSettings
    • currentTimestamp

      public 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 interface CastDataProvider
      Returns:
      the current timestamp for CURRENT_TIMESTAMP(9)
    • currentTimeZone

      public TimeZoneProvider currentTimeZone()
      Description copied from interface: CastDataProvider
      Returns the current time zone.
      Specified by:
      currentTimeZone in interface CastDataProvider
      Returns:
      the current time zone
    • getJavaObjectSerializer

      public JavaObjectSerializer getJavaObjectSerializer()
      Description copied from interface: CastDataProvider
      Returns the custom Java object serializer, or null.
      Specified by:
      getJavaObjectSerializer in interface CastDataProvider
      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 interface CastDataProvider
      Returns:
      are ENUM values 0-based