[framework] TxCore → common-framework (ACM-FW-001)

This commit is contained in:
forge-bot 2026-07-18 11:11:57 +00:00
parent 78505a81ea
commit 3491780f0a

View file

@ -0,0 +1,74 @@
package com.klaro.acquirecore.framework;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* Transaction template replacing C tx_begin/tx_commit pattern.
* Provides @Transactional-backed execution with context management.
* Mirrors C tx_begin(), tx_commit(), tx_rollback() lifecycle.
*/
@Component
public class TxTemplate {
/**
* Execute a transactional operation with automatic commit on success,
* rollback on exception. Mirrors C tx_begin + tx_commit pattern.
*
* @param serviceName the name of the registered service
* @param input the input data
* @param <T> the result type
* @return TxResult containing the outcome
*/
@Transactional(rollbackFor = Exception.class)
public <T> TxResult<T> execute(String serviceName, Object input) {
return executeWithContext(serviceName, new TxContext(serviceName), input);
}
/**
* Execute with an existing transaction context.
*
* @param serviceName the name of the registered service
* @param ctx the existing transaction context
* @param input the input data
* @param <T> the result type
* @return TxResult containing the outcome
*/
@Transactional(rollbackFor = Exception.class)
public <T> TxResult<T> executeWithContext(String serviceName, TxContext ctx, Object input) {
ctx.setStatus(TxStatus.ACTIVE);
try {
Object result = doExecute(serviceName, ctx, input);
ctx.markCommitted();
return TxResult.ok(result);
} catch (Exception e) {
ctx.markRolledBack();
return TxResult.error("TX_FAILED", e.getMessage());
}
}
/**
* Execute without transaction boundary (read-only operations).
*
* @param serviceName the name of the registered service
* @param input the input data
* @param <T> the result type
* @return TxResult containing the outcome
*/
@Transactional(readOnly = true)
public <T> TxResult<T> query(String serviceName, Object input) {
try {
TxContext ctx = new TxContext(serviceName);
Object result = doExecute(serviceName, ctx, input);
return TxResult.ok(result);
} catch (Exception e) {
return TxResult.error("QUERY_FAILED", e.getMessage());
}
}
private Object doExecute(String serviceName, TxContext ctx, Object input) {
throw new UnsupportedOperationException(
"Direct execution not supported. Use TxServiceRegistry.dispatch() or inject TxService directly."
);
}
}