- All Superinterfaces:
AutoCloseable
Manages a
Connection instance, providing basic transaction control.
A factory for DatabaseConnection instances.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringSQLException state indicating that a query did not return a result -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Performs a rollback and disconnects this connection.voidcommit()Performs a commitvoidPerforms a commit and ends the current transactionbooleandatabase()static DatabaseConnectiondatabaseConnection(Database database, is.codion.common.user.User user) Constructs a new DatabaseConnection instance, based on the given Database and Userstatic DatabaseConnectiondatabaseConnection(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.is.codion.common.logging.MethodLoggervoidrollback()Performs a rollbackvoidPerforms a rollback and ends the current transactionvoidsetConnection(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.voidsetMethodLogger(is.codion.common.logging.MethodLogger methodLogger) voidStarts a transaction on this connection, to end the transaction usecommitTransaction()orrollbackTransaction().booleanis.codion.common.user.Useruser()
-
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 aIllegalStateExceptionuntil 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 aIllegalStateExceptionbeing thrown, that is, until a newConnectionis set viasetConnection(Connection).- Specified by:
closein interfaceAutoCloseable
-
user
is.codion.common.user.User user()- Returns:
- the connection user
-
database
Database database()- Returns:
- the database implementation this connection is based on
-
setMethodLogger
void setMethodLogger(is.codion.common.logging.MethodLogger methodLogger) - Parameters:
methodLogger- the MethodLogger to use, null to disable method logging
-
getMethodLogger
is.codion.common.logging.MethodLogger getMethodLogger()- Returns:
- the MethodLogger being used, possibly null
-
databaseConnection
static DatabaseConnection databaseConnection(Database database, is.codion.common.user.User user) throws DatabaseException 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
-