- All Superinterfaces:
AutoCloseable
Manages a
Connection
instance, providing basic transaction control.
A factory for DatabaseConnection instances.-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
SQLException state indicating that a query did not return a result -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Performs a rollback and disconnects this connection.void
commit()
Performs a commitvoid
Performs a commit and ends the current transactionboolean
database()
static DatabaseConnection
databaseConnection
(Database database, User user) Constructs a new DatabaseConnection instance, based on the given Database and Userstatic DatabaseConnection
databaseConnection
(Database database, Connection connection) Constructs a new DatabaseConnection instance, based on the given Connection object.Returns the underlying connection object, null in case this connection has been closed.void
rollback()
Performs a rollbackvoid
Performs a rollback and ends the current transactionvoid
setConnection
(Connection connection) Sets the internal connection to use, note that no validation or transaction checking is performed on the connection and auto-commit is assumed to be disabled.void
setMethodLogger
(MethodLogger methodLogger) void
Starts a transaction on this connection, to end the transaction usecommitTransaction()
orrollbackTransaction()
.boolean
user()
-
Field Details
-
SQL_STATE_NO_DATA
SQLException state indicating that a query did not return a result- See Also:
-
-
Method Details
-
connected
boolean connected()- Returns:
- true if the connection has been established and is valid
-
getConnection
Connection getConnection()Returns the underlying connection object, null in case this connection has been closed. Useconnected()
to verify that the connection is not null and valid.- Returns:
- the underlying connection object, null in case this connection has been closed
-
setConnection
Sets the internal connection to use, note that no validation or transaction checking is performed on the connection and auto-commit is assumed to be disabled. The connection is simply used 'as is'. Note that setting the connection to null causes all methods requiring it to throw aIllegalStateException
until a non-null connection is set.- Parameters:
connection
- the JDBC connection
-
startTransaction
void startTransaction()Starts a transaction on this connection, to end the transaction usecommitTransaction()
orrollbackTransaction()
.- Throws:
IllegalStateException
- in case a transaction is already open
-
transactionOpen
boolean transactionOpen()- Returns:
- true if a transaction is open
-
commitTransaction
void commitTransaction()Performs a commit and ends the current transaction- Throws:
IllegalStateException
- in case a transaction is not open
-
rollbackTransaction
void rollbackTransaction()Performs a rollback and ends the current transaction- Throws:
IllegalStateException
- in case a transaction is not open
-
commit
Performs a commit- Throws:
SQLException
- thrown if anything goes wrong during the commitIllegalStateException
- in case a transaction is already open
-
rollback
Performs a rollback- Throws:
SQLException
- thrown if anything goes wrong during the rollbackIllegalStateException
- in case a transaction is open
-
close
void close()Performs a rollback and disconnects this connection. The only way to resurrect a closed connection is by usingsetConnection(Connection)
. Calling this method renders this connection unusable, subsequent calls to methods using the underlying connection result in aIllegalStateException
being thrown, that is, until a newConnection
is set viasetConnection(Connection)
.- Specified by:
close
in interfaceAutoCloseable
-
user
User user()- Returns:
- the connection user
-
database
Database database()- Returns:
- the database implementation this connection is based on
-
setMethodLogger
- Parameters:
methodLogger
- the MethodLogger to use, null to disable method logging
-
getMethodLogger
MethodLogger getMethodLogger()- Returns:
- the MethodLogger being used, possibly null
-
databaseConnection
Constructs a new DatabaseConnection instance, based on the given Database and User- Parameters:
database
- the databaseuser
- the user for the db-connection- Returns:
- a new DatabaseConnection instance
- Throws:
DatabaseException
- in case there is a problem connecting to the database
-
databaseConnection
static DatabaseConnection databaseConnection(Database database, Connection connection) throws DatabaseException Constructs a new DatabaseConnection instance, based on the given Connection object. NB. auto commit is disabled on the Connection that is provided.- Parameters:
database
- the databaseconnection
- the Connection object to base this DatabaseConnection on- Returns:
- a new DatabaseConnection instance
- Throws:
DatabaseException
- in case there is a problem with the connection
-