Last modified by Ravi Mendis on 2012/02/11 08:28

Show last authors
1 Processors today ship with dual or quad cores. Server processors ship with between 8 to 32 cores. Programs that are developed in languages like Java, Ruby, Groovy, Python and Perl struggle to exploit all that power. On the other hand, Scala, as its name suggests is built from the ground-up for [[concurrent programming>>url:http://en.wikipedia.org/wiki/Concurrent_computing||shape="rect"]] - a methodology that can take advantage of these modern multi-core processors.
2
3 If you use Scala for a concurrent programming application, the chances are that you're using Scala [[Actors>>url:http://en.wikipedia.org/wiki/Actor_model||shape="rect"]] to do that.
4
5 {{info title="Scala Actors"}}
6 Concurrent programming in Scala is based on **Actors** (with an inbox) - a simple metaphor that is analogous to **Message Queues**.
7 {{/info}}
8
9 == Using Scala Actors with EOF ==
10
11 EOF being notoriously single-threaded, is incredibly unsuitable for use with Scala Actors:
12
13 * EOs are mutable objects and as such they cannot be passed safely to Scala Actors
14 * Fetching and updating EOs from within Scala Actors can cause [[deadlocks>>url:http://en.wikipedia.org/wiki/Deadlock||shape="rect"]] with the WebObjects application
15
16 Instead you can still use EOF but in a limited fashion - only to execute SQL.
17
18 ===== Using EOAccess to execute SQL =====
19
20 Use Wonder API: [[ERXEOAccessUtilities.evaluateSQLWithEntityNamed()>>url:http://wocommunity.org/documents/javadoc/wonder/latest/er/extensions/eof/ERXEOAccessUtilities.html#evaluateSQLWithEntity(com.webobjects.eocontrol.EOEditingContext,%20com.webobjects.eoaccess.EOEntity,%20java.lang.String)||shape="rect"]]
21
22 ===== Caveats =====
23
24 If you're updating the state of EOs directly in the database using SQL from Scala Actors, you will subsequently need to refresh/refetch these EOs for the WebObjects application to see those changes.
25
26 You must also unlock attributes you update via SQL in order to prevent EOF optimistic locking exceptions.
27
28 == Squeryl ==
29
30 You may also manipulate the database from outside the EOF stack.
31
32 [[Squeryl>>url:http://max-l.github.com/Squeryl/index.html||shape="rect"]] is..
33
34 >A Scala ORM and [[Domain-specific language>>url:http://en.wikipedia.org/wiki/Domain-specific_language||shape="rect"]] for talking with Databases
35
36 You may update the database from Scala Actors using Squeryl instead of using EOF. The advantage here is that you may access the database concurrently avoiding the single-threaded EOF bottleneck in your application. However the same caveat applies - you will need to refresh EOs in the EOF stack for the WebObjects application to reflect the changes made by Squeryl.
37
38 To generate a Squeryl schema from an EO model see [[doc:documentation.Home.How-tos.WebObjects with Scala.WebObjects and Squeryl.WebHome]].
39
40 === Advantages ===
41
42 ==== 1. Immutable Objects ====
43
44 Squeryl allows you to define simple, [[POSOs>>url:http://en.wikipedia.org/wiki/Plain_Old_Java_Object||shape="rect"]] that are immutable and so are able to be safely passed between Scala Actors.
45
46 ==== 2. Explicit Transaction Control ====
47
48 Database transactions are explicit in Squeryl. All interactions with the database occur within transaction blocks.
49
50 {{code}}
51
52 transaction {
53 from (songs) select(_) // SELECT * FROM SONG
54 }
55
56 {{/code}}
57
58 This simple, yet explicit mechanism to control Squeryl database transactions allows the developer to construct transactions that may be issued concurrently.
59
60 ===== Examples: =====
61
62 * In general, database INSERTs are concurrency-friendly.
63 * ALL SELECTs are concurrency-friendly.
64 * Certain partial updates:
65
66 {{code}}
67
68 update(songs)(s =>
69 where(s.title === "Watermelon Man")
70 set(s.year := s.year.~ + 1)
71 )
72
73 {{/code}}
74
75 The partial update generates SQL like:
76
77 {{code 0="SQL"}}
78
79 update Song set
80 year = (year + 1)
81 Where
82 (title = ?)
83
84 {{/code}}
85
86 This is a single SQL statement instead of a select and then an update. This is concurrency-friendly.
87
88 === References ===
89
90 * WOWODC '10 [[Slides>>url:http://www.wocommunity.org/wowodc10/slides/Scala+WO.pdf||shape="rect"]] & [[Podcast>>url:http://www.wocommunity.org/podcasts/wowodc/2010/ScalaSession.mov||shape="rect"]]
91 * [[Actors in Scala - Chapter 1. Concurrency Everywhere>>url:http://www.artima.com/samples/ActorsInScalaPrePrintCh1.pdf||shape="rect"]]
92 * Scala Actors [[A Short Tutorial>>url:http://www.scala-lang.org/node/242||shape="rect"]]