22
22
import java .time .Duration ;
23
23
import java .util .Set ;
24
24
import java .util .concurrent .atomic .AtomicLong ;
25
+ import java .util .function .Function ;
25
26
import java .util .function .Supplier ;
26
27
import java .util .regex .Pattern ;
27
28
@@ -181,23 +182,25 @@ public ScanBuilder scan() {
181
182
182
183
@ Override
183
184
public void tx (Runnable runnable ) {
184
- tx (() -> {
185
- runnable .run ();
186
- return null ;
187
- });
185
+ txC (__ -> runnable .run ());
188
186
}
189
187
190
188
@ Override
191
189
public <T > T tx (Supplier <T > supplier ) {
190
+ return tx (__ -> supplier .get ());
191
+ }
192
+
193
+ @ Override
194
+ public <T > T tx (Function <Tx , T > func ) {
192
195
if (name == null ) {
193
- return withGeneratedNameAndLine ().tx (supplier );
196
+ return withGeneratedNameAndLine ().tx (func );
194
197
}
195
198
196
199
checkSeparatePolicy (separatePolicy , name );
197
- return txImpl (supplier );
200
+ return txImpl (func );
198
201
}
199
202
200
- private <T > T txImpl (Supplier < T > supplier ) {
203
+ private <T > T txImpl (Function < Tx , T > func ) {
201
204
RetryableException lastRetryableException = null ;
202
205
TxImpl lastTx = null ;
203
206
try (Timer ignored = totalDuration .labels (name ).startTimer ()) {
@@ -207,7 +210,7 @@ private <T> T txImpl(Supplier<T> supplier) {
207
210
T result ;
208
211
try (var ignored1 = attemptDuration .labels (name ).startTimer ()) {
209
212
lastTx = new TxImpl (name , repository .startTransaction (options ), options );
210
- result = runAttempt (supplier , lastTx );
213
+ result = runAttempt (lastTx , func );
211
214
}
212
215
213
216
if (options .isDryRun ()) {
@@ -258,11 +261,11 @@ private String getExceptionNameForMetric(RetryableException e) {
258
261
return Strings .removeSuffix (e .getClass ().getSimpleName (), "Exception" );
259
262
}
260
263
261
- private <T > T runAttempt (Supplier < T > supplier , TxImpl tx ) {
264
+ private <T > T runAttempt (TxImpl tx , Function < Tx , T > func ) {
262
265
try (var ignored2 = MDC .putCloseable ("tx" , formatTx ());
263
266
var ignored3 = MDC .putCloseable ("tx-id" , formatTxId ());
264
267
var ignored4 = MDC .putCloseable ("tx-name" , formatTxName (false ))) {
265
- return tx .run (supplier );
268
+ return tx .run (func );
266
269
}
267
270
}
268
271
0 commit comments