org.openstreetmap.osmosis.core.apidb.common
Class DatabaseContext

java.lang.Object
  extended by org.openstreetmap.osmosis.core.apidb.common.DatabaseContext

public class DatabaseContext
extends java.lang.Object

This class manages the lifecycle of JDBC objects to minimise the risk of connection leaks and to support a consistent approach to database access.

Author:
Brett Henderson

Constructor Summary
DatabaseContext(DatabaseLoginCredentials loginCredentials)
          Creates a new instance.
 
Method Summary
 void commit()
          Commits any outstanding transaction.
 void disableIndexes(java.util.List<java.lang.String> tables)
          Disables the indexes of the specified tables.
 boolean doesColumnExist(java.lang.String tableName, java.lang.String columnName)
          Indicates if the specified column exists in the database.
 boolean doesTableExist(java.lang.String tableName)
          Indicates if the specified table exists in the database.
 void enableIndexes(java.util.List<java.lang.String> tables)
          Enables the indexes of the specified tables.
 java.sql.ResultSet executeQuery(java.lang.String sql)
          Executes a query and returns a result set.
 void executeStatement(java.lang.String sql)
          Executes a sql statement against the database.
protected  void finalize()
          Enforces cleanup of any remaining resources during garbage collection.
 DatabaseType getDatabaseType()
          Returns the database type currently in use.
 long getLastInsertId()
          Gets the last inserted identity column value.
 long getLastSequenceId(java.lang.String sequenceName)
          Gets the last retrieved sequence value.
 void lockTables(java.util.List<java.lang.String> tables)
          Locks the specified tables for exclusive access.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
          Creates a new database prepared statement.
 java.sql.PreparedStatement prepareStatementForStreaming(java.lang.String sql)
          Creates a new database statement that is configured so that any result sets created using it will stream data from the database instead of returning all records at once and storing in memory.
 void release()
          Releases all database resources.
 void setAutoCommit(boolean autoCommit)
          Sets the auto-commit property on the underlying connection.
 void truncateTables(java.util.List<java.lang.String> tables)
          Truncates the contents of the specified tables.
 void unlockTables(java.util.List<java.lang.String> tables)
          Unlocks the specified tables.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseContext

public DatabaseContext(DatabaseLoginCredentials loginCredentials)
Creates a new instance.

Parameters:
loginCredentials - Contains all information required to connect to the database.
Method Detail

getDatabaseType

public DatabaseType getDatabaseType()
Returns the database type currently in use. This should only be used when it is not possible to write database agnostic statements.

Returns:
The database type.

truncateTables

public void truncateTables(java.util.List<java.lang.String> tables)
Truncates the contents of the specified tables.

Parameters:
tables - The tables to be truncated.

disableIndexes

public void disableIndexes(java.util.List<java.lang.String> tables)
Disables the indexes of the specified tables.

Parameters:
tables - The tables to disable indexes on.

enableIndexes

public void enableIndexes(java.util.List<java.lang.String> tables)
Enables the indexes of the specified tables.

Parameters:
tables - The tables to enable indexes on.

lockTables

public void lockTables(java.util.List<java.lang.String> tables)
Locks the specified tables for exclusive access.

Parameters:
tables - The tables to lock.

unlockTables

public void unlockTables(java.util.List<java.lang.String> tables)
Unlocks the specified tables.

Parameters:
tables - The tables to unlock.

getLastInsertId

public long getLastInsertId()
Gets the last inserted identity column value. This is a global value and may not work correctly if the database uses triggers.

Returns:
The last inserted identity column value.

getLastSequenceId

public long getLastSequenceId(java.lang.String sequenceName)
Gets the last retrieved sequence value. This is specific to the current connection only.

Parameters:
sequenceName - The name of the sequence.
Returns:
The last inserted identity column value.

executeStatement

public void executeStatement(java.lang.String sql)
Executes a sql statement against the database.

Parameters:
sql - The sql statement to be invoked.

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
Creates a new database prepared statement.

Parameters:
sql - The statement to be created.
Returns:
The newly created statement.

prepareStatementForStreaming

public java.sql.PreparedStatement prepareStatementForStreaming(java.lang.String sql)
Creates a new database statement that is configured so that any result sets created using it will stream data from the database instead of returning all records at once and storing in memory.

If no input parameters need to be set on the statement, use the executeStreamingQuery method instead.

Parameters:
sql - The statement to be created. This must be a select statement.
Returns:
The newly created statement.

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql)
Executes a query and returns a result set. The returned result set must be closed by the caller.

Parameters:
sql - The query to execute.
Returns:
The newly created result set.

commit

public void commit()
Commits any outstanding transaction.


release

public void release()
Releases all database resources. This method is guaranteed not to throw transactions and should always be called in a finally block whenever this class is used.


setAutoCommit

public void setAutoCommit(boolean autoCommit)
Sets the auto-commit property on the underlying connection.

Parameters:
autoCommit - The new auto commit value.

doesColumnExist

public boolean doesColumnExist(java.lang.String tableName,
                               java.lang.String columnName)
Indicates if the specified column exists in the database.

Parameters:
tableName - The table to check for.
columnName - The column to check for.
Returns:
True if the column exists, false otherwise.

doesTableExist

public boolean doesTableExist(java.lang.String tableName)
Indicates if the specified table exists in the database.

Parameters:
tableName - The table to check for.
Returns:
True if the table exists, false otherwise.

finalize

protected void finalize()
                 throws java.lang.Throwable
Enforces cleanup of any remaining resources during garbage collection. This is a safeguard and should not be required if release is called appropriately.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - If a problem occurs during finalization.