Skip to content

Commit 92c75a0

Browse files
Formatting
1 parent 3c828ee commit 92c75a0

File tree

3 files changed

+101
-111
lines changed

3 files changed

+101
-111
lines changed

transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/DefaultPersistor.java

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package com.gruelbox.transactionoutbox;
22

3-
import lombok.AccessLevel;
4-
import lombok.AllArgsConstructor;
5-
import lombok.Builder;
6-
import lombok.experimental.SuperBuilder;
7-
import lombok.extern.slf4j.Slf4j;
8-
93
import java.io.IOException;
104
import java.io.Reader;
115
import java.io.StringWriter;
@@ -15,6 +9,11 @@
159
import java.util.ArrayList;
1610
import java.util.List;
1711
import java.util.Optional;
12+
import lombok.AccessLevel;
13+
import lombok.AllArgsConstructor;
14+
import lombok.Builder;
15+
import lombok.experimental.SuperBuilder;
16+
import lombok.extern.slf4j.Slf4j;
1817

1918
/**
2019
* The default {@link Persistor} for {@link TransactionOutbox}.
@@ -37,9 +36,9 @@ public class DefaultPersistor implements Persistor, Validatable {
3736

3837
/**
3938
* @param writeLockTimeoutSeconds How many seconds to wait before timing out on obtaining a write
40-
* lock. There's no point making this long; it's always better to just back off as quickly as
41-
* possible and try another record. Generally these lock timeouts only kick in if {@link
42-
* Dialect#isSupportsSkipLock()} is false.
39+
* lock. There's no point making this long; it's always better to just back off as quickly as
40+
* possible and try another record. Generally these lock timeouts only kick in if {@link
41+
* Dialect#isSupportsSkipLock()} is false.
4342
*/
4443
@SuppressWarnings("JavaDoc")
4544
@Builder.Default
@@ -60,19 +59,19 @@ public class DefaultPersistor implements Persistor, Validatable {
6059

6160
/**
6261
* @param migrate Set to false to disable automatic database migrations. This may be preferred if
63-
* the default migration behaviour interferes with your existing toolset, and you prefer to
64-
* manage the migrations explicitly (e.g. using FlyWay or Liquibase), or you do not give the
65-
* application DDL permissions at runtime. You may use {@link #writeSchema(Writer)} to access
66-
* the migrations.
62+
* the default migration behaviour interferes with your existing toolset, and you prefer to
63+
* manage the migrations explicitly (e.g. using FlyWay or Liquibase), or you do not give the
64+
* application DDL permissions at runtime. You may use {@link #writeSchema(Writer)} to access
65+
* the migrations.
6766
*/
6867
@SuppressWarnings("JavaDoc")
6968
@Builder.Default
7069
private final boolean migrate = true;
7170

7271
/**
7372
* @param serializer The serializer to use for {@link Invocation}s. See {@link
74-
* InvocationSerializer} for more information. Defaults to {@link
75-
* InvocationSerializer#createDefaultJsonSerializer()} with no custom serializable classes.
73+
* InvocationSerializer} for more information. Defaults to {@link
74+
* InvocationSerializer#createDefaultJsonSerializer()} with no custom serializable classes.
7675
*/
7776
@SuppressWarnings("JavaDoc")
7877
@Builder.Default
@@ -151,9 +150,9 @@ private void setupInsert(
151150
@Override
152151
public void delete(Transaction tx, TransactionOutboxEntry entry) throws Exception {
153152
try (PreparedStatement stmt =
154-
// language=MySQL
155-
tx.connection()
156-
.prepareStatement("DELETE FROM " + tableName + " WHERE id = ? and version = ?")) {
153+
// language=MySQL
154+
tx.connection()
155+
.prepareStatement("DELETE FROM " + tableName + " WHERE id = ? and version = ?")) {
157156
stmt.setString(1, entry.getId());
158157
stmt.setInt(2, entry.getVersion());
159158
if (stmt.executeUpdate() != 1) {
@@ -166,14 +165,14 @@ public void delete(Transaction tx, TransactionOutboxEntry entry) throws Exceptio
166165
@Override
167166
public void update(Transaction tx, TransactionOutboxEntry entry) throws Exception {
168167
try (PreparedStatement stmt =
169-
tx.connection()
170-
.prepareStatement(
171-
// language=MySQL
172-
"UPDATE "
173-
+ tableName
174-
+ " "
175-
+ "SET lastAttemptTime = ?, nextAttemptTime = ?, attempts = ?, blocked = ?, processed = ?, version = ? "
176-
+ "WHERE id = ? and version = ?")) {
168+
tx.connection()
169+
.prepareStatement(
170+
// language=MySQL
171+
"UPDATE "
172+
+ tableName
173+
+ " "
174+
+ "SET lastAttemptTime = ?, nextAttemptTime = ?, attempts = ?, blocked = ?, processed = ?, version = ? "
175+
+ "WHERE id = ? and version = ?")) {
177176
stmt.setTimestamp(
178177
1,
179178
entry.getLastAttemptTime() == null ? null : Timestamp.from(entry.getLastAttemptTime()));
@@ -195,17 +194,17 @@ public void update(Transaction tx, TransactionOutboxEntry entry) throws Exceptio
195194
@Override
196195
public boolean lock(Transaction tx, TransactionOutboxEntry entry) throws Exception {
197196
try (PreparedStatement stmt =
198-
tx.connection()
199-
.prepareStatement(
200-
dialect.isSupportsSkipLock()
201-
// language=MySQL
202-
? "SELECT id, invocation FROM "
203-
+ tableName
204-
+ " WHERE id = ? AND version = ? FOR UPDATE SKIP LOCKED"
205-
// language=MySQL
206-
: "SELECT id, invocation FROM "
207-
+ tableName
208-
+ " WHERE id = ? AND version = ? FOR UPDATE")) {
197+
tx.connection()
198+
.prepareStatement(
199+
dialect.isSupportsSkipLock()
200+
// language=MySQL
201+
? "SELECT id, invocation FROM "
202+
+ tableName
203+
+ " WHERE id = ? AND version = ? FOR UPDATE SKIP LOCKED"
204+
// language=MySQL
205+
: "SELECT id, invocation FROM "
206+
+ tableName
207+
+ " WHERE id = ? AND version = ? FOR UPDATE")) {
209208
stmt.setString(1, entry.getId());
210209
stmt.setInt(2, entry.getVersion());
211210
stmt.setQueryTimeout(writeLockTimeoutSeconds);
@@ -254,19 +253,19 @@ public List<TransactionOutboxEntry> selectBatch(Transaction tx, int batchSize, I
254253
throws Exception {
255254
String forUpdate = dialect.isSupportsSkipLock() ? " FOR UPDATE SKIP LOCKED" : "";
256255
try (PreparedStatement stmt =
257-
tx.connection()
258-
.prepareStatement(
259-
// language=MySQL
260-
"SELECT "
261-
+ ALL_FIELDS
262-
+ " FROM "
263-
+ tableName
264-
+ " WHERE nextAttemptTime < ? AND blocked = "
265-
+ dialect.booleanValue(false)
266-
+ " AND processed = "
267-
+ dialect.booleanValue(false)
268-
+ dialect.getLimitCriteria()
269-
+ forUpdate)) {
256+
tx.connection()
257+
.prepareStatement(
258+
// language=MySQL
259+
"SELECT "
260+
+ ALL_FIELDS
261+
+ " FROM "
262+
+ tableName
263+
+ " WHERE nextAttemptTime < ? AND blocked = "
264+
+ dialect.booleanValue(false)
265+
+ " AND processed = "
266+
+ dialect.booleanValue(false)
267+
+ dialect.getLimitCriteria()
268+
+ forUpdate)) {
270269
stmt.setTimestamp(1, Timestamp.from(now));
271270
stmt.setInt(2, batchSize);
272271
return gatherResults(batchSize, stmt);
@@ -277,8 +276,8 @@ public List<TransactionOutboxEntry> selectBatch(Transaction tx, int batchSize, I
277276
public int deleteProcessedAndExpired(Transaction tx, int batchSize, Instant now)
278277
throws Exception {
279278
try (PreparedStatement stmt =
280-
tx.connection()
281-
.prepareStatement(dialect.getDeleteExpired().replace("{{table}}", tableName))) {
279+
tx.connection()
280+
.prepareStatement(dialect.getDeleteExpired().replace("{{table}}", tableName))) {
282281
stmt.setTimestamp(1, Timestamp.from(now));
283282
stmt.setInt(2, batchSize);
284283
return stmt.executeUpdate();
@@ -330,16 +329,16 @@ public void clear(Transaction tx) throws SQLException {
330329
@Override
331330
public boolean checkConnection(Transaction tx) throws SQLException {
332331
try (Statement stmt = tx.connection().createStatement();
333-
ResultSet rs = stmt.executeQuery(dialect.getCheckSql())) {
332+
ResultSet rs = stmt.executeQuery(dialect.getCheckSql())) {
334333
return rs.next() && (rs.getInt(1) == 1);
335334
}
336335
}
337336

338337
@Override
339338
public Optional<TransactionOutboxEntry> load(Transaction tx, String entryId) throws Exception {
340339
try (PreparedStatement stmt =
341-
tx.connection()
342-
.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableName + " WHERE id = ?")) {
340+
tx.connection()
341+
.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableName + " WHERE id = ?")) {
343342
stmt.setString(1, entryId);
344343
List<TransactionOutboxEntry> results = gatherResults(1, stmt);
345344
if (results.isEmpty()) {

transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/StubPersistor.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package com.gruelbox.transactionoutbox;
22

3-
import lombok.Builder;
4-
53
import java.time.Instant;
64
import java.util.List;
75
import java.util.Optional;
6+
import lombok.Builder;
87

9-
/**
10-
* Stub implementation of {@link Persistor}.
11-
*/
8+
/** Stub implementation of {@link Persistor}. */
129
@Builder
1310
public class StubPersistor implements Persistor {
1411

15-
StubPersistor() {
16-
}
12+
StubPersistor() {}
1713

1814
@Override
1915
public void migrate(TransactionManager transactionManager) {
@@ -56,8 +52,7 @@ public int deleteProcessedAndExpired(Transaction tx, int batchSize, Instant now)
5652
}
5753

5854
@Override
59-
public void clear(Transaction tx) {
60-
}
55+
public void clear(Transaction tx) {}
6156

6257
@Override
6358
public Optional<TransactionOutboxEntry> load(Transaction tx, String entryId) {

0 commit comments

Comments
 (0)