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 TypeMethodDescriptionbooleanWarning: only use the raw transaction methods if you really know what you're doing.clone()Simply clones the instance with the default clone method.voidclose()Releases all the resources that are being used by this connection.booleancommit()Warning: only use the raw transaction methods if you really know what you're doing.Creates a newDbStatementinstance for this connection.createStatement(int resultSetType, int resultSetConcurrency) Creates a newDbStatementinstance for this connection with the given type and concurrency.createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a newDbStatementinstance for this connection with the given type, concurrency, and holdability.Retrieves the datasource this connection has been obtained from.Retrieves aDatabaseMetaDataobject that contains metadata about the database to which thisDbConnectionobject represents a connection.Creates a newDbPreparedStatementinstance for this connection from a regular SQL query string.getPreparedStatement(String sql, int autoGeneratedKeys) Creates a newDbPreparedStatementinstance for this connection from aQueryinstance that has the capability to retrieve auto-generated keys.getPreparedStatement(Query query) Creates a newDbPreparedStatementinstance for this connection from aQueryinstance.getPreparedStatement(Query query, int autoGeneratedKeys) Creates a newDbPreparedStatementinstance for this connection from aQueryinstance that has the capability to retrieve auto-generated keys.getPreparedStatement(Query query, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a newDbPreparedStatementinstance for this connection from aQueryinstance that will generateResultSetobjects with the given type, concurrency, and holdability.booleanisClosed()Indicates whether thisDbConnection's connection to the database is closed.booleanisFree()Indicates whether thisDbConnectionis free to execute statements for the current thread.booleanIndicates whether the current thread has a valid transaction going on for the execution of statements.booleanrollback()Warning: only use the raw transaction methods if you really know what you're doing.voidsetTransactionIsolation(int level) Attempts to change the transaction isolation level for thisDbConnectionobject to the one given.booleanIndicates whether theDatasourceof thisDbConnectionsupports transactions or not.
-
Method Details
-
getDatasource
Retrieves the datasource this connection has been obtained from.- Returns:
- the
Datasourceinstance 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:
closein 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 newDbStatementinstance for this connection. It will be registered and automatically closed when thisDbConnectioncleans 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
DbConnectionis 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
DbStatementinstance - Throws:
DatabaseException- when an exception has occurred during the creation of theDbStatementinstance- Since:
- 1.0
- See Also:
-
createStatement
public DbStatement createStatement(int resultSetType, int resultSetConcurrency) throws DatabaseException Creates a newDbStatementinstance for this connection with the given type and concurrency. It will be registered and automatically closed when thisDbConnectioncleans 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
DbConnectionis 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
DbStatementinstance - Throws:
DatabaseException- when an exception has occurred during the creation of theDbStatementinstance- Since:
- 1.0
- See Also:
-
createStatement
public DbStatement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws DatabaseException Creates a newDbStatementinstance for this connection with the given type, concurrency, and holdability. It will be registered and automatically closed when thisDbConnectioncleans 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
DbConnectionis 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
DbStatementinstance - Throws:
DatabaseException- when an exception has occurred during the creation of theDbStatementinstance- Since:
- 1.0
- See Also:
-
getPreparedStatement
Creates a newDbPreparedStatementinstance for this connection from a regular SQL query string. Since the statement is created from aStringand not aParametrizedQueryinstance, information is lacking to be able to fully use the features of the resultingDbPreparedStatementinstance. It's recommended to use thegetPreparedStatement(Query)method instead if this is possible.The new statement will be registered and automatically closed when this
DbConnectioncleans 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
DbConnectionis 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- aStringinstance with the SQL that is used to set up the prepared statement- Returns:
- a new
DbPreparedStatementinstance - Throws:
DatabaseException- when an exception has occurred during the creation of theDbPreparedStatementinstance- Since:
- 1.0
- See Also:
-
getPreparedStatement
public DbPreparedStatement getPreparedStatement(String sql, int autoGeneratedKeys) throws DatabaseException Creates a newDbPreparedStatementinstance for this connection from aQueryinstance 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
Stringand not aParametrizedQueryinstance, information is lacking to be able to fully use the features of the resultingDbPreparedStatementinstance. It's recommended to use thegetPreparedStatement(Query)method instead if this is possible.The new statement will be registered and automatically closed when this
DbConnectioncleans 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
DbConnectionis 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- aStringinstance 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_KEYSorStatement.NO_GENERATED_KEYS- Returns:
- a new
DbPreparedStatementinstance - Throws:
DatabaseException- when an exception has occurred during the creation of theDbPreparedStatementinstance- Since:
- 1.0
- See Also:
-
getPreparedStatement
Creates a newDbPreparedStatementinstance for this connection from aQueryinstance. Thanks to the additional meta-data that's stored in aQueryobject, it's possible to use the additional features that theDbPreparedStatementprovides on top of regular JDBC methods.The new statement will be registered and automatically closed when this
DbConnectioncleans 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
DbConnectionis 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- aQueryinstance that is used to set up the prepared statement- Returns:
- a new
DbPreparedStatementinstance - Throws:
DatabaseException- when an exception has occurred during the creation of theDbPreparedStatementinstance- Since:
- 1.0
- See Also:
-
getPreparedStatement
public DbPreparedStatement getPreparedStatement(Query query, int autoGeneratedKeys) throws DatabaseException Creates a newDbPreparedStatementinstance for this connection from aQueryinstance 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
Queryobject, it's possible to use the additional features that theDbPreparedStatementprovides on top of regular JDBC methods.The new statement will be registered and automatically closed when this
DbConnectioncleans 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
DbConnectionis 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- aQueryinstance that is used to set up the prepared statementautoGeneratedKeys- a flag indicating whether auto-generated keys should be returned; one ofStatement.RETURN_GENERATED_KEYSorStatement.NO_GENERATED_KEYS- Returns:
- a new
DbPreparedStatementinstance - Throws:
DatabaseException- when an exception has occurred during the creation of theDbPreparedStatementinstance- Since:
- 1.0
- See Also:
-
getPreparedStatement
public DbPreparedStatement getPreparedStatement(Query query, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws DatabaseException Creates a newDbPreparedStatementinstance for this connection from aQueryinstance that will generateResultSetobjects with the given type, concurrency, and holdability.This method is the same as the
getPreparedStatementmethod 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
Queryobject, it's possible to use the additional features that theDbPreparedStatementprovides on top of regular JDBC methods.The new statement will be registered and automatically closed when this
DbConnectioncleans 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
DbConnectionis 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- aQueryinstance that is used to set up the prepared statementresultSetType- one of the followingResultSetconstants:ResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, orResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency- one of the followingResultSetconstants:ResultSet.CONCUR_READ_ONLYorResultSet.CONCUR_UPDATABLEresultSetHoldability- one of the followingResultSetconstants:ResultSet.HOLD_CURSORS_OVER_COMMITorResultSet.CLOSE_CURSORS_AT_COMMIT- Returns:
- a new
DbPreparedStatementinstance, that will generateResultSetobjects with the given type, concurrency, and holdability - Throws:
DatabaseException- when an exception has occurred during the creation of theDbPreparedStatementinstance- Since:
- 1.0
- See Also:
-
supportsTransactions
Indicates whether theDatasourceof thisDbConnectionsupports 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
DbConnectionis 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:
trueif theDatasourcesupports transactions; orfalseif theDatasourcedoesn'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
inTransactionmethod of theDbQueryManagerclass instead.Starts a new transaction if the
Datasourcesupports it.If an exception is thrown, this
DbConnectionis 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:
trueif the transaction was successfully started; orfalseif theDatasourcedoesn'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
inTransactionmethod of theDbQueryManagerclass 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
DbConnectionis 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:
trueif the transaction was successfully committed; orfalseif theDatasourcedoesn'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
inTransactionmethod of theDbQueryManagerclass 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
DbConnectionis 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:
trueif the transaction was successfully rolled-back; orfalseif theDatasourcedoesn'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 thisDbConnectionis free to execute statements for the current thread.- Returns:
trueif a statement can be executed by the current thread on thisDbConnection; orfalseif the connection is closed or when a transaction is already active on thisDbConnectionfor 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
DbConnectionis 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:
trueif a transaction is active that can be used by the current thread; orfalseif 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
DbConnectionis automatically cleaned up. Also, any ongoing transaction will be rolled-back automatically.- Returns:
truewhen thisDbConnectionis closed; orfalseif it's connected.- Throws:
DatabaseException- when an error occurred during the verification of the JDBC connection's closed status- Since:
- 1.0
-
getMetaData
Retrieves aDatabaseMetaDataobject that contains metadata about the database to which thisDbConnectionobject 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
DbConnectionis 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
DatabaseMetaDataobject for thisDbConnectioninstance; ornullif theDbConnectioninstance is not connected. - Throws:
DatabaseException- if a database access error occurs
-
setTransactionIsolation
Attempts to change the transaction isolation level for thisDbConnectionobject to the one given. The constants defined in the interfaceConnectionare 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.
-