com.googlecode.jdbw.impl
Class DatabaseConnectionImpl

java.lang.Object
  extended by com.googlecode.jdbw.impl.DatabaseConnectionImpl
All Implemented Interfaces:
DatabaseConnection

public class DatabaseConnectionImpl
extends Object
implements DatabaseConnection

This is a default implementation of the DatabaseConnection interface, you can construct objects of this type with a Connection or a DataSource.

Author:
Martin Berglund

Constructor Summary
DatabaseConnectionImpl(Connection connection)
          Creates a DatabaseConnectionImpl backed by one physical database connection.
DatabaseConnectionImpl(Connection connection, DatabaseServerType databaseServerType)
          Creates a DatabaseConnectionImpl backed by one physical database connection.
DatabaseConnectionImpl(DataSource dataSource)
          Creates a DatabaseConnectionImpl backed by a DataSource.
DatabaseConnectionImpl(DataSource dataSource, DatabaseServerType databaseServerType)
          Creates a DatabaseConnectionImpl backed by a DataSource.
DatabaseConnectionImpl(DataSource dataSource, DataSourceCloser dataSourceCloser)
          Creates a DatabaseConnectionImpl backed by a DataSource.
DatabaseConnectionImpl(DataSource dataSource, DataSourceCloser dataSourceCloser, DatabaseServerType databaseServerType)
          Creates a DatabaseConnectionImpl backed by a DataSource.
 
Method Summary
 DatabaseTransaction beginTransaction(TransactionIsolation transactionIsolation)
          Allocated a connection and begins a transaction
 void close()
          Closes this connection and any underlying data source
 AutoExecutor createAutoExecutor()
          Creates an AutoExecutor using this database connection for supplying connections
protected  MetaDataResolver createMetaDataResolver()
           
 Catalog getCatalog(String catalogName)
          Looks up one catalog on the server
 List<Catalog> getCatalogs()
           
 String getDefaultCatalogName()
           
 TransactionIsolation getDefaultTransactionIsolation()
           
 DatabaseServerType getServerType()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseConnectionImpl

public DatabaseConnectionImpl(Connection connection)
Creates a DatabaseConnectionImpl backed by one physical database connection. Since there is only one connection, you can only have one ongoing operation against the database at one time. Trying to operate on the database with multiple threads will serialize to one thread accessing the database at a time through blocking. An active transaction counts as an ongoing operation; failing to properly close it (by committing or rolling back) may leave the DatabaseConnectionImpl unusable with some method calls blocking indefinitely.

The type of the database server will be guessed, but you can also be helpful and call the overloaded constructor which takes a Connection and a DatabaseServerType to specify what you are connecting to.

Parameters:
connection - Connection that is backing this DatabaseConnection
Throws:
IllegalArgumentException - If the database server type could not be figured out

DatabaseConnectionImpl

public DatabaseConnectionImpl(Connection connection,
                              DatabaseServerType databaseServerType)
Creates a DatabaseConnectionImpl backed by one physical database connection. Since there is only one connection, you can only have one ongoing operation against the database at one time. Trying to operate on the database with multiple threads will serialize to one thread accessing the database at a time through blocking. An active transaction counts as an ongoing operation; failing to properly close it (by committing or rolling back) may leave the DatabaseConnectionImpl unusable with some method calls blocking indefinitely.

Parameters:
connection - Connection that is backing this DatabaseConnection
databaseServerType - Type of the database server

DatabaseConnectionImpl

public DatabaseConnectionImpl(DataSource dataSource)
Creates a DatabaseConnectionImpl backed by a DataSource. The behavior of the constructed DatabaseConnectionImpl is very much determined by what kind of DataSource you are passing in as the first parameter. Typically, this will be a connection pool and as such you will be able to operated on this DatabaseConnectionImpl with multiple threads in parallel. Normally, if the pool is starved and cannot allocate any more connection, it will block until a connection is available. In this case, the DatabaseConnectionImpl will block too, when calling the methods that utilize the database connection. If the DataSource throws an exception when no more connections can be allocated, so will the DatabaseConnectionImpl.

Please note that a DataSource doesn't expose any method for closing it, so calling close() on this DatabaseConnectionImpl won't do anything. If you want to be able to close the underlying source through this object, please use one of the constructor overloads with a DataSourceCloser parameter.

The type of the database server will be guessed, but you can also be helpful and call an overloaded constructor which takes a DatabaseServerType to specify what you are connecting to.

Parameters:
dataSource - Underlying database connection supplier
Throws:
IllegalArgumentException - If the database server type could not be figured out

DatabaseConnectionImpl

public DatabaseConnectionImpl(DataSource dataSource,
                              DatabaseServerType databaseServerType)
Creates a DatabaseConnectionImpl backed by a DataSource. The behavior of the constructed DatabaseConnectionImpl is very much determined by what kind of DataSource you are passing in as the first parameter. Typically, this will be a connection pool and as such you will be able to operated on this DatabaseConnectionImpl with multiple threads in parallel. Normally, if the pool is starved and cannot allocate any more connection, it will block until a connection is available. In this case, the DatabaseConnectionImpl will block too, when calling the methods that utilize the database connection. If the DataSource throws an exception when no more connections can be allocated, so will the DatabaseConnectionImpl.

