apache / apache/jena

Support passing a custom `TransactionalSystem` in `TDB2StorageBuilder`

Open
#2,200 3 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
1.4k
Forks
712
Avg merge
15h 41m
Merged PRs (30d)
53

Description

### Version

4.10.0

### Feature

I want to use Jena in a [cats-effect](https://typelevel.org/cats-effect/) Scala application.
Cats Effect uses an asynchronous runtime that implements many concurrency constructs in a "semantic" fashion (for example, blocking on a lock is implemented by waiting to execute a callback, instead of using an actual Java lock).

Cats Effect programs are split into many consecutive callbacks which are then executed by their runtime. The runtime uses many threads that execute the next action available for execution.
With Cats Effect I can implement transactions, but they don't work with Jena, since Jena assumes that your code runs sequentially on the same thread. This assumption is reflected in many places.
In [TransactionalBase](https://github.com/apache/jena/blob/3a580054a1b3481e12a23f1ea6d3123a9b8212e0/jena-db/jena-dboe-transaction/src/main/java/org/apache/jena/dboe/transaction/txn/TransactionalBase.java#L40) Jena maintains a `ThreadLocal` of a `Transaction` so only one can exist per thread.
In [TransactionCoordinator](https://github.com/apache/jena/blob/3a580054a1b3481e12a23f1ea6d3123a9b8212e0/jena-db/jena-dboe-transaction/src/main/java/org/apache/jena/dboe/transaction/txn/TransactionCoordinator.java#L97) Jena uses a `ReentrantReadWriteLock` which doesn't allow to lock it from a thread and unlock it from another.
In normal Java code, this makes sense since it's highly unsafe to lock from one thread and unlock from another.
But in Cats Effect code there are other safeguards to make sure that no race conditions happen.

I would like to customize TDB2 to allow using my own `TransactionalSystem`, but this is impossible at the moment since the only way to construct a `TDB2StorageBuilder` is via its [`build`](https://github.com/apache/jena/blob/22d30fdeedd3ad5756af73899dcdff257e2c69b9/jena-tdb2/src/main/java/org/apache/jena/tdb2/store/TDB2StorageBuilder.java#L80) static methods which hardcode the TransactionalSystem to use `TransactionalBase` here.

If I can implement my own `TransactionalSystem` I think I can disable the built-in transactionality and enforce it in other ways.

### Are you interested in contributing a solution yourself?

None

Contributor guide

Open the contributing guide

Research direction

Start with the static build methods in jena-tdb2/src/main/java/org/apache/jena/tdb2/store/TDB2StorageBuilder.java, then read TransactionalBase.java and TransactionCoordinator.java to understand the current transaction wiring. Determine how a caller could supply a custom TransactionalSystem and what existing construction behavior must remain compatible; done means TDB2StorageBuilder supports that customization.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, scala
Domain
backend, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.