Class DbConnection
- All Implemented Interfaces:
AutoCloseable
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.
- Since:
- 1.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Warning: only use the raw transaction methods if you really know what you're doing.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.Creates a newDbStatement
instance for this connection.createStatement
(int resultSetType, int resultSetConcurrency) Creates a newDbStatement
instance for this connection with the given type and concurrency.createStatement
(int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a newDbStatement
instance for this connection with the given type, concurrency, and holdability.Retrieves the datasource this connection has been obtained from.Retrieves aDatabaseMetaData
object that contains metadata about the database to which thisDbConnection
object represents a connection.Creates a newDbPreparedStatement
instance for this connection from a regular SQL query string.getPreparedStatement
(String sql, int autoGeneratedKeys) Creates a newDbPreparedStatement
instance for this connection from aQuery
instance that has the capability to retrieve auto-generated keys.getPreparedStatement
(Query query) Creates a newDbPreparedStatement
instance for this connection from aQuery
instance.getPreparedStatement
(Query query, int autoGeneratedKeys) Creates a newDbPreparedStatement
instance for this connection from aQuery
instance that has the capability to retrieve auto-generated keys.getPreparedStatement
(Query query, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a newDbPreparedStatement
instance for this connection from aQuery
instance that will generateResultSet
objects with the given type, concurrency, and holdability.boolean
isClosed()
Indicates whether thisDbConnection
's connection to the database is closed.boolean
isFree()
Indicates whether thisDbConnection
is free to execute statements for the current thread.boolean
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 thisDbConnection
object to the one given.boolean
Indicates whether theDatasource
of thisDbConnection
supports transactions or not.
-
Method Details
-
getDatasource
Retrieves the datasource this connection has been obtained from.- Returns:
- the
Datasource
instance this connection has been obtained from - Since:
- 1.0
-
close
Releases all the resources that are being used by this connection. If the connection has been obtained from a pooled datasource, it will not be closed, but reused later. If the datasource isn't pooled, the connection itself will be closed as expected.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.
- Specified by:
close
in interfaceAutoCloseable
- Throws:
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.- Since:
- 1.0
-
createStatement
Creates a newDbStatement
instance for this connection. It will be registered and automatically closed when thisDbConnection
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.- Returns:
- a new
DbStatement
instance - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbStatement
instance- Since:
- 1.0
- See Also:
-
createStatement
public DbStatement createStatement(int resultSetType, int resultSetConcurrency) throws DatabaseException Creates a newDbStatement
instance for this connection with the given type and concurrency. It will be registered and automatically closed when thisDbConnection
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.- Parameters:
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- Returns:
- a new
DbStatement
instance - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbStatement
instance- Since:
- 1.0
- See Also:
-
createStatement
public DbStatement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws DatabaseException Creates a newDbStatement
instance for this connection with the given type, concurrency, and holdability. It will be registered and automatically closed when thisDbConnection
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.- Parameters:
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- Returns:
- a new
DbStatement
instance - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbStatement
instance- Since:
- 1.0
- See Also:
-
getPreparedStatement
Creates a newDbPreparedStatement
instance for this connection from a regular SQL query string. Since the statement is created from aString
and not aParametrizedQuery
instance, information is lacking to be able to fully use the features of the resultingDbPreparedStatement
instance. It's recommended to use thegetPreparedStatement(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.- Parameters:
sql
- aString
instance with the SQL that is used to set up the prepared statement- Returns:
- a new
DbPreparedStatement
instance - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbPreparedStatement
instance- Since:
- 1.0
- See Also:
-
getPreparedStatement
public DbPreparedStatement getPreparedStatement(String sql, int autoGeneratedKeys) throws DatabaseException Creates a newDbPreparedStatement
instance for this connection from aQuery
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 aParametrizedQuery
instance, information is lacking to be able to fully use the features of the resultingDbPreparedStatement
instance. It's recommended to use thegetPreparedStatement(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.- Parameters:
sql
- aString
instance with the SQL that is used to set up the prepared statementautoGeneratedKeys
- a flag indicating whether auto-generated keys should be returned; one ofStatement.RETURN_GENERATED_KEYS
orStatement.NO_GENERATED_KEYS
- Returns:
- a new
DbPreparedStatement
instance - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbPreparedStatement
instance- Since:
- 1.0
- See Also:
-
getPreparedStatement
Creates a newDbPreparedStatement
instance for this connection from aQuery
instance. Thanks to the additional meta-data that's stored in aQuery
object, it's possible to use the additional features that theDbPreparedStatement
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.- Parameters:
query
- aQuery
instance that is used to set up the prepared statement- Returns:
- a new
DbPreparedStatement
instance - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbPreparedStatement
instance- Since:
- 1.0
- See Also:
-
getPreparedStatement
public DbPreparedStatement getPreparedStatement(Query query, int autoGeneratedKeys) throws DatabaseException Creates a newDbPreparedStatement
instance for this connection from aQuery
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 theDbPreparedStatement
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.- Parameters:
query
- aQuery
instance that is used to set up the prepared statementautoGeneratedKeys
- a flag indicating whether auto-generated keys should be returned; one ofStatement.RETURN_GENERATED_KEYS
orStatement.NO_GENERATED_KEYS
- Returns:
- a new
DbPreparedStatement
instance - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbPreparedStatement
instance- Since:
- 1.0
- See Also:
-
getPreparedStatement
public DbPreparedStatement getPreparedStatement(Query query, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws DatabaseException Creates a newDbPreparedStatement
instance for this connection from aQuery
instance that will generateResultSet
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 theDbPreparedStatement
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.- Parameters:
query
- aQuery
instance that is used to set up the prepared statementresultSetType
- one of the followingResultSet
constants:ResultSet.TYPE_FORWARD_ONLY
,ResultSet.TYPE_SCROLL_INSENSITIVE
, orResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency
- one of the followingResultSet
constants:ResultSet.CONCUR_READ_ONLY
orResultSet.CONCUR_UPDATABLE
resultSetHoldability
- one of the followingResultSet
constants:ResultSet.HOLD_CURSORS_OVER_COMMIT
orResultSet.CLOSE_CURSORS_AT_COMMIT
- Returns:
- a new
DbPreparedStatement
instance, that will generateResultSet
objects with the given type, concurrency, and holdability - Throws:
DatabaseException
- when an exception has occurred during the creation of theDbPreparedStatement
instance- Since:
- 1.0
- See Also:
-
supportsTransactions
Indicates whether theDatasource
of thisDbConnection
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.- Returns:
true
if theDatasource
supports transactions; orfalse
if theDatasource
doesn't support transactions.- Throws:
DatabaseException
- when an error occurred during the verification of the transaction support- Since:
- 1.0
- See Also:
-
beginTransaction
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 theDbQueryManager
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.- Returns:
true
if the transaction was successfully started; orfalse
if theDatasource
doesn't support transactions, or if a transaction is already active on thisDbConnection
.- Throws:
DatabaseException
- when an error occurred during the creation of the new transaction, or when the active transaction has timed-out.- Since:
- 1.0
- See Also:
-
commit
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 theDbQueryManager
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.- Returns:
true
if the transaction was successfully committed; orfalse
if theDatasource
doesn't support transactions, or when no transaction is active on thisDbConnection
, or when the executing thread isn't the thread that began the transaction.- Throws:
DatabaseException
- when an error occurred during the commit of the active transaction, or when the active transaction has timed-out.- Since:
- 1.0
- See Also:
-
rollback
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 theDbQueryManager
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.- Returns:
true
if the transaction was successfully rolled-back; orfalse
if theDatasource
doesn't support transactions, or when no transaction is active on thisDbConnection
, or when the executing thread isn't the thread that began the transaction.- Throws:
DatabaseException
- when an error occurred during the rollback of the active transaction, or when the active transaction has timed-out.- Since:
- 1.0
- See Also:
-
isFree
public boolean isFree()Indicates whether thisDbConnection
is free to execute statements for the current thread.- Returns:
true
if a statement can be executed by the current thread on thisDbConnection
; orfalse
if the connection is closed or when a transaction is already active on thisDbConnection
for another thread.- Since:
- 1.0
- See Also:
-
isTransactionValidForThread
public boolean isTransactionValidForThread()Indicates whether the current thread has a valid transaction going on for the execution of statements.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.- Returns:
true
if a transaction is active that can be used by the current thread; orfalse
if the connection is closed, doesn't support transactions, has no active transaction or has a transaction that was started by another thread.- Throws:
DatabaseException
- when errors occurred during the verification of the connection's open status and support for transactions- Since:
- 1.0
- See Also:
-
isClosed
Indicates whether thisDbConnection
'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.- Returns:
true
when thisDbConnection
is closed; orfalse
if it's connected.- Throws:
DatabaseException
- when an error occurred during the verification of the JDBC connection's closed status- Since:
- 1.0
-
getMetaData
Retrieves aDatabaseMetaData
object that contains metadata about the database to which thisDbConnection
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.- Returns:
- a
DatabaseMetaData
object for thisDbConnection
instance; ornull
if theDbConnection
instance is not connected. - Throws:
DatabaseException
- if a database access error occurs
-
setTransactionIsolation
Attempts to change the transaction isolation level for thisDbConnection
object to the one given. The constants defined in the interfaceConnection
are the possible transaction isolation levels.- Parameters:
level
- transaction isolation level constant defined in the{@link java.sql.Connection Connection}
interface- Throws:
DatabaseException
- if a database access error occurs- See Also:
-
clone
Simply clones the instance with the default clone method. This creates a shallow copy of all fields and the clone will in fact just be another reference to the same underlying data. The independence of each cloned instance is consciously not respected since they rely on resources that can't be cloned.
-