Return to Snippet

Revision: 23999
at February 18, 2010 09:56 by sai-venkat


Updated Code
require "java"
require "clojure.jar"
include_class "clojure.lang.LockingTransaction"
include_class "clojure.lang.Ref"

my_account = Ref.new(1)

puts "Initial value #{my_account.deref}"

begin
  my_account.set(1000)
rescue java.lang.IllegalStateException
  puts "Reference updated outside a transaction.. Naughty.. :)"
end

puts "Value after failed update #{my_account.deref}"

Thread.new {LockingTransaction.run_in_transaction(Proc.new { my_account.set 1000 })}

Thread.new {LockingTransaction.run_in_transaction(Proc.new { sleep 0.1; my_account.set 21000 })} # Will fail because get commited after first transaction 

puts "Value after successful transaction #{my_account.deref}"

Revision: 23998
at February 18, 2010 09:55 by sai-venkat


Updated Code
require "java"
require "clojure.jar"
include_class "clojure.lang.LockingTransaction"
include_class "clojure.lang.Ref"

my_account = Ref.new(1)

puts "Initial value #{my_account.deref}"

begin
  my_account.set(1000)
rescue java.lang.IllegalStateException
  puts "Reference updated outside a transaction.. Naughty.. :)"
end

puts "Value after failed update #{my_account.deref}"

Thread.new {LockingTransaction.run_in_transaction(Proc.new { my_account.set 1000 })}
Thread.new {LockingTransaction.run_in_transaction(Proc.new { sleep 0.1; my_account.set 21000 })} # Will fail because get commited after first transaction 

puts "Value after successful transaction #{my_account.deref}"

Revision: 23997
at February 18, 2010 09:30 by sai-venkat


Initial Code
require "java"
require "clojure.jar"
include_class "clojure.lang.LockingTransaction"
include_class "clojure.lang.Ref"

my_account = Ref.new(1)

puts "Initial value #{my_account.deref}"

begin
  my_account.set(1000)
rescue java.lang.IllegalStateException
  puts "Reference updated outside a transaction.. Naughty.. :)"
end

puts "Value after failed update #{my_account.deref}"

LockingTransaction.run_in_transaction(Proc.new { my_account.set 1000 })

puts "Value after successful transaction #{my_account.deref}"

Initial URL

                                

Initial Description

                                

Initial Title
Clojure STM in JRuby

Initial Tags
ruby

Initial Language
Ruby