-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support spring tx * Revert "support spring tx" This reverts commit 94dd068. * feat: add TxWatcher (#98) * fix: add the new usage to README.md
- Loading branch information
Showing
4 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/org/casbin/spring/boot/autoconfigure/TxWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.casbin.spring.boot.autoconfigure; | ||
|
||
import org.casbin.jcasbin.persist.Watcher; | ||
import org.springframework.transaction.support.TransactionSynchronization; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import static org.springframework.transaction.support.TransactionSynchronizationManager.isActualTransactionActive; | ||
import static org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization; | ||
|
||
public class TxWatcher implements Watcher { | ||
private final Watcher watcher; | ||
|
||
public TxWatcher(Watcher watcher) { | ||
this.watcher = watcher; | ||
} | ||
|
||
@Override | ||
public void setUpdateCallback(Runnable runnable) { | ||
this.watcher.setUpdateCallback(runnable); | ||
} | ||
|
||
@Override | ||
public void setUpdateCallback(Consumer<String> consumer) { | ||
this.watcher.setUpdateCallback(consumer); | ||
} | ||
|
||
@Override | ||
public void update() { | ||
if (isActualTransactionActive()) { | ||
registerSynchronization(new TransactionSynchronization() { | ||
@Override | ||
public void afterCommit() { | ||
watcher.update(); | ||
} | ||
}); | ||
} else { | ||
watcher.update(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters