package TS;

import RDG.BoughtTicket;
import java.sql.SQLException;

/**
 *
 * @author Marcel
 */
public class SeatChooseTS {
    /**
     * reserve seat
     * isolation level: serializable
     * - reserve seat
     * @param ticket_id to which ticket
     * @param seat_id which seat
     * @throws SQLException
     * @throws Exception
     * @return Boolean if everything went right 
     */
    public static boolean transactionSeatChoose(Integer ticket_id, Integer seat_id) throws SQLException, Exception{
        RDG.DbContext.getConnection().setAutoCommit(false);
        
        RDG.DbContext.getConnection().setTransactionIsolation(java.sql.Connection.TRANSACTION_SERIALIZABLE);
        
        boolean podarilo = false;
        Exception t=null;
        for(int i=0;i<3&&!podarilo;i++){
            podarilo=true;
            try{
                BoughtTicket.reserveSeat(seat_id, ticket_id);
            }catch(Exception e){
                RDG.DbContext.getConnection().rollback();
                t=e;
                podarilo=false;
            }
        }
        if(podarilo){
            RDG.DbContext.getConnection().commit();
            RDG.DbContext.getConnection().setAutoCommit(true);
            return true;
        }
        System.err.println(t);
        RDG.DbContext.getConnection().rollback();
        RDG.DbContext.getConnection().setAutoCommit(true);
        return false;
    }
}
