[framework] TxCore → common-framework (ACM-FW-001)
This commit is contained in:
parent
16f1c5beeb
commit
b73958739e
1 changed files with 74 additions and 0 deletions
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.klaro.acquirecore.framework;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template for executing transactional operations.
|
||||||
|
* Migrated from txcore.c tx_begin/tx_commit functions.
|
||||||
|
* Provides @Transactional equivalent functionality.
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class TxTemplate {
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public <T> TxResult<T> execute(TxServiceRegistry.TxOperation<T> operation) {
|
||||||
|
TxContext context = new TxContext();
|
||||||
|
context.setStatus(TxContext.TransactionStatus.ACTIVE);
|
||||||
|
try {
|
||||||
|
T result = operation.execute(context);
|
||||||
|
context.setStatus(TxContext.TransactionStatus.COMMITTED);
|
||||||
|
context.setCommittedAt(Instant.now());
|
||||||
|
return TxResult.success(result, context);
|
||||||
|
} catch (Exception e) {
|
||||||
|
context.setStatus(TxContext.TransactionStatus.ROLLED_BACK);
|
||||||
|
context.setErrorMessage(e.getMessage());
|
||||||
|
throw new TxExecutionException("Transaction failed", e, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public <T> TxResult<T> execute(String serviceName, TxServiceRegistry.TxOperation<T> operation) {
|
||||||
|
TxContext context = new TxContext(serviceName);
|
||||||
|
context.setStatus(TxContext.TransactionStatus.ACTIVE);
|
||||||
|
try {
|
||||||
|
T result = operation.execute(context);
|
||||||
|
context.setStatus(TxContext.TransactionStatus.COMMITTED);
|
||||||
|
context.setCommittedAt(Instant.now());
|
||||||
|
return TxResult.success(result, context);
|
||||||
|
} catch (Exception e) {
|
||||||
|
context.setStatus(TxContext.TransactionStatus.ROLLED_BACK);
|
||||||
|
context.setErrorMessage(e.getMessage());
|
||||||
|
throw new TxExecutionException("Transaction failed for service: " + serviceName, e, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TxContext txBegin() {
|
||||||
|
return new TxContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TxContext txBegin(String serviceName) {
|
||||||
|
return new TxContext(serviceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public TxResult<Void> txCommit(TxContext context) {
|
||||||
|
if (context == null) {
|
||||||
|
return TxResult.failure("INVALID_CONTEXT", "Transaction context is null");
|
||||||
|
}
|
||||||
|
context.setStatus(TxContext.TransactionStatus.COMMITTED);
|
||||||
|
context.setCommittedAt(Instant.now());
|
||||||
|
return TxResult.success(null, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public TxResult<Void> txRollback(TxContext context, String reason) {
|
||||||
|
if (context == null) {
|
||||||
|
return TxResult.failure("INVALID_CONTEXT", "Transaction context is null");
|
||||||
|
}
|
||||||
|
context.setStatus(TxContext.TransactionStatus.ROLLED_BACK);
|
||||||
|
context.setErrorMessage(reason);
|
||||||
|
return TxResult.success(null, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in a new issue