java - jOOQ transaction does not work as expected - Stack Overflow
I have a back-end API that does a simple 'getOrCreate' operation on DB.
For some reason, this API can get called twice at the same time, hence I have made a change to not create the record twice (by adding a transaction). However, every single time that the code runs, two records are created.
Two API calls share the same DSLContext instance, but each call runs on a separate thread.
Sample code:
public static Record getOrCreateData(int id, DSLContext dslContext) {
var result = dslContext.transactionResult(config -> {
var dsl = DSL.using(config);
var existingData = dsl
.selectFrom(TABLE1)
.where(TABLE1.LINK_ID.eq(id))
.fetchOptionalInto(RecordDto.class);
if ( existingData.isPresent() ) {
return existingData.get();
}
var record = dsl.newRecord(TABLE1);
...
record.store();
return record.into(RecordDto.class);
});
return result;
}
I'm using Hikari CP with Postgresql database with default config.
How can I fix this?
最新文章
- Windows 8取胜机会:10个值得考虑的因素
- Pre-increment and Post-increment in C - Stack Overflow
- Spring Boot 3: Exclude REST Endpoints from Authorization - Stack Overflow
- electron - Windows task scheduler triggers the task but it is not invoking the app which I want to open on system login - Stack
- open source - Langgraph State not being passed properly with Ollama - Stack Overflow
- django - Multipe one-to-many relation between two models - Stack Overflow
- excel - Sum with multiple criteria and first matches only (excluding duplicates) - Stack Overflow
- uart - MH-z19b stop working after 999999 millis (NodeMCU v3 ESP8266 12F) - Stack Overflow
- stata - Fill in missing values by group for all the variables in the dataset - Stack Overflow
- monit missing file in sys directory when restarting monit - Stack Overflow
- bitcoin - Problem with sign PSBT.Error sign transaction - Stack Overflow
- android - How to setImageCaptureResolutionSelector() in CameraController? - Stack Overflow
- How to programmatically trigger "Next desktop background" in Windows using C#? - Stack Overflow
- python - Unet isn't working orand i'm not using it correctly - Stack Overflow
- javascript - AJAX implementation doesn't work properly for "like" button ASP.NET Core 2.1 - Stack Over
- utf 8 - correct way to Import database dump in PowerShell - Stack Overflow
- powerbi - How do I use GenerateSeries with a dynamic end value? - Stack Overflow