Class DBTransaction

java.lang.Object
marauroa.server.db.DBTransaction

public class DBTransaction extends Object
a database transaction
Author:
hendrik
  • Constructor Details

    • DBTransaction

      protected DBTransaction(DatabaseAdapter databaseAdapter)
      Creates a new DBTransaction.
      Parameters:
      databaseAdapter - database adapter for accessing the database
  • Method Details

    • setThread

      protected void setThread(Thread thread)
      sets the thread in which this transaction is supposed to be used.
      Parameters:
      thread - Thread
    • commit

      protected void commit() throws SQLException
      tries to commits this transaction, in case the commit fails, a rollback is executed.
      Throws:
      SQLException - in case of an database error
    • rollback

      protected void rollback() throws SQLException
      rollsback this transaction
      Throws:
      SQLException - in case of an database error
    • close

      protected void close()
      closes the database connection
    • subst

      public String subst(String sql, Map<String,​?> params) throws SQLException
      Replaces variables SQL-Statements and prevents SQL injection attacks
      Parameters:
      sql - SQL-String
      params - replacement parameters
      Returns:
      SQL-String with substituted parameters
      Throws:
      SQLException - in case of an sql injection attack
    • execute

      public int execute(String query, Map<String,​Object> params) throws SQLException
      executes an SQL statement with parameter substitution
      Parameters:
      query - SQL statement
      params - parameter values
      Returns:
      number of affected rows
      Throws:
      SQLException - in case of an database error
    • execute

      public int execute(String query, Map<String,​Object> params, InputStream... inStream) throws SQLException, IOException
      executes an SQL statement with parameter substitution
      Parameters:
      query - SQL statement
      params - parameter values
      inStream - input streams to stream into "?" columns
      Returns:
      number of affected rows
      Throws:
      SQLException - in case of an database error
      IOException - in case of an input/output error
    • query

      public ResultSet query(String query, Map<String,​Object> params) throws SQLException
      queries the database
      Parameters:
      query - SQL statement
      params - parameter values
      Returns:
      ResultSet
      Throws:
      SQLException - in case of an database error
    • querySingleCellInt

      public int querySingleCellInt(String query, Map<String,​Object> params) throws SQLException
      queries the database and returns the first column in the first row as integer (for example for a count(*)).
      Parameters:
      query - SQL statement
      params - parameter values
      Returns:
      value of the first column in the first row
      Throws:
      SQLException - in case of an database error
    • getLastInsertId

      public int getLastInsertId(String table, String idcolumn) throws SQLException
      gets the id of the last insert. Note: The table and idcolumn parameters must match the last insert statement. This is because on some database systems a SELECT IDENTITY is performed and on other database systems a SELECT curval(table_idcolumn_seq).
      Parameters:
      table - name of table on which the last insert was done
      idcolumn - name autoincrement serial column of that table
      Returns:
      generated id
      Throws:
      SQLException - in case of an database error
    • prepareStatement

      public PreparedStatement prepareStatement(String query, Map<String,​Object> params) throws SQLException
      Prepares a statement for a batch operation.
      Parameters:
      query - SQL statement
      params - parameter values
      Returns:
      PreparedStatement
      Throws:
      SQLException - in case of an database error
    • doesTableExist

      public boolean doesTableExist(String table) throws SQLException
      checks whether the specified table exists
      Parameters:
      table - name of table
      Returns:
      true, if the table exists, false otherwise
      Throws:
      SQLException - in case of an database error
    • doesColumnExist

      public boolean doesColumnExist(String table, String column) throws SQLException
      checks whether the specified column exists
      Parameters:
      table - name of table
      column - name of column
      Returns:
      true, if the column exists, false otherwise
      Throws:
      SQLException - in case of an database error
    • getColumnLength

      public int getColumnLength(String table, String column) throws SQLException
      Gets the length of the specified column
      Parameters:
      table - name of table
      column - name of column
      Returns:
      the length of the column
      Throws:
      SQLException - in case of a database error
    • verifyConnection

      public boolean verifyConnection()
      verifies that the connection i still working
      Returns:
      true, if the connection is valid; false otherwise
    • isConnectionError

      public boolean isConnectionError(Exception e)
      determine if the Exception indicates a connection error, so a retry of the transaction makes sense.
      Parameters:
      e - Exception
      Returns:
      true, if the exception indicates a connection error; false otherwise
    • isDeadlockError

      public boolean isDeadlockError(SQLException e)
      determines if this Exception was caused by an deadlock, so a retry of the transaction makes sense
      Parameters:
      e - Exception
      Returns:
      true, if the exception indicates a deadlock; false otherwise