001package io.ebean.annotation;
002
003import java.sql.Connection;
004
005/**
006 * The Transaction Isolation levels.
007 * <p>
008 * These match those of java.sql.Connection with the addition of DEFAULT which
009 * implies the configured default of the DataSource.
010 * </p>
011 * <p>
012 * This can be used with TxScope to define transactional scopes to execute
013 * method within.
014 * </p>
015 */
016public enum TxIsolation {
017
018  /**
019   * Read Committed Isolation level. This is typically the default for most
020   * configurations.
021   */
022  READ_COMMITED(Connection.TRANSACTION_READ_COMMITTED),
023
024  /**
025   * Read uncommitted Isolation level.
026   */
027  READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED),
028
029  /**
030   * Repeatable Read Isolation level.
031   */
032  REPEATABLE_READ(Connection.TRANSACTION_REPEATABLE_READ),
033
034  /**
035   * Serializable Isolation level.
036   */
037  SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE),
038
039  /**
040   * No Isolation level.
041   */
042  NONE(Connection.TRANSACTION_NONE),
043
044  /**
045   * The default isolation level. This typically means the default that the
046   * DataSource is using or configured to use.
047   */
048  DEFAULT(-1);
049
050  final int level;
051
052  TxIsolation(int level) {
053    this.level = level;
054  }
055
056  /**
057   * Return the level as per java.sql.Connection.
058   * <p>
059   * Note that -1 denotes the default isolation level.
060   * </p>
061   */
062  public int getLevel() {
063    return level;
064  }
065
066  /**
067   * Return the TxIsolation given the java.sql.Connection isolation level.
068   * <p>
069   * Note that -1 denotes the default isolation level.
070   * </p>
071   */
072  public static TxIsolation fromLevel(int connectionIsolationLevel) {
073
074    switch (connectionIsolationLevel) {
075      case Connection.TRANSACTION_READ_UNCOMMITTED:
076        return TxIsolation.READ_UNCOMMITTED;
077
078      case Connection.TRANSACTION_READ_COMMITTED:
079        return TxIsolation.READ_COMMITED;
080
081      case Connection.TRANSACTION_REPEATABLE_READ:
082        return TxIsolation.REPEATABLE_READ;
083
084      case Connection.TRANSACTION_SERIALIZABLE:
085        return TxIsolation.SERIALIZABLE;
086
087      case Connection.TRANSACTION_NONE:
088        return TxIsolation.NONE;
089
090      case -1:
091        return TxIsolation.DEFAULT;
092
093      default:
094        throw new RuntimeException("Unknown isolation level " + connectionIsolationLevel);
095    }
096
097  }
098}