Skip to content

Instantly share code, notes, and snippets.

View archerz's full-sized avatar

Arash Shahkar archerz

  • GroupBy inc.
  • Toronto, Canada
View GitHub Profile
@archerz
archerz / nested-tx.md
Created May 24, 2022 19:39
On nested transactions, jOOQ and Hibernate

My Use Cases for Nested Transactions

I had two very similar yet slightly different use cases. Both can be summarized as “finding potentially many entities with incorrect state amongst many more, and fixing them one by one”. Each entity was a non-trivial aggregate object and already mapped using Hibernate annotations to 3-4 tables, wired up using Spring Data JPA. Fixing their state would require running complicated business logic implemented in Java. In one situation, a very large number of records were affected. So many that I ideally didn’t want to have to load all their IDs into memory. At the same time, finding the affected records required a really complex query that I had to write using jOOQ. So, this is what I tried to do:

  • Outer transaction: Lazy-fetch the IDs of affected entities using ResultSet.stream(), backed by a Postgres cursor (which requires a transaction). This transaction is read-only and has an isolation level of “read committed”.

    • Inner transaction (run for every affected entity): R

Keybase proof

I hereby claim:

  • I am archerz on github.
  • I am archerz (https://keybase.io/archerz) on keybase.
  • I have a public key ASBfVmdG1u7ACkM0Mmn2mcv1_OlLXVMJtA9J98dOAs10BQo

To claim this, I am signing this object:

@archerz
archerz / DateConversionConfig.java
Created March 26, 2018 17:42
MongoDB / Java ZonedDateTime Handling
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;