Java 11+  ·  PostgreSQL  ·  Zero Dependencies

fencepost

Distributed concurrency toolkit for Java + PostgreSQL.

Three lock strategies - advisory, session, and lease - all backed by PostgreSQL. Zero dependencies beyond java.sql.

<dependency>
    <groupId>com.pivovarit</groupId>
    <artifactId>fencepost</artifactId>
</dependency>

Three locks, one interface.

Pick the strategy that fits your workload. All three implement the same API.

pg

advisory

PostgreSQL advisory locks. No table setup. Simplest option - just point at a DataSource and go.

simplest holds connection
tx

session

SELECT ... FOR UPDATE with monotonic fencing tokens. Rejects stale writes from superseded holders.

fencing tokens holds connection
ttl

lease

Timestamp TTL with optional heartbeat. Releases the connection immediately - best for long-running work.

fencing tokens auto-expiry heartbeat