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

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

public class DatabaseContext2
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
DatabaseContext2(DatabaseLoginCredentials loginCredentials)
          Creates a new instance.
 
Method Summary
 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.lang.Object executeWithinTransaction(org.springframework.transaction.support.TransactionCallback txnCallback)
          Invokes the provided callback code within a transaction.
protected  void finalize()
          Enforces cleanup of any remaining resources during garbage collection.
 DatabaseType getDatabaseType()
          Returns the database type currently in use.
 org.springframework.jdbc.core.JdbcTemplate getJdbcTemplate()
          Gets the jdbc template which provides access to database functions.
 long getLastInsertId()
          Gets the last inserted identity column value.
 long getLastSequenceId(java.lang.String sequenceName)
          Gets the last retrieved sequence value.
 org.springframework.jdbc.core.simple.SimpleJdbcTemplate getSimpleJdbcTemplate()
          Gets the jdbc template which provides simple access to database functions.
 void lockTables(java.util.List<java.lang.String> tables)
          Locks the specified tables for exclusive access.
 void release()
          Releases all database resources.
 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

DatabaseContext2

public DatabaseContext2(DatabaseLoginCredentials loginCredentials)
Creates a new instance.

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

getSimpleJdbcTemplate

public org.springframework.jdbc.core.simple.SimpleJdbcTemplate getSimpleJdbcTemplate()
Gets the jdbc template which provides simple access to database functions.

Returns:
The jdbc template.

getJdbcTemplate

public org.springframework.jdbc.core.JdbcTemplate getJdbcTemplate()
Gets the jdbc template which provides access to database functions.

Returns:
The jdbc template.

executeWithinTransaction

public java.lang.Object executeWithinTransaction(org.springframework.transaction.support.TransactionCallback txnCallback)
Invokes the provided callback code within a transaction.

Parameters:
txnCallback - The logic to be invoked within a transaction.
Returns:
The result.

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.

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.


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.