concept record in category akka

appears as: records, record
Akka in Action

This is an excerpt from Manning's book Akka in Action.

The figure shows one of the possible flows through the code, for adding a Message to a Conversation. You can imagine that there are numerous variations of database access flows through the use of the DAOs. Allowing any party to mutate or query records at any point in time can lead to performance problems that we can’t predict, like deadlocks and other issues. It’s exactly the kind of complexity we want to avoid.

Listing 5.8. Using Promise to create a Future API
def sendToKafka(record: ProducerRecord): Future[RecordMetadata] = {   val promise: Promise[[RecordMetadata] = Promise[RecordMetadata]() #1   val future: Future[RecordMetadata] = promise.future #2   val callback = new Callback() { #3     def onCompletion(metadata: RecordMetadata, e: Exception): Unit = {       if (e != null) promise.failure(e) #4       else promise.success(metadata) #5     }   }   producer.send(record, callback) #6   future #7 } #1 - Creates a Promise of the expected result type, RecordMetadata #2 - Gets a reference to the Future[RecordMetadata] that we can pass on #3 - This is the Kafka callback that’s used to indicate that sending has completed. It’s called once after sending the record has completed on another thread. #4 - Writes a failure to the promise if there’s an error. #5 - Writes a success to the promise otherwise #6 - Does the actual sending, passing in the callback #7 - Returns the future to the user of the sendToKafka method !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!

It’s quite common to use a database API for creating, retrieving, updating, and deleting records (also known as CRUD operations). Database records are often used to represent the current state of a system. The database acts as a container for shared mutable state in this case.

15.1.1. Updating records in place

Updating records in place is commonplace when using a SQL database for OLTP (online transaction processing). Figure 15.1 shows how SQL insert and update statements can be used to keep track of the calculation result shown in the calculator.

Figure 15.1. Storing state with updates

The calculator inserts one row on startup; the statement for finding out if the record already exists is omitted. Every calculation is executed in the database, using update statements on one row in the table. (You can assume that every update will be executed in its own transaction.)