|
22 | 22 |
|
23 | 23 | import java.time.Duration; |
24 | 24 | import java.util.Arrays; |
| 25 | +import java.util.Collections; |
25 | 26 |
|
26 | 27 | import static io.r2dbc.spi.IsolationLevel.READ_COMMITTED; |
27 | 28 | import static io.r2dbc.spi.IsolationLevel.READ_UNCOMMITTED; |
@@ -52,6 +53,54 @@ void isInTransaction() { |
52 | 53 | .doOnSuccess(ignored -> assertThat(connection.isInTransaction()).isFalse())); |
53 | 54 | } |
54 | 55 |
|
| 56 | + @Test |
| 57 | + void autoRollbackPreRelease() { |
| 58 | + // Mock pool allocate/release. |
| 59 | + complete(conn -> conn.postAllocate() |
| 60 | + .thenMany(conn.createStatement("CREATE TEMPORARY TABLE test (id INT NOT NULL PRIMARY KEY)") |
| 61 | + .execute()) |
| 62 | + .flatMap(MySqlResult::getRowsUpdated) |
| 63 | + .then(conn.beginTransaction()) |
| 64 | + .thenMany(conn.createStatement("INSERT INTO test VALUES (1)") |
| 65 | + .execute()) |
| 66 | + .flatMap(MySqlResult::getRowsUpdated) |
| 67 | + .single() |
| 68 | + .doOnNext(it -> assertThat(it).isEqualTo(1)) |
| 69 | + .doOnSuccess(ignored -> assertThat(conn.isInTransaction()).isTrue()) |
| 70 | + .then(conn.preRelease()) |
| 71 | + .doOnSuccess(ignored -> assertThat(conn.isInTransaction()).isFalse()) |
| 72 | + .then(conn.postAllocate()) |
| 73 | + .thenMany(conn.createStatement("SELECT * FROM test") |
| 74 | + .execute()) |
| 75 | + .flatMap(it -> it.map((row, metadata) -> row.get(0, Integer.class))) |
| 76 | + .count() |
| 77 | + .doOnNext(it -> assertThat(it).isZero())); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void shouldNotRollbackCommittedPreRelease() { |
| 82 | + // Mock pool allocate/release. |
| 83 | + complete(conn -> conn.postAllocate() |
| 84 | + .thenMany(conn.createStatement("CREATE TEMPORARY TABLE test (id INT NOT NULL PRIMARY KEY)") |
| 85 | + .execute()) |
| 86 | + .flatMap(MySqlResult::getRowsUpdated) |
| 87 | + .then(conn.beginTransaction()) |
| 88 | + .thenMany(conn.createStatement("INSERT INTO test VALUES (1)") |
| 89 | + .execute()) |
| 90 | + .flatMap(MySqlResult::getRowsUpdated) |
| 91 | + .single() |
| 92 | + .doOnNext(it -> assertThat(it).isEqualTo(1)) |
| 93 | + .then(conn.commitTransaction()) |
| 94 | + .then(conn.preRelease()) |
| 95 | + .doOnSuccess(ignored -> assertThat(conn.isInTransaction()).isFalse()) |
| 96 | + .then(conn.postAllocate()) |
| 97 | + .thenMany(conn.createStatement("SELECT * FROM test") |
| 98 | + .execute()) |
| 99 | + .flatMap(it -> it.map((row, metadata) -> row.get(0, Integer.class))) |
| 100 | + .collectList() |
| 101 | + .doOnNext(it -> assertThat(it).isEqualTo(Collections.singletonList(1)))); |
| 102 | + } |
| 103 | + |
55 | 104 | @Test |
56 | 105 | void transactionDefinitionLockWaitTimeout() { |
57 | 106 | complete(connection -> connection.beginTransaction(MySqlTransactionDefinition.builder() |
|
0 commit comments