Skip to content

Distributed lock using database (works with h2, postgres, sql server, mysql)

License

Notifications You must be signed in to change notification settings

deodeveloper/distributedlockusingdatabase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

distributed lock using sql database

Distributed lock using database (works with h2, postgres, sql server, mysql, oracle) Remember to add appropriate jdbc driver in the classpath(like using maven dependency) to talk to the database.

Simple API - cross-compiled to work with jdk 8+ Usage -

  SqlDistributedLockManager sqlDistributedLockManager = new SqlDistributedLockManager(getDatabaseConnection(), 
        "LOCKS", Clock.systemDefaultZone());
        // acquire(lockName, lockOwnerId, DurationOfLock <for locksforever put a large duration>)
  boolean acquired = sqlDistributedLockManager.acquire(new LockRequest("sample-lock", "userId", Duration.ofMinutes(1)));
  if(acquired){
  // do your thing
  }
  //finally release the lock
  sqlDistributedLockManager.release("sample-lock", "userId")

Please create the table afront before using the api. Please add indexing on columns - ACQUIRED_BY, EXPIRES_AT DataBase table schema to be created -

      create table LOCKS
      (
      	ID varchar(512) not null
      		constraint LOCKS_pk
      			primary key nonclustered,
      	ACQUIRED_BY varchar(512) not null,
      	ACQUIRED_AT datetime2 not null,
      	EXPIRES_AT datetime2 not null
      )
      -- add indexing on ACQUIRED_BY, EXPIRES_AT
      go

About

Distributed lock using database (works with h2, postgres, sql server, mysql)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages