use of org.ballerinalang.util.transactions.LocalTransactionInfo in project ballerina by ballerina-lang.
the class AbstractHTTPAction method prepareOutboundRequest.
protected void prepareOutboundRequest(Context context, BStruct connector, String path, HTTPCarbonMessage outboundRequest) {
validateParams(connector);
if (context.isInTransaction()) {
LocalTransactionInfo localTransactionInfo = context.getLocalTransactionInfo();
outboundRequest.setHeader(HttpConstants.HEADER_X_XID, localTransactionInfo.getGlobalTransactionId());
outboundRequest.setHeader(HttpConstants.HEADER_X_REGISTER_AT_URL, localTransactionInfo.getURL());
}
try {
String uri = connector.getStringField(0) + path;
URL url = new URL(uri);
int port = getOutboundReqPort(url);
String host = url.getHost();
setOutboundReqProperties(outboundRequest, url, port, host);
setOutboundReqHeaders(outboundRequest, port, host);
} catch (MalformedURLException e) {
throw new BallerinaException("Malformed url specified. " + e.getMessage());
} catch (Throwable t) {
throw new BallerinaException("Failed to prepare request. " + t.getMessage());
}
}
use of org.ballerinalang.util.transactions.LocalTransactionInfo in project ballerina by ballerina-lang.
the class SQLDatasourceUtils method handleErrorOnTransaction.
public static void handleErrorOnTransaction(Context context) {
LocalTransactionInfo localTransactionInfo = context.getLocalTransactionInfo();
if (localTransactionInfo == null) {
return;
}
SQLDatasourceUtils.notifyTxMarkForAbort(context, localTransactionInfo);
throw new BallerinaException(BLangVMErrors.TRANSACTION_ERROR);
}
use of org.ballerinalang.util.transactions.LocalTransactionInfo in project ballerina by ballerina-lang.
the class CPU method beginTransaction.
private static void beginTransaction(WorkerExecutionContext ctx, int transactionBlockId, int retryCountRegIndex, int committedFuncIndex, int abortedFuncIndex) {
// If global tx enabled, it is managed via transaction coordinator. Otherwise it is managed locally without
// any interaction with the transaction coordinator.
boolean isGlobalTransactionEnabled = ctx.getGlobalTransactionEnabled();
// Transaction is attempted three times by default to improve resiliency
int retryCount = TransactionConstants.DEFAULT_RETRY_COUNT;
if (retryCountRegIndex != -1) {
retryCount = (int) ctx.workerLocal.longRegs[retryCountRegIndex];
if (retryCount < 0) {
ctx.setError(BLangVMErrors.createError(ctx, BLangExceptionHelper.getErrorMessage(RuntimeErrors.INVALID_RETRY_COUNT)));
handleError(ctx);
return;
}
}
// Register committed function handler if exists.
if (committedFuncIndex != -1) {
FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[committedFuncIndex];
BFunctionPointer fpCommitted = new BFunctionPointer(funcRefCPEntry);
TransactionResourceManager.getInstance().registerCommittedFunction(transactionBlockId, fpCommitted);
}
// Register aborted function handler if exists.
if (abortedFuncIndex != -1) {
FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[abortedFuncIndex];
BFunctionPointer fpAborted = new BFunctionPointer(funcRefCPEntry);
TransactionResourceManager.getInstance().registerAbortedFunction(transactionBlockId, fpAborted);
}
LocalTransactionInfo localTransactionInfo = ctx.getLocalTransactionInfo();
if (localTransactionInfo == null) {
String globalTransactionId;
String protocol = null;
String url = null;
if (isGlobalTransactionEnabled) {
BValue[] returns = TransactionUtils.notifyTransactionBegin(ctx, null, null, transactionBlockId, TransactionConstants.DEFAULT_COORDINATION_TYPE);
BStruct txDataStruct = (BStruct) returns[0];
globalTransactionId = txDataStruct.getStringField(1);
protocol = txDataStruct.getStringField(2);
url = txDataStruct.getStringField(3);
} else {
globalTransactionId = UUID.randomUUID().toString().replaceAll("-", "");
}
localTransactionInfo = new LocalTransactionInfo(globalTransactionId, url, protocol);
ctx.setLocalTransactionInfo(localTransactionInfo);
} else {
if (isGlobalTransactionEnabled) {
TransactionUtils.notifyTransactionBegin(ctx, localTransactionInfo.getGlobalTransactionId(), localTransactionInfo.getURL(), transactionBlockId, localTransactionInfo.getProtocol());
}
}
localTransactionInfo.beginTransactionBlock(transactionBlockId, retryCount);
}
use of org.ballerinalang.util.transactions.LocalTransactionInfo in project ballerina by ballerina-lang.
the class ResourceExecutor method execute.
/**
* This method will execute the resource, given required details.
* And it will use the callback to notify interested parties about the
* outcome of the execution.
*
* @param resource to be executed.
* @param responseCallback to notify.
* @param properties to be passed to context.
* @param tracer to be passed to context.
* @param bValues for parameters.
*/
public static void execute(Resource resource, CallableUnitCallback responseCallback, Map<String, Object> properties, Tracer tracer, BValue... bValues) throws BallerinaConnectorException {
if (resource == null || responseCallback == null) {
throw new BallerinaConnectorException("invalid arguments provided");
}
ResourceInfo resourceInfo = resource.getResourceInfo();
WorkerExecutionContext context = new WorkerExecutionContext(resourceInfo.getPackageInfo().getProgramFile());
if (properties != null) {
context.globalProps.putAll(properties);
if (properties.get(Constants.GLOBAL_TRANSACTION_ID) != null) {
context.setLocalTransactionInfo(new LocalTransactionInfo(properties.get(Constants.GLOBAL_TRANSACTION_ID).toString(), properties.get(Constants.TRANSACTION_URL).toString(), "2pc"));
}
}
BLangVMUtils.initServerConnectorTrace(context, resource, tracer);
BLangVMUtils.setServiceInfo(context, resourceInfo.getServiceInfo());
BLangFunctions.invokeCallable(resourceInfo, context, bValues, responseCallback);
}
use of org.ballerinalang.util.transactions.LocalTransactionInfo in project ballerina by ballerina-lang.
the class AbstractSQLAction method getDatabaseConnection.
private Connection getDatabaseConnection(Context context, SQLDatasource datasource, boolean isInTransaction) throws SQLException {
Connection conn;
if (!isInTransaction) {
conn = datasource.getSQLConnection();
return conn;
} else {
// since the action call is outside of the transaction block.
if (!context.getLocalTransactionInfo().hasTransactionBlock()) {
conn = datasource.getSQLConnection();
return conn;
}
}
String connectorId = datasource.getConnectorId();
boolean isXAConnection = datasource.isXAConnection();
LocalTransactionInfo localTransactionInfo = context.getLocalTransactionInfo();
String globalTxId = localTransactionInfo.getGlobalTransactionId();
int currentTxBlockId = localTransactionInfo.getCurrentTransactionBlockId();
BallerinaTransactionContext txContext = localTransactionInfo.getTransactionContext(connectorId);
if (txContext == null) {
if (isXAConnection) {
XAConnection xaConn = datasource.getXADataSource().getXAConnection();
XAResource xaResource = xaConn.getXAResource();
TransactionResourceManager.getInstance().beginXATransaction(globalTxId, currentTxBlockId, xaResource);
conn = xaConn.getConnection();
txContext = new SQLTransactionContext(conn, xaResource);
} else {
conn = datasource.getSQLConnection();
conn.setAutoCommit(false);
txContext = new SQLTransactionContext(conn);
}
localTransactionInfo.registerTransactionContext(connectorId, txContext);
TransactionResourceManager.getInstance().register(globalTxId, currentTxBlockId, txContext);
} else {
conn = ((SQLTransactionContext) txContext).getConnection();
}
return conn;
}
Aggregations