|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.uwyn.rife.database.DbConnection
public class DbConnection
Represents one connection to a database. A connection has to be obtained by
using the getConnection method on a Datasource
instance. The resulting DbConnection instance can be used to
obtain statement objects from and to manage transactions.
Statements are used to execute SQL queries either in a static or in a
prepared fashion. This corresponds to the DbStatement and
DbPreparedStatement classes. Look there for details about how
to use them. A DbConnection keeps track of which statements
have been openened and will automatically close them when database access
errors occur or when the connection itself is closed.
Several statements can be executed as a whole through the use of
transactions. Only if they all succeeded, the transaction should be
committed and all the modifications will be preserved. Otherwise, the
transaction should be rolled back, and the modifications will not be
integrated into the general data storage. When a transaction has been
started through the beginTransaction() method, it will be
bound to the currently executing thread. Other threads will not be able to
manipulate the transaction status and if they obtain and execute
statements, they will be put in a wait state and woken up again after the
transaction has finished.
Datasource.getConnection(),
DbStatement,
DbPreparedStatement| Method Summary | |
|---|---|
boolean |
beginTransaction()
Warning: only use the raw transaction methods if you really know what you're doing. |
Object |
clone()
Simply clones the instance with the default clone method. |
void |
close()
Releases all the resources that are being used by this connection. |
boolean |
commit()
Warning: only use the raw transaction methods if you really know what you're doing. |
DbStatement |
createStatement()
Creates a new DbStatement instance for this connection. |
DbStatement |
createStatement(int resultSetType,
int resultSetConcurrency)
Creates a new DbStatement instance for this connection with
the given type and concurrency. |
DbStatement |
createStatement(int resultSetType,
int resultSetConcurrency,
int resultSetHoldability)
Creates a new DbStatement instance for this connection with
the given type, concurrency, and holdability.. |
protected void |
finalize()
Ensures that this DbConnection is correctly cleaned-up
when it's garbage collected. |
Datasource |
getDatasource()
Retrieves the datasource this connection has been obtained from. |
DatabaseMetaData |
getMetaData()
Retrieves a DatabaseMetaData object that contains metadata
about the database to which this DbConnection object
represents a connection. |
DbPreparedStatement |
getPreparedStatement(Query query)
Creates a new DbPreparedStatement instance for this
connection from a Query instance. |
DbPreparedStatement |
getPreparedStatement(Query query,
int autoGeneratedKeys)
Creates a new DbPreparedStatement instance for this
connection from a Query instance that has the capability
to retrieve auto-generated keys. |
DbPreparedStatement |
getPreparedStatement(Query query,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability)
Creates a new DbPreparedStatement instance for this
connection from a Query instance that will generate
ResultSet objects with the given type, concurrency,
and holdability. |
DbPreparedStatement |
getPreparedStatement(String sql)
Creates a new DbPreparedStatement instance for this
connection from a regular SQL query string. |
DbPreparedStatement |
getPreparedStatement(String sql,
int autoGeneratedKeys)
Creates a new DbPreparedStatement instance for this
connection from a Query instance that has the capability
to retrieve auto-generated keys. |
boolean |
isClosed()
Indicates whether this DbConnection's connection to the
database is closed. |
boolean |
isFree()
Indicates whether this DbConnection is free to execute
statements for the current thread. |
boolean |
isTransactionValidForThread()
Indicates whether the current thread has a valid transaction going on for the execution of statements. |
boolean |
rollback()
Warning: only use the raw transaction methods if you really know what you're doing. |
void |
setTransactionIsolation(int level)
Attempts to change the transaction isolation level for this DbConnection object to the one given. |
boolean |
supportsTransactions()
Indicates whether the Datasource of this
DbConnection supports transactions or not. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public Datasource getDatasource()
Datasource instance this connection has been
obtained from
public void close()
throws DatabaseException
Any ongoing transactions will be automatically rolled-back.
All open statements will be closed and if a transaction is active, it will be automatically rolled back and unregistered.
DatabaseException - if a database access error occurs, or if
an error occurred during the closing of an ongoing transaction, or if
an error occurred during the closing of the opened statements, or if an
error occurred during the closing of the underlying JDBC connection.
public DbStatement createStatement()
throws DatabaseException
DbStatement instance for this connection. It
will be registered and automatically closed when this
DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
DbStatement instance
DatabaseException - when an exception has occurred during the
creation of the DbStatement instanceDbStatement,
getPreparedStatement(String),
getPreparedStatement(Query)
public DbStatement createStatement(int resultSetType,
int resultSetConcurrency)
throws DatabaseException
DbStatement instance for this connection with
the given type and concurrency. It
will be registered and automatically closed when this
DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency - a concurrency type; one of
ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
DbStatement instance
DatabaseException - when an exception has occurred during the
creation of the DbStatement instanceDbStatement,
getPreparedStatement(String),
getPreparedStatement(Query)
public DbStatement createStatement(int resultSetType,
int resultSetConcurrency,
int resultSetHoldability)
throws DatabaseException
DbStatement instance for this connection with
the given type, concurrency, and holdability.. It
will be registered and automatically closed when this
DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency - a concurrency type; one of
ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLEresultSetHoldability - one of the following ResultSet constants:
ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
DbStatement instance
DatabaseException - when an exception has occurred during the
creation of the DbStatement instanceDbStatement,
getPreparedStatement(String),
getPreparedStatement(Query)
public DbPreparedStatement getPreparedStatement(String sql)
throws DatabaseException
DbPreparedStatement instance for this
connection from a regular SQL query string. Since the statement is
created from a String and not a
ParametrizedQuery instance, information is lacking to be
able to fully use the features of the resulting
DbPreparedStatement instance. It's recommended to use the
getPreparedStatement(Query) method instead if this is
possible.
The new statement will be registered and automatically closed when
this DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
sql - a String instance with the SQL that is used to
set up the prepared statement
DbPreparedStatement instance
DatabaseException - when an exception has occurred during the
creation of the DbPreparedStatement instanceDbPreparedStatement,
createStatement(),
getPreparedStatement(Query)
public DbPreparedStatement getPreparedStatement(String sql,
int autoGeneratedKeys)
throws DatabaseException
DbPreparedStatement instance for this
connection from a Query instance that has the capability
to retrieve auto-generated keys. The given constant tells the driver
whether it should make auto-generated keys available for retrieval.
This parameter is ignored if the SQL statement is not an INSERT
statement.
Since the statement is created from a String and not a
ParametrizedQuery instance, information is lacking to be
able to fully use the features of the resulting
DbPreparedStatement instance. It's recommended to use the
getPreparedStatement(Query) method instead if this is
possible.
The new statement will be registered and automatically closed when
this DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
sql - a String instance with the SQL that is used to
set up the prepared statementautoGeneratedKeys - a flag indicating whether auto-generated keys
should be returned; one of Statement.RETURN_GENERATED_KEYS
or Statement.NO_GENERATED_KEYS
DbPreparedStatement instance
DatabaseException - when an exception has occurred during the
creation of the DbPreparedStatement instanceDbPreparedStatement,
createStatement(),
getPreparedStatement(Query)
public DbPreparedStatement getPreparedStatement(Query query)
throws DatabaseException
DbPreparedStatement instance for this
connection from a Query instance. Thanks to the additional
meta-data that's stored in a Query object, it's possible
to use the additional features that the
DbPreparedStatement provides on top of regular JDBC
methods.
The new statement will be registered and automatically closed when
this DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
query - a Query instance that is used to set up the
prepared statement
DbPreparedStatement instance
DatabaseException - when an exception has occurred during the
creation of the DbPreparedStatement instanceDbPreparedStatement,
createStatement(),
getPreparedStatement(String)
public DbPreparedStatement getPreparedStatement(Query query,
int autoGeneratedKeys)
throws DatabaseException
DbPreparedStatement instance for this
connection from a Query instance that has the capability
to retrieve auto-generated keys. The given constant tells the driver
whether it should make auto-generated keys available for retrieval.
This parameter is ignored if the SQL statement is not an INSERT
statement.
Thanks to the additional meta-data that's stored in a
Query object, it's possible to use the additional features
that the DbPreparedStatement provides on top of regular
JDBC methods.
The new statement will be registered and automatically closed when
this DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
query - a Query instance that is used to set up the
prepared statementautoGeneratedKeys - a flag indicating whether auto-generated keys
should be returned; one of Statement.RETURN_GENERATED_KEYS
or Statement.NO_GENERATED_KEYS
DbPreparedStatement instance
DatabaseException - when an exception has occurred during the
creation of the DbPreparedStatement instanceDbPreparedStatement,
createStatement(),
getPreparedStatement(String)
public DbPreparedStatement getPreparedStatement(Query query,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability)
throws DatabaseException
DbPreparedStatement instance for this
connection from a Query instance that will generate
ResultSet objects with the given type, concurrency,
and holdability.
This method is the same as the getPreparedStatement method
above, but it allows the default result set
type, concurrency, and holdability to be overridden.
Thanks to the additional meta-data that's stored in a
Query object, it's possible to use the additional features
that the DbPreparedStatement provides on top of regular
JDBC methods.
The new statement will be registered and automatically closed when
this DbConnection cleans up. It is recommended though to
manually close the statement when it's not needed anymore for sensible
resource preservation.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
query - a Query instance that is used to set up the
prepared statementresultSetType - one of the following ResultSet
constants:
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency - one of the following ResultSet
constants:
ResultSet.CONCUR_READ_ONLY or
ResultSet.CONCUR_UPDATABLEresultSetHoldability - one of the following ResultSet
constants:
ResultSet.HOLD_CURSORS_OVER_COMMIT or
ResultSet.CLOSE_CURSORS_AT_COMMIT
DbPreparedStatement instance, that will generate
ResultSet objects with the given type,
concurrency, and holdability
DatabaseException - when an exception has occurred during the
creation of the DbPreparedStatement instanceDbPreparedStatement,
createStatement(),
getPreparedStatement(String)
public boolean supportsTransactions()
throws DatabaseException
Datasource of this
DbConnection supports transactions or not.
This information is only retrieved once and cached for the rest of the lifetime of this connection.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
true if the Datasource supports
transactions; or
false if the Datasource doesn't support
transactions.
DatabaseException - when an error occurred during the
verification of the transaction supportbeginTransaction(),
commit(),
rollback(),
isFree(),
isTransactionValidForThread()
public boolean beginTransaction()
throws DatabaseException
Warning: only use the raw transaction methods if
you really know what you're doing. It's almost always better to use the
inTransaction
method of the DbQueryManager class instead.
Starts a new transaction if the Datasource supports it.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
true if the transaction was successfully started;
or
false if the Datasource doesn't support
transactions, or if a transaction is already active on this
DbConnection.
DatabaseException - when an error occurred during the creation
of the new transaction, or when the active transaction has timed-out.DbQueryManager.inTransaction(DbTransactionUser),
supportsTransactions(),
commit(),
rollback(),
isFree(),
isTransactionValidForThread()
public boolean commit()
throws DatabaseException
Warning: only use the raw transaction methods if
you really know what you're doing. It's almost always better to use the
inTransaction
method of the DbQueryManager class instead.
Commits an active transaction.
All transaction-related resources are cleared and all the threads that are waiting for the transaction to terminate are woken up.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
true if the transaction was successfully
committed; or
false if the Datasource doesn't support
transactions, or when no transaction is active on this
DbConnection, or when the executing thread isn't the
thread that began the transaction.
DatabaseException - when an error occurred during the commit
of the active transaction, or when the active transaction has
timed-out.DbQueryManager.inTransaction(DbTransactionUser),
supportsTransactions(),
beginTransaction(),
rollback(),
isFree(),
isTransactionValidForThread()
public boolean rollback()
throws DatabaseException
Warning: only use the raw transaction methods if
you really know what you're doing. It's almost always better to use the
inTransaction
method of the DbQueryManager class instead.
Rolls-back an active transaction.
All transaction-related resources are cleared and all the threads that are waiting for the transaction to terminate are woken up.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
true if the transaction was successfully
rolled-back; or
false if the Datasource doesn't support
transactions, or when no transaction is active on this
DbConnection, or when the executing thread isn't the
thread that began the transaction.
DatabaseException - when an error occurred during the rollback
of the active transaction, or when the active transaction has
timed-out.DbQueryManager.inTransaction(DbTransactionUser),
supportsTransactions(),
beginTransaction(),
commit(),
isFree(),
isTransactionValidForThread()public boolean isFree()
DbConnection is free to execute
statements for the current thread.
true if a statement can be executed by the current
thread on this DbConnection; or
false if the connection is closed or when a transaction
is already active on this DbConnection for another thread.
supportsTransactions(),
beginTransaction(),
commit(),
rollback(),
isTransactionValidForThread()public boolean isTransactionValidForThread()
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again.
true if a transaction is active that can be used
by the current thread; or
false if the connection is closed, doesn't support
transactions, has no active transaction or has a transaction that was
started by another thread.
DatabaseException - when errors occurred during the
verification of the connection's open status and support for
transactionssupportsTransactions(),
beginTransaction(),
commit(),
rollback(),
isFree()
public boolean isClosed()
throws DatabaseException
DbConnection's connection to the
database is closed.
If an exception is thrown, this DbConnection is
automatically cleaned up. Also, any ongoing transaction will be
rolled-back automatically.
true when this DbConnection is
closed; or
false if it's connected.
DatabaseException - when an error occurred during the
verification of the JDBC connection's closed status
public DatabaseMetaData getMetaData()
throws DatabaseException
DatabaseMetaData object that contains metadata
about the database to which this DbConnection object
represents a connection. The metadata includes information about the
database's tables, its supported SQL grammar, its stored procedures,
the capabilities of this connection, and so on.
If an exception is thrown, this DbConnection is
automatically closed and if it's part of a pool, all the other
connections are closed too and the pool is set up again. Also, any
ongoing transaction will be rolled-back automatically.
DatabaseMetaData object for this
DbConnection instance; or
null if the DbConnection instance is not
connected.
DatabaseException - if a database access error occurs
public void setTransactionIsolation(int level)
throws DatabaseException
DbConnection object to the one given. The constants
defined in the interface Connection are the possible
transaction isolation levels.
level - transaction isolation level constant defined in the Connection interface
DatabaseException - if a database access error occursConnection
protected void finalize()
throws Throwable
DbConnection is correctly cleaned-up
when it's garbage collected.
finalize in class ObjectThrowable - if an error occurred during the finalizationpublic Object clone()
clone in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||