Building Concurrent Applications with WebObjects and Scala
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 - 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.
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.
Squeryl
You may also manipulate the database from outside the EOF stack.
Squeryl is..
A Scala ORM and Domain-specific language for talking with Databases
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 between Scala Actors.
2. Explicit Transaction Control
Database transactions are explicit in Squeryl. All interactions with the database occur within transaction blocks.
transaction {
from (songs) select(_) // SELECT * FROM SONG
}
This simple, yet explicit mechanism to control Squeryl database transactions allows the developer to construct transactions that may be issued concurrently.
Examples:
- In general, database INSERTs are concurrency-friendly.
- ALL SELECTs are concurrency-friendly.
- Certain partial updates:
update(songs)(s =>
where(s.title === "Watermelon Man")
set(s.year := s.year.~ + 1)
)
The partial update generates SQL like:
update Song set
year = (year + 1)
Where
(title = ?)
This is a single SQL statement instead of a select and then an update. This is concurrency-friendly.
References
- WOWODC '10 Slides & Podcast
- Actors in Scala - Chapter 1. Concurrency Everywhere
- Scala Actors A Short Tutorial