Building Concurrent Applications with WebObjects and Scala

Version 51.1 by Ravi Mendis on 2011/03/21 01:02
Warning
For security reasons, the document is displayed in restricted mode as it is not the current version. There may be differences and errors due to this.

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  a methodology that can take advantage of these modern multi-core processors.

If you use Scala for a concurrent programming application, the chances are that you're using Scala Actors to do that.

Information
Scala Actors

Concurrent programming in Scala is based on *Actors* (with an inbox) - a simple metaphor that is analogous to *Message Queues*.

Using Scala Actors with EOF

EOF being notoriously single-threaded, is incredibly unsuitable for use with Scala Actors:

  • EOs are mutable objects and as such they cannot be passed safely to Scala Actors
  • Fetching and updating EOs from within Scala Actors can cause deadlocks with the WebObjects application

Instead you can still use EOF but in a limited fashion - only to execute SQL.

Using EOAccess to execute SQL

Use Wonder API: ERXEOAccessUtilities.evaluateSQLWithEntityNamed()

Caveats

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.

You must also unlock attributes you update via SQL in order to prevent EOF optimistic locking exceptions.

EOF Alternatives

You may also manipulate the database from outside the EOF stack.

Squeryl

Squeryl is..

A Scala ORM and Domain-specific language for talking with DatabasesUnknown macro: quote. Click on this message for details.

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.

To generate a Squeryl schema from an EO model see WebObjects and Squeryl.

Advantages

1. Immutable Objects

Squeryl allows you to define simple, POSOs that are immutable and so are able to be safely passed to and between Scala Actors.

2. Fine Grained Transaction Control

Database transactions are explicit in Squeryl. All interactions with the database occur within transaction blocks. 

This simple, yet explicit mechanism to control the transactions Squeryl performs on the database allows the developer to construct concurrent transactions.

Examples:
  • All database INSERTS are concurrency-friendly by default
  • Partial updates:

update(songs)(s =>
 where(s.title === "Watermelon Man")
 set(s.year  := s.year.~ + 1)
)

This generates SQL like:


update Song set
 year = (year + 1)
Where
  (title = ?)

The above SQL doesn't perform a select and then an update. So it is concurrency-friendly.

WOWODC '10 Slides & Podcast
Actors in Scala - Chapter 1. Concurrency Everywhere