Class JdbcSession

  • All Implemented Interfaces:
    AutoCloseable

    public class JdbcSession
    extends Object
    implements AutoCloseable
    Wrapper around JDBC Connection representing "session", typically a transactional one. The construction can be fluently followed by startTransaction() or its variants. Without starting the transaction connection will likely be in auto-commit mode. Use commit() or rollback() to finish the transaction. While not typical, multiple transactions can be executed in sequence (not concurrently). The next transaction starts immediately after committing/rolling back the previous one, there is no need to explicitly start another transaction if one of startTransaction() methods was called before. Using multiple transactions is in general discouraged in favour of multiple try-with-resource blocks each using new session (physical SQL connections are pooled, of course). Object is AutoCloseable and can be used in try-with-resource blocks (which is preferred). 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 OperationResults.

    All SQLExceptions are translated to SystemException.

    • Method Detail

      • startTransaction

        public JdbcSession startTransaction()
        Starts transaction and returns this.
      • 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 returns this.
      • 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 various handle*Exception() methods that do the same thing adding exception logging and changes to the operation result.
      • addColumn

        public void addColumn​(String tableName,
                              com.querydsl.sql.ColumnMetadata column)
        Alters table adding another column - intended for custom/extension columns.
      • newQuery

        public com.querydsl.sql.SQLQuery<?> newQuery()
        Creates Querydsl query based on current Querydsl configuration and session's connection.
      • newInsert

        public com.querydsl.sql.dml.SQLInsertClause newInsert​(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.
      • newUpdate

        public com.querydsl.sql.dml.SQLUpdateClause newUpdate​(com.querydsl.sql.RelationalPath<?> entity)
      • newDelete

        public com.querydsl.sql.dml.SQLDeleteClause newDelete​(com.querydsl.sql.RelationalPath<?> entity)
      • getNativeTypeName

        public String getNativeTypeName​(int typeCode)
      • 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 parameter
        RuntimeException - 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