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

java.lang.Object
  extended by org.openstreetmap.osmosis.core.mysql.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 executeStatement(java.lang.String sql)
          Executes a sql statement against the database.
 java.sql.ResultSet executeStreamingQuery(java.lang.String sql)
          Creates a result set that is configured to stream results from the database.
protected  void finalize()
          Enforces cleanup of any remaining resources during garbage collection.
 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.
 
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

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.

executeStreamingQuery

public java.sql.ResultSet executeStreamingQuery(java.lang.String sql)
Creates a result set that is configured to stream results from the database.

Parameters:
sql - The query to invoke.
Returns:
The 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.


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.