001/*
002 * Copyright 2015-2022 Transmogrify LLC, 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.pyranid;
018
019import org.jspecify.annotations.NonNull;
020import org.jspecify.annotations.Nullable;
021
022import java.sql.Connection;
023import java.util.Optional;
024
025/**
026 * Strategies for database locking during transactional operations.
027 *
028 * @author <a href="https://www.revetkn.com">Mark Allen</a>
029 * @since 1.0.0
030 */
031public enum TransactionIsolation {
032        /**
033         * Default isolation (DBMS-specific).
034         */
035        DEFAULT(null),
036
037        /**
038         * Maps to JDBC value {@link Connection#TRANSACTION_READ_COMMITTED}.
039         */
040        READ_COMMITTED(Connection.TRANSACTION_READ_COMMITTED),
041
042        /**
043         * Maps to JDBC value {@link Connection#TRANSACTION_READ_UNCOMMITTED}.
044         */
045        READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED),
046
047        /**
048         * Maps to JDBC value {@link Connection#TRANSACTION_REPEATABLE_READ}.
049         */
050        REPEATABLE_READ(Connection.TRANSACTION_REPEATABLE_READ),
051
052        /**
053         * Maps to JDBC value {@link Connection#TRANSACTION_SERIALIZABLE}.
054         */
055        SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE);
056
057        @Nullable
058        private final Integer jdbcLevel;
059
060        TransactionIsolation(@Nullable Integer jdbcLevel) {
061                this.jdbcLevel = jdbcLevel;
062        }
063
064        @NonNull
065        Optional<Integer> getJdbcLevel() {
066                return Optional.ofNullable(this.jdbcLevel);
067        }
068}