Class JdbcSession
- java.lang.Object
-
- com.evolveum.midpoint.repo.sql.helpers.JdbcSession
-
- All Implemented Interfaces:
AutoCloseable
public class JdbcSession extends Object implements AutoCloseable
Wrapper around JDBCConnection
representing "session", typically a transaction. Connection is prepared for the transaction (auto-commit is disabled), butstartTransaction()
orstartReadOnlyTransaction()
must be called explicitly. While not typical, multiple transactions can be executed in sequence (not concurrently). Object isAutoCloseable
and can be used in try-with-resource blocks. If transaction is still active during closing the JDBC session, it commits the transaction. If database does not support read-only transactions directly,commit()
executes rollback instead.Provides convenient methods for handling exceptions and
OperationResult
s.All
SQLException
s are translated toSystemException
.
-
-
Constructor Summary
Constructors Constructor Description JdbcSession(@NotNull Connection connection, @NotNull SqlRepositoryConfiguration repoConfiguration, @NotNull com.querydsl.sql.Configuration querydslConfiguration)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addColumn(String tableName, com.querydsl.sql.ColumnMetadata column)
Alters table adding another column - intended for custom/extension columns.void
close()
void
commit()
Commits current transaction.Connection
connection()
SqlRepositoryConfiguration.Database
databaseType()
com.querydsl.sql.dml.SQLDeleteClause
delete(com.querydsl.sql.RelationalPath<?> entity)
void
executeStatement(String sql)
This is used for technical statements and throwsSystemException
.String
getNativeTypeName(int typeCode)
void
handleGeneralCheckedException(@NotNull Throwable ex, @Nullable OperationResult result)
Rolls back the transaction and throws exception.void
handleGeneralException(@NotNull Throwable ex, @Nullable OperationResult result)
Rolls back the transaction and throws exception.void
handleGeneralRuntimeException(@NotNull RuntimeException ex, @Nullable OperationResult result)
Rolls back the transaction and throws exception.com.querydsl.sql.dml.SQLInsertClause
insert(com.querydsl.sql.RelationalPath<?> entity)
Starts insert clause for specified entity.com.querydsl.sql.SQLQuery<?>
query()
Creates Querydsl query based on current Querydsl configuration and session's connection.void
rollback()
Rolls back the transaction.JdbcSession
startReadOnlyTransaction()
Starts read-only transaction and returnsthis
.JdbcSession
startTransaction()
Starts transaction and returnsthis
.JdbcSession
startTransaction(int transactionLevel)
Starts transaction with different transaction isolation level.
-
-
-
Constructor Detail
-
JdbcSession
public JdbcSession(@NotNull @NotNull Connection connection, @NotNull @NotNull SqlRepositoryConfiguration repoConfiguration, @NotNull @NotNull com.querydsl.sql.Configuration querydslConfiguration)
-
-
Method Detail
-
startTransaction
public JdbcSession startTransaction()
Starts transaction and returnsthis
.
-
startTransaction
public JdbcSession startTransaction(int transactionLevel)
Starts transaction with different transaction isolation level. This level will NOT be reverted to previous level after the end of transaction. It is advisable to use this only for short-lived JDBC sessions with special requirements.
-
startReadOnlyTransaction
public JdbcSession startReadOnlyTransaction()
Starts read-only transaction and returnsthis
.
-
commit
public void commit()
Commits current transaction. If read-only transaction is not supported by database it rolls back read-only transaction.
-
rollback
public void rollback()
Rolls back the transaction. See also varioushandle*Exception()
methods that do the same thing adding exception logging and changes to the operation result.
-
executeStatement
public void executeStatement(String sql) throws SystemException
This is used for technical statements and throwsSystemException
. Don't use this for unsafe concatenated statements with parameters!- Throws:
SystemException
-
addColumn
public void addColumn(String tableName, com.querydsl.sql.ColumnMetadata column)
Alters table adding another column - intended for custom/extension columns.
-
query
public com.querydsl.sql.SQLQuery<?> query()
Creates Querydsl query based on current Querydsl configuration and session's connection.
-
insert
public com.querydsl.sql.dml.SQLInsertClause insert(com.querydsl.sql.RelationalPath<?> entity)
Starts insert clause for specified entity. Check Querydsl docs on insert for more about various ways how to use it.
-
delete
public com.querydsl.sql.dml.SQLDeleteClause delete(com.querydsl.sql.RelationalPath<?> entity)
-
getNativeTypeName
public String getNativeTypeName(int typeCode)
-
connection
public Connection connection()
-
databaseType
public SqlRepositoryConfiguration.Database databaseType()
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
handleGeneralException
public void handleGeneralException(@NotNull @NotNull Throwable ex, @Nullable @Nullable OperationResult result)
Rolls back the transaction and throws exception. UseshandleGeneralCheckedException(java.lang.Throwable, com.evolveum.midpoint.schema.result.OperationResult)
orhandleGeneralRuntimeException(java.lang.RuntimeException, com.evolveum.midpoint.schema.result.OperationResult)
depending on the exception type.- Throws:
SystemException
- wrapping the exception used as parameterRuntimeException
- rethrows input exception if related to transaction serialization
-
handleGeneralRuntimeException
public void handleGeneralRuntimeException(@NotNull @NotNull RuntimeException ex, @Nullable @Nullable OperationResult result)
Rolls back the transaction and throws exception. If the exception is related to transaction serialization problems, the operation result does not record the error (non-fatal).- Throws:
SystemException
- wrapping the exception used as parameterRuntimeException
- rethrows input exception if related to transaction serialization
-
handleGeneralCheckedException
public void handleGeneralCheckedException(@NotNull @NotNull Throwable ex, @Nullable @Nullable OperationResult result)
Rolls back the transaction and throws exception.- Throws:
SystemException
- wrapping the exception used as parameter
-
-