Version 66.1 by Ravi Mendis on 2011/03/21 22:55

Hide last authors
Ravi Mendis 65.1 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>>http://en.wikipedia.org/wiki/Concurrent_computing]] a methodology that can take advantage of these modern multi-core processors.
Ravi Mendis 50.1 2
3 If you use Scala for a concurrent programming application, the chances are that you're using Scala [[Actors>>http://en.wikipedia.org/wiki/Actor_model]] to do that.
4
5 {{info title="Scala Actors"}}
6
7 Concurrent programming in Scala is based on *Actors* (with an inbox) - a simple metaphor that is analogous to *Message Queues*.
8
9 {{/info}}
10
11 == Using Scala Actors with EOF ==
12
13 EOF being notoriously single-threaded, is incredibly unsuitable for use with Scala Actors:
14
15 * EOs are mutable objects and as such they cannot be passed safely to Scala Actors
16 * Fetching and updating EOs from within Scala Actors can cause [[deadlocks>>http://en.wikipedia.org/wiki/Deadlock]] with the WebObjects application
17
18 Instead you can still use EOF but in a limited fashion - only to execute SQL.
19
20 ===== Using EOAccess to execute SQL =====
21
22 Use Wonder API: [[ERXEOAccessUtilities.evaluateSQLWithEntityNamed()>>http://webobjects.mdimension.com/hudson/job/Wonder53/javadoc/er/extensions/eof/ERXEOAccessUtilities.html#evaluateSQLWithEntity(com.webobjects.eocontrol.EOEditingContext,%20com.webobjects.eoaccess.EOEntity,%20java.lang.String)]]
23
24 ===== Caveats =====
25
26 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.
27
28 You must also unlock attributes you update via SQL in order to prevent EOF optimistic locking exceptions.
29
Ravi Mendis 66.1 30 == EOF Alternatives ==
Ravi Mendis 50.1 31
32 You may also manipulate the database from outside the EOF stack.
33
Ravi Mendis 66.1 34 === Squeryl ===
35
Ravi Mendis 50.1 36 [[Squeryl>>http://max-l.github.com/Squeryl/index.html]] is..
37
38 >A Scala ORM and [[Domain-specific language>>http://en.wikipedia.org/wiki/Domain-specific_language]] for talking with Databases{{quote}}{{/quote}}
39
40 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.
41
Ravi Mendis 56.1 42 To generate a Squeryl schema from an EO model see [[WebObjects and Squeryl]].
Ravi Mendis 50.1 43
Ravi Mendis 66.1 44 ==== Advantages ====
Ravi Mendis 56.1 45
Ravi Mendis 66.1 46 ===== 1. Immutable Objects =====
Ravi Mendis 56.1 47
Ravi Mendis 60.1 48 Squeryl allows you to define simple, [[POSOs>>http://en.wikipedia.org/wiki/Plain_Old_Java_Object]] that are immutable and so are able to be safely passed between Scala Actors.
Ravi Mendis 56.1 49
Ravi Mendis 66.1 50 ===== 2. Explicit Transaction Control =====
Ravi Mendis 56.1 51
52 Database transactions are explicit in Squeryl. All interactions with the database occur within transaction blocks.
53
Ravi Mendis 63.1 54 {{code}}
55
56 transaction {
57 from (songs) select(_) // SELECT All
58 }
59
60 {{/code}}
61
Ravi Mendis 65.1 62 This simple, yet explicit mechanism to control Squeryl database transactions allows the developer to construct transactions that may be issued concurrently.
Ravi Mendis 56.1 63
Ravi Mendis 66.1 64 ====== Examples: ======
Ravi Mendis 56.1 65
Ravi Mendis 64.1 66 * In general, database INSERTS are concurrency-friendly.
Ravi Mendis 56.1 67 * Partial updates:
68
69 {{code}}
70
71 update(songs)(s =>
72 where(s.title === "Watermelon Man")
73 set(s.year := s.year.~ + 1)
74 )
75
76 {{/code}}
77
Ravi Mendis 61.1 78 The partial update generates SQL like:
Ravi Mendis 56.1 79
80 {{code value="SQL"}}
81
82 update Song set
83 year = (year + 1)
84 Where
85 (title = ?)
86
87 {{/code}}
88
Ravi Mendis 61.1 89 This is a single SQL statement instead of a select and then an update. This is concurrency-friendly.
Ravi Mendis 56.1 90
Ravi Mendis 66.1 91 === External Links ===
Ravi Mendis 50.1 92
93 WOWODC '10 [[Slides>>http://www.wocommunity.org/wowodc10/slides/Scala+WO.pdf]] & [[Podcast>>http://www.wocommunity.org/podcasts/wowodc/2010/ScalaSession.mov]]
94 [[Actors in Scala - Chapter 1. Concurrency Everywhere>>http://www.artima.com/samples/ActorsInScalaPrePrintCh1.pdf]]