[#1547] Support "optimistic locking" in UpdatableRecord.storeLocked() -
repeat integration test with another table, to check against more data types
This commit is contained in:
parent
add0476024
commit
b5e7029c60
@ -56,9 +56,11 @@ import org.jooq.InsertQuery;
|
||||
import org.jooq.Record;
|
||||
import org.jooq.SQLDialect;
|
||||
import org.jooq.StoreQuery;
|
||||
import org.jooq.TableField;
|
||||
import org.jooq.TableRecord;
|
||||
import org.jooq.UDTRecord;
|
||||
import org.jooq.UpdatableRecord;
|
||||
import org.jooq.UpdatableTable;
|
||||
import org.jooq.exception.DataAccessException;
|
||||
import org.jooq.exception.DataChangedException;
|
||||
import org.jooq.exception.InvalidResultException;
|
||||
@ -579,64 +581,74 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
|
||||
public void testStoreLocked() throws Exception {
|
||||
jOOQAbstractTest.reset = false;
|
||||
|
||||
testStoreLocked0(TBook(), TBook_ID(), TBook_TITLE());
|
||||
|
||||
// Avoid referential integrity problems for subsequent test
|
||||
create().executeDelete(TBook());
|
||||
testStoreLocked0(TAuthor(), TAuthor_ID(), TAuthor_LAST_NAME());
|
||||
}
|
||||
|
||||
private <R extends UpdatableRecord<R>> void testStoreLocked0(
|
||||
UpdatableTable<R> table, TableField<R, Integer> id, TableField<R, String> string) throws Exception {
|
||||
|
||||
// Storing without changing shouldn't execute any queries
|
||||
B book1 = create().fetchOne(TBook(), TBook_ID().equal(1));
|
||||
assertEquals(0, book1.storeLocked());
|
||||
assertEquals(0, book1.storeLocked());
|
||||
R record1 = create().fetchOne(table, id.equal(1));
|
||||
assertEquals(0, record1.storeLocked());
|
||||
assertEquals(0, record1.storeLocked());
|
||||
|
||||
// Succeed if there are no concurrency issues
|
||||
book1.setValue(TBook_TITLE(), "New Title 1");
|
||||
assertEquals(1, book1.storeLocked());
|
||||
assertEquals("New Title 1", create().fetchOne(TBook(), TBook_ID().equal(1)).getValue(TBook_TITLE()));
|
||||
record1.setValue(string, "New Title 1");
|
||||
assertEquals(1, record1.storeLocked());
|
||||
assertEquals("New Title 1", create().fetchOne(table, id.equal(1)).getValue(string));
|
||||
|
||||
// Get new books
|
||||
B book2 = create().fetchOne(TBook(), TBook_ID().equal(1));
|
||||
B book3 = create().fetchOne(TBook(), TBook_ID().equal(1));
|
||||
R record2 = create().fetchOne(table, id.equal(1));
|
||||
R record3 = create().fetchOne(table, id.equal(1));
|
||||
|
||||
// Still won't fail, but this will cause book3 to be stale
|
||||
book2.setValue(TBook_TITLE(), "New Title 2");
|
||||
assertEquals(1, book2.storeLocked());
|
||||
assertEquals("New Title 2", create().fetchOne(TBook(), TBook_ID().equal(1)).getValue(TBook_TITLE()));
|
||||
// Still won't fail, but this will cause record3 to be stale
|
||||
record2.setValue(string, "New Title 2");
|
||||
assertEquals(1, record2.storeLocked());
|
||||
assertEquals("New Title 2", create().fetchOne(table, id.equal(1)).getValue(string));
|
||||
|
||||
// Storing without changing shouldn't execute any queries
|
||||
assertEquals(0, book3.storeLocked());
|
||||
assertEquals(0, record3.storeLocked());
|
||||
|
||||
// This should fail as book3 is stale
|
||||
book3.setValue(TBook_TITLE(), "New Title 3");
|
||||
// This should fail as record3 is stale
|
||||
record3.setValue(string, "New Title 3");
|
||||
try {
|
||||
book3.storeLocked();
|
||||
record3.storeLocked();
|
||||
fail();
|
||||
}
|
||||
catch (DataChangedException expected) {}
|
||||
assertEquals("New Title 2", create().fetchOne(TBook(), TBook_ID().equal(1)).getValue(TBook_TITLE()));
|
||||
assertEquals("New Title 2", create().fetchOne(table, id.equal(1)).getValue(string));
|
||||
|
||||
// Refreshing first will work, though
|
||||
book3.refresh();
|
||||
book3.setValue(TBook_TITLE(), "New Title 3");
|
||||
assertEquals(1, book3.storeLocked());
|
||||
assertEquals("New Title 3", create().fetchOne(TBook(), TBook_ID().equal(1)).getValue(TBook_TITLE()));
|
||||
record3.refresh();
|
||||
record3.setValue(string, "New Title 3");
|
||||
assertEquals(1, record3.storeLocked());
|
||||
assertEquals("New Title 3", create().fetchOne(table, id.equal(1)).getValue(string));
|
||||
|
||||
// Get new books
|
||||
B book4 = create().fetchOne(TBook(), TBook_ID().equal(1));
|
||||
B book5 = create().fetchOne(TBook(), TBook_ID().equal(1));
|
||||
R record4 = create().fetchOne(table, id.equal(1));
|
||||
R record5 = create().fetchOne(table, id.equal(1));
|
||||
|
||||
// Delete the book
|
||||
assertEquals(1, book4.delete());
|
||||
assertEquals(1, record4.delete());
|
||||
|
||||
// Storing without changing shouldn't execute any queries
|
||||
assertEquals(0, book5.storeLocked());
|
||||
assertEquals(0, record5.storeLocked());
|
||||
|
||||
// This should fail, as the database record no longer exists
|
||||
book5.setValue(TBook_TITLE(), "New Title 5");
|
||||
record5.setValue(string, "New Title 5");
|
||||
try {
|
||||
book5.storeLocked();
|
||||
record5.storeLocked();
|
||||
fail();
|
||||
}
|
||||
catch (DataChangedException expected) {}
|
||||
|
||||
// Restore the book, then it should work
|
||||
assertEquals(1, book4.storeLocked());
|
||||
assertEquals(1, book5.storeLocked());
|
||||
assertEquals("New Title 5", create().fetchOne(TBook(), TBook_ID().equal(1)).getValue(TBook_TITLE()));
|
||||
assertEquals(1, record4.storeLocked());
|
||||
assertEquals(1, record5.storeLocked());
|
||||
assertEquals("New Title 5", create().fetchOne(table, id.equal(1)).getValue(string));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user