Search in sources :

Example 1 with BallerinaTransactionContext

use of org.ballerinalang.util.transactions.BallerinaTransactionContext 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

Connection (java.sql.Connection)1 XAConnection (javax.sql.XAConnection)1 XAResource (javax.transaction.xa.XAResource)1 BString (org.ballerinalang.model.values.BString)1 SQLTransactionContext (org.ballerinalang.nativeimpl.actions.data.sql.SQLTransactionContext)1 BallerinaTransactionContext (org.ballerinalang.util.transactions.BallerinaTransactionContext)1 LocalTransactionInfo (org.ballerinalang.util.transactions.LocalTransactionInfo)1