This page (revision-4) was last changed on 26-Sep-2023 13:03 by Dieter Käppel

This page was created on 06-Apr-2014 13:20 by Dieter Käppel

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
4 26-Sep-2023 13:03 8 KB Dieter Käppel to previous
3 23-Jun-2023 11:42 6 KB Dieter Käppel to previous | to last
2 06-Apr-2014 13:20 2 KB Dieter Käppel to previous | to last
1 06-Apr-2014 13:20 930 bytes Dieter Käppel to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 94 added 39 lines
!!!Optimistic Locking
Spring Data implementiert Optimistic Locking nur mit JPA. Mit folgendem Code kann man das Verhalten simulieren:
{{{
package com.intersult.r2dbc_test;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.r2dbc.mapping.event.BeforeConvertCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class PersonOptimisticLocking implements BeforeConvertCallback<Person> {
@Autowired
private DatabaseClient client;
@Override
public Publisher<Person> onBeforeConvert(Person person, SqlIdentifier table) {
if (person.getId() == null)
return Mono.just(person);
return client.sql("SELECT version FROM person WHERE id = :id")
.bind("id", person.getId())
.map(r -> r.get("version", Integer.class))
.first()
.map(version -> {
if (version != person.getVersion())
throw new OptimisticLockingFailureException("Optimistic locking conflict: your version: " + person.getVersion() + ", database version: " + version);
person.setVersion(version + 1);
return person;
});
}
}
}}}