liveBook · Manning

JdbcProjection

As explained in Akka in Action, Second Edition

JdbcProjection is an Akka Projection component that processes event streams and writes projected data to a JDBC-accessible database.

JdbcProjection connects an event-stream source to a JDBC-accessible database. It reads tagged events, passes them to a handler, and writes the resulting read model through a repository and database session. section 10.3.3 section 15.1.1

A practical projection assembles these parts:

  • An EventSourcedProvider.eventsByTag source selects events from the journal through a JDBC read-journal plugin. section 10.3.3 section 15.1.1
  • A handler interprets each event and invokes repository operations with the supplied database session. section 15.1.1
  • A session factory supplies ScalikeJdbcSession instances, while a projection identifier distinguishes each tagged projection. section 15.1.1
  • Tags enable parallel projections; separate instances can run on different nodes to scale out and isolate failures. section 10.3.3

Bet projection

A betting projection selects Bet.Event entries by tag. Its handler persists only Bet.Opened events by calling addBet with the event fields and session; other events are ignored. JdbcProjection.exactlyOnce combines the source provider, handler, repository-backed database writes, and session factory into one projection. section 15.1.1 section 15.1.1

Listing 10.8 CargosPerContainerProjection implementation
object CargosPerContainerProjection {
 
  def createProjectionFor(                                  #1
      system: ActorSystem[_],
      repository: CargosPerContainerRepository,
      indexTag: Int): ExactlyOnceProjection[
    Offset,
    EventEnvelope[SPContainer.Event]] = {
 
    val tag = "container-tag-" + indexTag                   #2
 
    val sourceProvider =                                    #3
      EventSourcedProvider.eventsByTag[SPContainer.Event](
        system = system,
        readJournalPluginId = JdbcReadJournal.Identifier,
        tag = tag)
 
    JdbcProjection.exactlyOnce(                             #4
      projectionId =
        ProjectionId("CargosPerContainerProjection", tag),
      sourceProvider = sourceProvider,
      handler =
        () => new CPCProjectionHandler(repository),
      sessionFactory = () => new ScalikeJdbcSession())(system)
  }
}

The implementation shows the complete construction pattern: derive a tag, create an events-by-tag source, and pass it to JdbcProjection.exactlyOnce with a handler and session factory. section 10.3.3

How Akka in Action, Second Edition covers it

How deep it goes
Explains source, handler, repository, session, tags, and exactly-once assembly, while deferring deeper database and stream internals.
Examples
Uses cargo-per-container and betting projections, including tagged events, repository inserts, and aggregated betting results.
What it assumes
Assumes familiarity with event journals, event tags, repositories, and JDBC sessions; supplies their roles in the projection assembly.
What you can do after
Build a tagged event-to-database projection with JdbcProjection.exactlyOnce, a handler, repository, and ScalikeJdbcSession factory.
How it connects
Connects event sourcing, EventSourcedProvider, JDBC, repository patterns, Akka Streams, and ShardedDaemonProcess.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.