#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance();
std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "root", "password"));
con->setSchema("test_db");
con->setAutoCommit(false);
std::unique_ptr<sql::Statement> stmt(con->createStatement());
stmt->execute("SAVEPOINT sp1");
stmt->execute("INSERT INTO test_table (name, age) VALUES ('Eve', 45)");
stmt->execute("SAVEPOINT sp2");
stmt->execute("INSERT INTO test_table (name, age) VALUES ('Frank', 50)");
throw std::runtime_error("Errore simulato nella transazione annidata");
stmt->execute("RELEASE SAVEPOINT sp2");
} catch (const std::exception& e) {
std::cerr << "Errore interno: " << e.what() << std::endl;
stmt->execute("ROLLBACK TO SAVEPOINT sp2");
std::cout << "Transazione completata con successo" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Errore: " << e.what() << std::endl;
std::cout << "Transazione annullata" << std::endl;
} catch (sql::SQLException& e) {
std::cerr << "Errore SQL durante il rollback
: " << e.what() << std::endl;