Skip to content

Commit

Permalink
unmanagedConnection can not use savepoint. (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
HidekiSugimoto189 authored Aug 14, 2024
1 parent fb3798f commit fb18afd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jp.co.future.uroborosql.context.ExecutionContext;
import jp.co.future.uroborosql.event.AfterBeginTransactionEvent;
import jp.co.future.uroborosql.event.BeforeEndTransactionEvent;
import jp.co.future.uroborosql.exception.UroborosqlRuntimeException;
import jp.co.future.uroborosql.exception.UroborosqlSQLException;

/**
Expand Down Expand Up @@ -355,18 +356,18 @@ public void rollback() {
*/
@Override
public <R> R savepointScope(final Supplier<R> supplier) {
return currentTxContext(false).map(txCtx -> {
var savepointName = UUID.randomUUID().toString();
txCtx.setSavepoint(savepointName);
try {
return supplier.get();
} catch (Throwable th) {
txCtx.rollback(savepointName);
throw th;
} finally {
txCtx.releaseSavepoint(savepointName);
}
}).orElse(null);
var txCtx = currentTxContext(false)
.orElseThrow(() -> new UroborosqlRuntimeException("UnmanagedConnection cannot use savepoint."));
var savepointName = UUID.randomUUID().toString();
txCtx.setSavepoint(savepointName);
try {
return supplier.get();
} catch (Throwable th) {
txCtx.rollback(savepointName);
throw th;
} finally {
txCtx.releaseSavepoint(savepointName);
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/jp/co/future/uroborosql/tx/LocalTxManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,19 @@ void testSavepointScopeSupplier() {
}
}

@Test
void testSavepointScopeWithUnmanagedConnection() {
try (var agent = config.agent()) {
try {
agent.savepointScope(() -> {
throw new IllegalAccessError();
});
} catch (UroborosqlRuntimeException ex) {
assertThat(ex.getMessage(), is("UnmanagedConnection cannot use savepoint."));
}
}
}

@Test
void testagentEx01() {

Expand Down

0 comments on commit fb18afd

Please sign in to comment.