Search in sources :

Example 1 with LocalTransactionInfo

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());
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) URL(java.net.URL)

Example 2 with LocalTransactionInfo

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);
}
Also used : LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 3 with LocalTransactionInfo

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);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Example 4 with LocalTransactionInfo

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);
}
Also used : WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) ResourceInfo(org.ballerinalang.util.codegen.ResourceInfo) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo)

Example 5 with LocalTransactionInfo

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;
}
Also used : BallerinaTransactionContext(org.ballerinalang.util.transactions.BallerinaTransactionContext) XAResource(javax.transaction.xa.XAResource) LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo) SQLTransactionContext(org.ballerinalang.nativeimpl.actions.data.sql.SQLTransactionContext) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) BString(org.ballerinalang.model.values.BString) XAConnection(javax.sql.XAConnection)

Aggregations

LocalTransactionInfo (org.ballerinalang.util.transactions.LocalTransactionInfo)7 BString (org.ballerinalang.model.values.BString)2 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 XAConnection (javax.sql.XAConnection)1 XAResource (javax.transaction.xa.XAResource)1 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 BFunctionPointer (org.ballerinalang.model.values.BFunctionPointer)1 BStruct (org.ballerinalang.model.values.BStruct)1 BValue (org.ballerinalang.model.values.BValue)1 SQLTransactionContext (org.ballerinalang.nativeimpl.actions.data.sql.SQLTransactionContext)1 ResourceInfo (org.ballerinalang.util.codegen.ResourceInfo)1 FunctionRefCPEntry (org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry)1 BallerinaTransactionContext (org.ballerinalang.util.transactions.BallerinaTransactionContext)1