This Ballerina project is designed to test and debug XA transaction recovery by simulating scenarios like crashing before notifying commit/rollback and database failures.
main.bal
- Main Ballerina file with the XA transaction and logic to simulate crashes.control_sql/control_mysql_service.bal
- Ballerina script with endpoints to stop and start the SQL service.
- Make sure you have Ballerina 2201.8 or newer and MySQL 8.1 or newer installed.
- Set up the two MySQL databases on ports 3306 and 3308. (create necessary databases)
CREATE DATABASE m1;
CREATE TABLE m1.test1 (
id INT NOT NULL AUTO_INCREMENT,
hello VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
- Add the required configuration values to
Config.toml
file.
hostname = ""
username = ""
password1 = ""
password2 = ""
-
Open a terminal and navigate to the project directory.
-
Run the Ballerina program:
bal run
-
Open another terminal and run the Ballerina script for stopping and starting the SQL service For Windows only. This Ballerina script should run with admin privilages.:
bal run control_mysql_service.bal
Endpoint | Method | Parameters | Returns |
---|---|---|---|
/updateToChipiChipi |
GET |
- crashIt (boolean): Simulate a crash before committing. |
200/500 |
- stopSql (boolean): Stop the SQL service during the transaction. |
|||
- rollIt (boolean): Rollback the transaction based on conditions. |
|||
/updateToDubiDubi |
GET |
- crashIt (boolean): Simulate a crash before committing. |
200/500 |
- stopSql (boolean): Stop the SQL service during the transaction. |
|||
- rollIt (boolean): Rollback the transaction based on conditions. |
Endpoint | Method | Returns |
---|---|---|
/getCrash |
GET |
Boolean indicating whether a crash is scheduled. |
/stopSql |
GET |
Boolean indicating whether the SQL service was stopped. |
To simulate a crash, the program triggers a crash just before committing a transaction. This is achieved by dividing by zero in a Java function (divideByZero
) called through Ballerina's external function capability.
The SQL service can be stopped using a separate Ballerina script (control_mysql_service.bal). Running the /stopSql endpoint will stop the SQL service, and running the /startSql endpoint will start it again.
- Access the endpoints
/updateToChipiChipi
and/updateToDubiDubi
to initiate transactions with different parameters. - View the console output for logs on transaction progress and status.