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

From version 166.1
edited by Ravi Mendis
on 2010/04/05 21:18
Change comment: There is no comment for this version
To version 169.1
edited by Pascal Robert
on 2012/02/11 08:28
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.rmendis
1 +XWiki.probert
Content
... ... @@ -1,11 +1,9 @@
1 -Given that processors today ship with dual or quad cores and server processors ship with between 8 to 32 cores, programs developed in languages like Java, Ruby, Groovy, Python and Perl are struggling 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.
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 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.
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 4  
5 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 -
6 +Concurrent programming in Scala is based on **Actors** (with an inbox) - a simple metaphor that is analogous to **Message Queues**.
9 9  {{/info}}
10 10  
11 11  == Using Scala Actors with EOF ==
... ... @@ -13,24 +13,82 @@
13 13  EOF being notoriously single-threaded, is incredibly unsuitable for use with Scala Actors:
14 14  
15 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
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
17 17  
18 18  Instead you can still use EOF but in a limited fashion - only to execute SQL.
19 19  
20 -=== Using EOAccess to execute SQL ===
18 +===== Using EOAccess to execute SQL =====
21 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)]]
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"]]
23 23  
24 -==== Caveats ====
22 +===== Caveats =====
25 25  
26 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 27  
28 -== EOF Alternatives ==
26 +You must also unlock attributes you update via SQL in order to prevent EOF optimistic locking exceptions.
29 29  
28 +== Squeryl ==
29 +
30 30  You may also manipulate the database from outside the EOF stack.
31 31  
32 -=== Squeryl ===
32 +[[Squeryl>>url:http://max-l.github.com/Squeryl/index.html||shape="rect"]] is..
33 33  
34 -[[Squeryl>>http://max-l.github.com/Squeryl/index.html]] is a strongly typed and declarative [[Domain-specific language>>http://en.wikipedia.org/wiki/Domain-specific_language]] for manipulating database objects from within the Scala language.{{quote}}{{/quote}}
34 +>A Scala ORM and [[Domain-specific language>>url:http://en.wikipedia.org/wiki/Domain-specific_language||shape="rect"]] for talking with Databases
35 35  
36 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:WebObjects and Squeryl]].
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"]]