https://github.com/yusufaytas/dlock
Interval Lock
https://github.com/yusufaytas/dlock
interval java kotlin lock mysql postgres spring spring-boot timeout
Last synced: 29 days ago
JSON representation
Interval Lock
- Host: GitHub
- URL: https://github.com/yusufaytas/dlock
- Owner: yusufaytas
- License: apache-2.0
- Created: 2018-06-28T17:49:38.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-09-13T17:41:14.000Z (5 months ago)
- Last Synced: 2025-09-13T19:31:23.496Z (5 months ago)
- Topics: interval, java, kotlin, lock, mysql, postgres, spring, spring-boot, timeout
- Language: Java
- Homepage:
- Size: 93.8 KB
- Stars: 24
- Watchers: 2
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
DLock
========
[](https://www.apache.org/licenses/LICENSE-2.0.txt) [](https://travis-ci.org/yusufaytas/dlock)
DLock is a library to get an interval lock. An Interval Lock is a lock where the acquirer acquires the lock for a certain amount of time. Once the lock is acquired, it won't be released till the interval ends. Note that, each of the Interval Lock implementations guarantees atomicity over processes and threads. The lock can only be acquired by only one process or thread at a time as expected. The implementations differ based on the technology.
Feedback and pull-requests are welcome!
+ [Usage](#usage)
+ [Lock Implementations](#lock-implementations)
- [Postgres](#postgres)
- [MySQL](#mysql)
+ [Contributing](#contributing)
# Usage
## Requirements and dependencies
* Java 8
* slf4j-api
Note that, there should be only one global lock.
## Import project
#### maven
```xml
com.yusufaytas
dlock-spring
0.2.1
com.yusufaytas
dlock-jdbc
0.2.1
```
or you can import all
```xml
com.yusufaytas
dlock-all
0.2.1
```
#### gradle
```groovy
compile 'com.yusufaytas:dlock-spring:0.2.1'
compile 'com.yusufaytas:dlock-jdbc:0.2.1'
```
or you can import all
```groovy
compile 'com.yusufaytas:dlock-all:0.2.1'
```
## Add an Interval Lock Support
#### Spring Bean Config
An example lock support for Postgres can be added as follows
```xml
```
#### Java Code
```java
@Scheduled(cron = "*/1 * * * * *")
@TryLock(name = "example", owner = "dlock", lockFor = ONE_MINUTE)
public void exampleLock() {
System.out.println("lock works");
}
```
# Lock Implementations
## Jdbc
You need to execute the DDL at the target database with appropriate permissions to make the lock work.
### Postgres
We insert into postgres if there doesn't exist a lock. Please checkout the [Postgres DDL](https://github.com/yusufaytas/dlock/blob/master/dlock-jdbc/src/main/resources/ddls/postgres.ddl).
### MySQL
We get an exclusive lock on the lock table and insert a new lock if a conflicting lock doesn't exit. Please checkout the [MySQL DDL](https://github.com/yusufaytas/dlock/blob/master/dlock-jdbc/src/main/resources/ddls/mysql.ddl).
# Contributing
## Compiling project
```shell script
./mvnw clean install -Dgpg.skip
```
## License Header
```shell script
./mvnw license:format -Dgpg.skip
```