Package marauroa.server.db
Class DBTransaction
java.lang.Object
marauroa.server.db.DBTransaction
a database transaction
- Author:
- hendrik
-
Constructor Summary
ModifierConstructorDescriptionprotected
DBTransaction(DatabaseAdapter databaseAdapter)
Creates a new DBTransaction. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
close()
closes the database connectionprotected void
commit()
tries to commits this transaction, in case the commit fails, a rollback is executed.boolean
doesColumnExist(String table, String column)
checks whether the specified column existsboolean
doesTableExist(String table)
checks whether the specified table existsint
executes an SQL statement with parameter substitutionint
executes an SQL statement with parameter substitutionint
getColumnLength(String table, String column)
Gets the length of the specified columnint
getLastInsertId(String table, String idcolumn)
gets the id of the last insert.boolean
determine if the Exception indicates a connection error, so a retry of the transaction makes sense.boolean
determines if this Exception was caused by an deadlock, so a retry of the transaction makes senseprepareStatement(String query, Map<String,Object> params)
Prepares a statement for a batch operation.queries the databaseint
querySingleCellInt(String query, Map<String,Object> params)
queries the database and returns the first column in the first row as integer (for example for a count(*)).protected void
rollback()
rollsback this transactionprotected void
sets the thread in which this transaction is supposed to be used.Replaces variables SQL-Statements and prevents SQL injection attacksboolean
verifies that the connection i still working
-
Constructor Details
-
DBTransaction
Creates a new DBTransaction.- Parameters:
databaseAdapter
- database adapter for accessing the database
-
-
Method Details
-
setThread
sets the thread in which this transaction is supposed to be used.- Parameters:
thread
- Thread
-
commit
tries to commits this transaction, in case the commit fails, a rollback is executed.- Throws:
SQLException
- in case of an database error
-
rollback
rollsback this transaction- Throws:
SQLException
- in case of an database error
-
close
protected void close()closes the database connection -
subst
Replaces variables SQL-Statements and prevents SQL injection attacks- Parameters:
sql
- SQL-Stringparams
- replacement parameters- Returns:
- SQL-String with substituted parameters
- Throws:
SQLException
- in case of an sql injection attack
-
execute
executes an SQL statement with parameter substitution- Parameters:
query
- SQL statementparams
- 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, IOExceptionexecutes an SQL statement with parameter substitution- Parameters:
query
- SQL statementparams
- parameter valuesinStream
- input streams to stream into "?" columns- Returns:
- number of affected rows
- Throws:
SQLException
- in case of an database errorIOException
- in case of an input/output error
-
query
queries the database- Parameters:
query
- SQL statementparams
- parameter values- Returns:
- ResultSet
- Throws:
SQLException
- in case of an database error
-
querySingleCellInt
queries the database and returns the first column in the first row as integer (for example for a count(*)).- Parameters:
query
- SQL statementparams
- parameter values- Returns:
- value of the first column in the first row
- Throws:
SQLException
- in case of an database error
-
getLastInsertId
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 doneidcolumn
- 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 SQLExceptionPrepares a statement for a batch operation.- Parameters:
query
- SQL statementparams
- parameter values- Returns:
- PreparedStatement
- Throws:
SQLException
- in case of an database error
-
doesTableExist
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
checks whether the specified column exists- Parameters:
table
- name of tablecolumn
- name of column- Returns:
- true, if the column exists, false otherwise
- Throws:
SQLException
- in case of an database error
-
getColumnLength
Gets the length of the specified column- Parameters:
table
- name of tablecolumn
- 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
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
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
-