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;
}
Aggregations