Class Session

java.lang.Object
net.sf.jailer.database.Session

public class Session extends Object
Manages database sessions on a 'per thread' basis. Executes SQL-Statements in the context of a session.
Author:
Ralf Wisser
  • Field Details

    • connection

      protected ThreadLocal<Connection> connection
      Hold a connection for each thread.
    • scope

      public final WorkingTableScope scope
      Scope of temporary tables.
    • _log

      public static final org.slf4j.Logger _log
      The logger.
    • connectionFactory

      protected final Session.ConnectionFactory connectionFactory
      Connection factory.
    • dbms

      public final DBMS dbms
      The DBMS.
    • driverClassName

      public final String driverClassName
      The DBMS.
    • dbUrl

      public final String dbUrl
      The dbUrl (null if unknown)
    • down

      protected AtomicBoolean down
    • MD_GETCOLUMNS_LOCK

      public final Object MD_GETCOLUMNS_LOCK
    • lastUpdateTS

      public static volatile long lastUpdateTS
  • Constructor Details

    • Session

      public Session(DataSource dataSource, DBMS dbms, Integer isolationLevel) throws SQLException
      Constructor.
      Parameters:
      dataSource - the data source
      dbms - the DBMS
      isolationLevel - the transaction isolation level, or null to use the default
      Throws:
      SQLException
    • Session

      public Session(DataSource dataSource, DBMS dbms, Integer isolationLevel, WorkingTableScope scope, boolean transactional) throws SQLException
      Constructor.
      Parameters:
      dataSource - the data source
      dbms - the DBMS
      isolationLevel - the transaction isolation level, or null to use the default
      scope - the working-table scope
      transactional - true to operate in transactional mode
      Throws:
      SQLException
    • Session

      public Session(DataSource dataSource, DBMS dbms, Integer isolationLevel, WorkingTableScope scope, boolean transactional, boolean local) throws SQLException
      Constructor.
      Parameters:
      dataSource - the data source
      dbms - the DBMS
      isolationLevel - the transaction isolation level, or null to use the default
      scope - the working-table scope
      transactional - true to operate in transactional mode
      local - true for the local entity-graph database
      Throws:
      SQLException
  • Method Details

    • setConnection

      protected void setConnection(Connection con)
    • markConnectionAsPotentiallyInvalid

      public void markConnectionAsPotentiallyInvalid(Connection con)
      Marks a connection as potentially invalid. Forces to re-validate the connection in getConnection().
      Parameters:
      con - the connection
    • init

      protected void init() throws SQLException
      Throws:
      SQLException
    • reconnect

      public void reconnect()
      Closes current connection and opens a new one.
    • setSilent

      public void setSilent(boolean silent)
      No SQL-Exceptions will be logged in silent mode.
      Parameters:
      silent - true for silence
    • getSilent

      public boolean getSilent()
      No SQL-Exceptions will be logged in silent mode.
      Returns:
      true for silence
    • setLogStatements

      public void setLogStatements(boolean logStatements)
      Sets whether SQL statements should be logged.
      Parameters:
      logStatements - true to enable statement logging
    • getLogStatements

      public boolean getLogStatements()
      Returns whether SQL statements should be logged.
      Returns:
      true if statement logging is enabled
    • getSchema

      public String getSchema()
      Gets DB schema name.
      Returns:
      DB schema name (empty string if unknown)
    • executeQuery

      public long executeQuery(String sqlQuery, Session.ResultSetReader reader, boolean withExplicitCommit) throws SQLException
      Executes a SQL-Query (SELECT).
      Parameters:
      sqlQuery - the query in SQL
      reader - the reader for the result
      withExplicitCommit - if true, switch of autocommit and commit explicitly
      Throws:
      SQLException
    • executeQuery

      public long executeQuery(String sqlQuery, Session.ResultSetReader reader) throws SQLException
      Executes a SQL-Query (SELECT).
      Parameters:
      sqlQuery - the query in SQL
      reader - the reader for the result
      Throws:
      SQLException
    • executeQuery

      public long executeQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit, boolean withExplicitCommit) throws SQLException
      Executes a SQL-Query (SELECT).
      Parameters:
      sqlQuery - the query in SQL
      reader - the reader for the result
      alternativeSQL - query to be executed if sqlQuery fails
      context - cancellation context
      limit - row limit, 0 for unlimited
      withExplicitCommit - if true, switch of autocommit and commit explicitly
      Throws:
      SQLException
    • executeQuery

      public long executeQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit) throws SQLException
      Executes a SQL-Query (SELECT).
      Parameters:
      sqlQuery - the query in SQL
      reader - the reader for the result
      alternativeSQL - query to be executed if sqlQuery fails
      context - cancellation context
      limit - row limit, 0 for unlimited
      Throws:
      SQLException
    • executeQuery

      public long executeQuery(String sqlQuery, Session.ResultSetReader reader, String alternativeSQL, Object context, long limit, int timeout, boolean withExplicitCommit) throws SQLException
      Executes a SQL-Query (SELECT) with timeout.
      Parameters:
      sqlQuery - the query in SQL
      reader - the reader for the result
      alternativeSQL - query to be executed if sqlQuery fails
      context - cancellation context
      limit - row limit, 0 for unlimited
      timeout - the timeout in sec
      withExplicitCommit - if true, switch of autocommit and commit explicitly
      Throws:
      SQLException
    • executeQuery

      public void executeQuery(File sqlFile, Session.ResultSetReader reader, boolean withExplicitCommit) throws SQLException
      Executes a SQL-Query (SELECT).
      Parameters:
      sqlFile - file containing a query in SQL
      reader - the reader for the result
      withExplicitCommit - if true, switch of autocommit and commit explicitly
      Throws:
      SQLException
    • executeUpdate

      public int executeUpdate(String sqlUpdate) throws SQLException
      Executes a SQL-Update (INSERT, DELETE or UPDATE).
      Parameters:
      sqlUpdate - the update in SQL
      Returns:
      update-count
      Throws:
      SQLException
    • executeUpdate

      public int executeUpdate(String sqlUpdate, Object[] parameter) throws SQLException
      Executes a SQL-Update (INSERT, DELETE or UPDATE) with parameters.
      Parameters:
      sqlUpdate - the update in SQL
      parameter - the parameters
      Returns:
      update-count
      Throws:
      SQLException
    • insertClob

      public void insertClob(String table, String column, String where, File lobFile, long length) throws SQLException, IOException
      Inserts a CLob.
      Parameters:
      table - the target table name
      column - the target column name
      where - the WHERE clause identifying the row to update
      lobFile - the file containing the CLOB data
      length - the length of the CLOB data in characters
      Throws:
      SQLException
      IOException
    • insertSQLXML

      public void insertSQLXML(String table, String column, String where, File lobFile, long length) throws SQLException, IOException
      Inserts a SQL-XML.
      Parameters:
      table - the target table name
      column - the target column name
      where - the WHERE clause identifying the row to update
      lobFile - the file containing the XML data
      length - the length of the XML data in characters
      Throws:
      SQLException
      IOException
    • insertBlob

      public void insertBlob(String table, String column, String where, File lobFile) throws SQLException, IOException
      Inserts a BLob.
      Parameters:
      table - the target table name
      column - the target column name
      where - the WHERE clause identifying the row to update
      lobFile - the file containing the BLOB data
      Throws:
      SQLException
      IOException
    • execute

      public long execute(String sql) throws SQLException
      Executes a SQL-Statement.
      Parameters:
      sql - the SQL-Statement
      Returns:
      the update count
      Throws:
      SQLException
    • execute

      public long execute(String sql, Object cancellationContext, boolean acceptQueries) throws SQLException
      Executes a SQL-Statement.
      Parameters:
      sql - the SQL-Statement
      cancellationContext - the context used to check for cancellation, or null
      acceptQueries - if true, SELECT statements are also accepted
      Returns:
      the update count, or 0 for queries
      Throws:
      SQLException
    • disableMetaDataChecking

      public void disableMetaDataChecking()
      Disables re-validation of cached database meta data.
    • getMetaData

      public DatabaseMetaData getMetaData() throws SQLException
      Gets DB meta data.
      Returns:
      DB meta data
      Throws:
      SQLException
    • shutDown

      public void shutDown()
      Closes all connections.
    • isDown

      public boolean isDown()
      Returns whether this session has been shut down.
      Returns:
      true if the session is shut down
    • killRunningStatements

      public void killRunningStatements()
      Cancels all currently running statements.
    • rollbackAll

      public void rollbackAll() throws SQLException
      Rolls back and closes all connections.
      Throws:
      SQLException
    • commitAll

      public void commitAll() throws SQLException
      Commits all connections.
      Throws:
      SQLException
    • getIntrospectionSchema

      public String getIntrospectionSchema()
      Gets optional schema for database analysis.
      Returns:
      optional schema for database analysis
    • setIntrospectionSchema

      public void setIntrospectionSchema(String introspectionSchema)
      Sets optional schema for database analysis.
      Parameters:
      introspectionSchema - optional schema for database analysis
    • getPassword

      public String getPassword()
      Gets connection password (UI support).
      Returns:
      the connection password
    • setPassword

      public void setPassword(String password)
      Sets connection password (UI support).
      Parameters:
      password - the connection password
    • setCliArguments

      public void setCliArguments(List<String> args)
      Sets CLI connection arguments (UI support).
      Parameters:
      args - the CLI connection arguments
    • getCliArguments

      public List<String> getCliArguments()
      Gets CLI connection arguments (UI support).
      Returns:
      the CLI connection arguments
    • getConnection

      public Connection getConnection() throws SQLException
      Gets the connection for the current thread.
      Returns:
      the connection for the current thread
      Throws:
      SQLException
    • createNewConnection

      public Connection createNewConnection() throws SQLException
      Create a new connection. Must be closed from caller.
      Returns:
      a new connection
      Throws:
      SQLException
    • getInlineViewStyle

      public InlineViewStyle getInlineViewStyle()
      Returns a suitable InlineViewStyle for this session.
      Returns:
      a suitable InlineViewStyle for this session or null, if no style is found
    • setSessionProperty

      public void setSessionProperty(Class<?> owner, String name, Object property)
      Sets a session property.
      Parameters:
      owner - the class that owns the property
      name - name of the property
      property - value of the property
    • getSessionProperty

      public Object getSessionProperty(Class<?> owner, String name)
      Gets a session property.
      Parameters:
      owner - the class that owns the property
      name - name of the property
      Returns:
      value of the property
    • removeSessionProperties

      public void removeSessionProperties(Class<?> owner)
      Removes all session properties.
      Parameters:
      owner - the class that owns the properties
    • checkQuery

      public boolean checkQuery(String sql)
      Checks whether a SQL query can be executed without errors.
      Parameters:
      sql - the SQL query to check
      Returns:
      true if the query is executable without errors
    • setThreadSharesConnection

      public static void setThreadSharesConnection()
      Marks the current thread as sharing a connection with other threads.
    • setLogPrefix

      public void setLogPrefix(String logPrefix)
      Sets a prefix string that is prepended to all log messages from this session.
      Parameters:
      logPrefix - the log prefix to set
    • setGlobalFallbackConnection

      public void setGlobalFallbackConnection()
      Promotes the current default connection to the global fallback connection.
    • resetGlobalFallbackConnection

      public static void resetGlobalFallbackConnection()
      Clears the global fallback connection.
    • isConnectionExclusive

      public boolean isConnectionExclusive()
      Returns whether the current thread holds the connection exclusively.
      Returns:
      true if no other thread is using the same connection
    • getConnectionStats

      public String getConnectionStats()
      Returns a string summarising connection pool statistics.
      Returns:
      connection statistics string