alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Transactions.scala)

This example Scala source code file (Transactions.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

abortexception, abortexception, compound, int, long, long, option, option, running, runtimeexception, t, transaction, transaction, unit

The Scala Transactions.scala source code

package scala.concurrent

class AbortException extends RuntimeException

object Transaction {
  private var cnt = 0L
  def nextId: Long = synchronized {
    cnt += 1; cnt
  }

  // Transaction status constants
  val Running = 0
  val Committed = 1
  val Abortable = 2
  val Aborted = 3
  val Compound = 4

  def atomic[T](b: Transaction => T): Option[T] =
    (new Transaction).run(b)
}

class Transaction {
  var status: Int = _

  var id: Long = _  // only for real transactions

  var head: Transaction = this
  var next: Transaction = null

  def this(hd: Transaction, tl: Transaction) = { this(); this.head = head; this.next = next }
  
  def makeAbort() = synchronized {
    while (status != Transaction.Aborted && status != Transaction.Committed) {
      status = Transaction.Abortable
      wait()
    }
  }
  private def abort() = synchronized { status = Transaction.Aborted; notifyAll() }
  private def commit() = synchronized { status = Transaction.Committed; notifyAll() }
  def run[T](b: Transaction => T): Option[T] =
    try {
      status = Transaction.Running
      id = Transaction.nextId
      val result = Some(b(this))
      commit()
      result
    } catch {
      case ex: AbortException => abort(); None
      case ex: Throwable => abort(); throw ex
    }
  
}

trait Transactional {

  /** create a new snapshot */
  def checkPoint(): Unit

  /** copy back snapshot */
  def rollBack(): Unit
  
  var readers: Transaction
  var writer: Transaction

  def currentWriter(): Transaction = null
    if (writer == null) null
    else if (writer.status == Transaction.Running) writer
    else {
      if (writer.status != Transaction.Committed) rollBack(); 
      writer = null; 
      null 
    }
  
  def getter(thisTrans: Transaction) {
    if (writer == thisTrans) return
    var r = readers
    while (r != null && r.head.status != Transaction.Running) { r = r.next; readers = r }
    while (r != null) {
      if (r.head == thisTrans) return
      val last = r
      r = r.next
      while (r != null && r.head.status != Transaction.Running) { r = r.next; last.next = r }
    }
    synchronized {
      if (thisTrans.status == Transaction.Abortable) throw new AbortException
      val w = currentWriter()
      if (w != null)
        if (thisTrans.id < w.id) { w.makeAbort(); rollBack(); writer = null }
        else throw new AbortException
      readers = if (readers == null) thisTrans else new Transaction(thisTrans, readers)
    }
  }

  def setter(thisTrans: Transaction) {
    if (writer == thisTrans) return
    synchronized {
      val w = currentWriter()
      if (w != null)
        if (thisTrans.id < w.id) { w.makeAbort(); rollBack() } 
        else throw new AbortException
      var r = readers
      while (r != null && r.head.status != Transaction.Running) { r = r.next; readers = r }
      while (r != null) {
        if (r.id < thisTrans.id) throw new AbortException
        else w.makeAbort()
        val last = r
        r = r.next
        while (r != null && r.head.status != Transaction.Running) { r = r.next; last.next = r }
      }
      checkPoint()
    }
  }
}
      

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Transactions.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.