Please note that a DataSource doesn't expose any method for closing it, so calling close() on this DatabaseConnectionImpl won't do anything. If you want to be able to close the underlying source through this object, please use one of the constructor overloads with a DataSourceCloser parameter.

Parameters:
dataSource - Underlying database connection supplier
databaseServerType - Type of the database server

DatabaseConnectionImpl

public DatabaseConnectionImpl(DataSource dataSource,
                              DataSourceCloser dataSourceCloser)
Creates a DatabaseConnectionImpl backed by a DataSource. The behavior of the constructed DatabaseConnectionImpl is very much determined by what kind of DataSource you are passing in as the first parameter. Typically, this will be a connection pool and as such you will be able to operated on this DatabaseConnectionImpl with multiple threads in parallel. Normally, if the pool is starved and cannot allocate any more connection, it will block until a connection is available. In this case, the DatabaseConnectionImpl will block too, when calling the methods that utilize the database connection. If the DataSource throws an exception when no more connections can be allocated, so will the DatabaseConnectionImpl.

Please note that a DataSource doesn't expose any method for closing it, that why this constructor takes a DataSourceCloser interface as a parameter. It is assumed the closer knows how to close this particular DataSource. If you don't need to close the DataSource (through this DatabaseConnectionImpl), you can pass null for this parameter.

The type of the database server will be guessed, but you can also be helpful and call the overloaded constructor which takes a DataSource, a DataSourceCloser and a DatabaseServerType to specify what you are connecting to.

Parameters:
dataSource - Underlying database connection supplier
dataSourceCloser - Object which knows how to close the data source, or null if you want calls to close() be ignored
Throws:
IllegalArgumentException - If the database server type could not be figured out

DatabaseConnectionImpl

public DatabaseConnectionImpl(DataSource dataSource,
                              DataSourceCloser dataSourceCloser,
                              DatabaseServerType databaseServerType)
Creates a DatabaseConnectionImpl backed by a DataSource. The behavior of the constructed DatabaseConnectionImpl is very much determined by what kind of DataSource you are passing in as the first parameter. Typically, this will be a connection pool and as such you will be able to operated on this DatabaseConnectionImpl with multiple threads in parallel. Normally, if the pool is starved and cannot allocate any more connection, it will block until a connection is available. In this case, the DatabaseConnectionImpl will block too, when calling the methods that utilize the database connection. If the DataSource throws an exception when no more connections can be allocated, so will the DatabaseConnectionImpl.

Please note that a DataSource doesn't expose any method for closing it, that why this constructor takes a DataSourceCloser interface as a parameter. It is assumed the closer knows how to close this particular DataSource. If you don't need to close the DataSource (through this DatabaseConnectionImpl), you can pass null for this parameter.

Parameters:
dataSource - Underlying database connection supplier
dataSourceCloser - Object which knows how to close the data source, or null if you want calls to close() be ignored
databaseServerType - Type of the database server
Method Detail

getDefaultTransactionIsolation

public TransactionIsolation getDefaultTransactionIsolation()
Specified by:
getDefaultTransactionIsolation in interface DatabaseConnection
Returns:
Default transaction isolation of this connection

close

public void close()
Description copied from interface: DatabaseConnection
Closes this connection and any underlying data source

Specified by:
close in interface DatabaseConnection

beginTransaction

public DatabaseTransaction beginTransaction(TransactionIsolation transactionIsolation)
                                     throws SQLException
Description copied from interface: DatabaseConnection
Allocated a connection and begins a transaction

Specified by:
beginTransaction in interface DatabaseConnection
Parameters:
transactionIsolation - Isolation level for the new transaction
Returns:
DatabaseTransaction object representing the new transaction
Throws:
SQLException - If an error occurred when allocating a connection for this transaction

createAutoExecutor

public AutoExecutor createAutoExecutor()
Description copied from interface: DatabaseConnection
Creates an AutoExecutor using this database connection for supplying connections

Specified by:
createAutoExecutor in interface DatabaseConnection
Returns:
AutoExecutor using this connection

getCatalogs

public List<Catalog> getCatalogs()
                          throws SQLException
Specified by:
getCatalogs in interface DatabaseConnection
Returns:
List of all catalogs available on this server
Throws:
SQLException - If an error occurred when asking the database for catalog information

getCatalog

public Catalog getCatalog(String catalogName)
                   throws SQLException
Description copied from interface: DatabaseConnection
Looks up one catalog on the server

Specified by:
getCatalog in interface DatabaseConnection
Parameters:
catalogName - Name of the catalog
Returns:
Catalog object representing the catalog or null if not found
Throws:
SQLException - If an error occurred when asking the database for catalog information

getServerType

public DatabaseServerType getServerType()
Specified by:
getServerType in interface DatabaseConnection
Returns:
Type of the server this connection is connected to

getDefaultCatalogName

public String getDefaultCatalogName()
Specified by:
getDefaultCatalogName in interface DatabaseConnection
Returns:
Default catalog name of this connection

createMetaDataResolver

protected MetaDataResolver createMetaDataResolver()


Copyright © 2012. All Rights Reserved.