001package com.avaje.ebean;
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 * 
016 * @see TxScope
017 */
018public enum TxIsolation {
019
020  /**
021   * Read Committed Isolation level. This is typically the default for most
022   * configurations.
023   */
024  READ_COMMITED(Connection.TRANSACTION_READ_COMMITTED),
025
026  /**
027   * Read uncommitted Isolation level.
028   */
029  READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED),
030
031  /**
032   * Repeatable Read Isolation level.
033   */
034  REPEATABLE_READ(Connection.TRANSACTION_REPEATABLE_READ),
035
036  /**
037   * Serializable Isolation level.
038   */
039  SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE),
040
041  /**
042   * No Isolation level.
043   */
044  NONE(Connection.TRANSACTION_NONE),
045
046  /**
047   * The default isolation level. This typically means the default that the
048   * DataSource is using or configured to use.
049   */
050  DEFAULT(-1);
051
052  final int level;
053
054  TxIsolation(int level) {
055    this.level = level;
056  }
057
058  /**
059   * Return the level as per java.sql.Connection.
060   * <p>
061   * Note that -1 denotes the default isolation level.
062   * </p>
063   */
064  public int getLevel() {
065    return level;
066  }
067
068  /**
069   * Return the TxIsolation given the java.sql.Connection isolation level.
070   * <p>
071   * Note that -1 denotes the default isolation level.
072   * </p>
073   */
074  public static TxIsolation fromLevel(int connectionIsolationLevel) {
075
076    switch (connectionIsolationLevel) {
077    case Connection.TRANSACTION_READ_UNCOMMITTED:
078      return TxIsolation.READ_UNCOMMITTED;
079
080    case Connection.TRANSACTION_READ_COMMITTED:
081      return TxIsolation.READ_COMMITED;
082
083    case Connection.TRANSACTION_REPEATABLE_READ:
084      return TxIsolation.REPEATABLE_READ;
085
086    case Connection.TRANSACTION_SERIALIZABLE:
087      return TxIsolation.SERIALIZABLE;
088
089    case Connection.TRANSACTION_NONE:
090      return TxIsolation.NONE;
091
092    case -1:
093      return TxIsolation.DEFAULT;
094
095    default:
096      throw new RuntimeException("Unknown isolation level " + connectionIsolationLevel);
097    }
098
099  }
100}