콘텐츠로 이동

2024 02 07

2024-02-07

Non-Repeatable Read vs Phantom Read

참고: https://velog.io/@hgo641/%ED%8A%B8%EB%9E%9C%EC%9E%AD%EC%85%98-%EA%B2%A9%EB%A6%AC-%EC%88%98%EC%A4%80#non-repeatable-read-vs-phantom-read

  • Non-Repeatable Read
    • 하나의 레코드에 초점된 문제
    • 트랜잭션 A에서 레코드의 값을 UPDATE 했을 때,
    • 트랜잭션 B가 해당 레코드가 포함된 집합을 읽어올 때 데이터 정합성이 깨짐
  • Phantom Read
    • 레코드 집합에 초점된 문제
    • 트랜잭션 A가 레코드를 INSERT하거나 DELETE 했을 때,
    • 트랜잭션 B가 해당 레코드가 포함된 집합을 읽어올 때 데이터 정합성이 깨짐

Scala Enum

참고: https://wonyong-jang.github.io/scala/2021/09/26/Scala-Case-Object-Enumerations.html 참고: https://www.baeldung.com/scala/enumerations 참고: https://www.baeldung.com/scala/case-objects-vs-enumerations

  • 개요
    • Enum을 추상 클래스로 제공 : abstract class Enumeration (initial: Int) extends Serializable
  • 실습

    object Fingers extends Enumeration {
        type Finger = Value
    
        val Thumb, Index, Middle, Ring, Little = Value
    }
    

    • Enumeration 클래스는 Value라는 타입을 제공해 각 Enum 값을 나타냄
    • 한번 만든 Enum에 대해 값 추가하는 것은 쓰레드 세이프 하지 않음
  • Value
    • Value 생성자는 여러개가 있음 (사실 결국 생성은 후술할 Val 임)
      • id/name으로 구성됨
        • id: Int (auto_inc default 0)
        • name: String
            protected final def Value: Value = Value(nextId)
          
            /** Creates a fresh value, part of this enumeration, identified by the
          
              *  integer `i`.
              *
          
              *  @param i An integer that identifies this value at run-time. It must be
              *           unique amongst all values of the enumeration.
              *  @return  Fresh value identified by `i`.
                 */
                 protected final def Value(i: Int): Value = Value(i, nextNameOrNull)
          
            /** Creates a fresh value, part of this enumeration, called `name`.
              *
          
              *  @param name A human-readable name for that value.
              *  @return  Fresh value called `name`.
                 */
                 protected final def Value(name: String): Value = Value(nextId, name)
          
            /** Creates a fresh value, part of this enumeration, called `name`
          
              *  and identified by the integer `i`.
              *
          
              * @param i    An integer that identifies this value at run-time. It must be
              *             unique amongst all values of the enumeration.
              * @param name A human-readable name for that value.
              * @return     Fresh value with the provided identifier `i` and name `name`.
                */
                protected final def Value(i: Int, name: String): Value = new Val(i, name)
          
          object Fingers extends Enumeration {
              type Finger = Value
          
              val Thumb = Value(1, "Thumb Finger")
              val Index = Value(2, "Pointing Finger")
              val Middle = Value(3, "The Middle Finger")
              val Ring = Value(4, "Finger With The Ring")
              val Little = Value(5, "Shorty Finger")
          }
          
  • 그럼 값을 2개밖에 못 써?
    • Enumeration Class에서는 Val이라는 inner 클래스가 존재.
      • 이게 extend 되면서 추가 속성을 가질 수 있음
        /** A class implementing the [[scala.Enumeration.Value]] type. This class
        
         *  can be overridden to change the enumeration's naming and integer
         *  identification behaviour.
         */
        @SerialVersionUID(0 - 3501153230598116017L)
        protected class Val(i: Int, name: String) extends Value with Serializable {
          def this(i: Int)       = this(i, nextNameOrNull)
          def this(name: String) = this(nextId, name)
          def this()             = this(nextId)
        
          assert(!vmap.isDefinedAt(i), "Duplicate id: " + i)
          vmap(i) = this
          vsetDefined = false
          nextId = i + 1
          if (nextId > topId) topId = nextId
          if (i < bottomId) bottomId = i
          def id: Int = i
          override def toString(): String =
            if (name != null) name
            else try thisenum.nameOf(i)
            catch { case _: NoSuchElementException => "<Invalid enum: no field for #" + i + ">" }
        
          protected def readResolve(): AnyRef = {
            val enumeration = thisenum.readResolve().asInstanceOf[Enumeration]
            if (enumeration.vmap == null) this
            else enumeration.vmap(i)
          }
        }
        
        protected case class FingerDetails(i: Int, name: String, height: Double)
          extends super.Val(i, name) {
          def heightInCms(): Double = height * 2.54
        }
        

Case Object vs Enum

참고: https://www.baeldung.com/scala/case-objects-vs-enumerations

  • 개요
    • Enum은 OOP 친구
    • Scala Enum의 문제
      • Type Erasure를 통해 런타임엔 다 똑같은 타입이 되버림

Gap Lock & Next-Key Lock

참고: https://idea-sketch.tistory.com/46