[framework] TxCore → common-framework (ACM-FW-001)
This commit is contained in:
parent
78505a81ea
commit
3491780f0a
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in a new issue