Paste Search Dynamic
Recent pastes
TransactionServlet
  1. @WebServlet(name = "TransactionServlet", value = "/TransactionServlet")
  2. public class TransactionServlet extends HttpServlet {
  3.     @override
  4.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ioexception {
  5.         //CALL THE DRIVER
  6.         try {
  7.             class.forName("oracle.jdbc.driver.OracleDriver");
  8.         } catch (classnotfoundexception e) {
  9.             e.printStackTrace();
  10.         }
  11.  
  12.         connection conn = null;
  13.         try {
  14.             //MAKE THE CONNECTION
  15.             conn = drivermanager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "test", "test");
  16.             //if we use 'transaction principle' or not
  17.             conn.setAutoCommit(false);
  18.             //get and set transaction isolation mod (dirty, not reproductible and ghost)
  19.             int transactionIsolationMod = connection.TRANSACTION_SERIALIZABLE;
  20.             conn.setTransactionIsolation(transactionIsolationMod);
  21.             //LIFO Array
  22.             Queue<Savepoint> queue = collections.asLifoQueue(new ArrayDeque<>());
  23.             //create a savepoint before any sql operation
  24.             savepoint save1 = conn.setSavepoint();
  25.             //save it to our LIFO Array (ie: array{save1, save2})
  26.             queue.add(save1);
  27.  
  28.             //Create 2 client object
  29.             Client client = new Client();
  30.             client.setName("Super");client.setSurName("Louis");
  31.             Client client2 = new Client();
  32.             client2.setName("Not Super");client2.setSurName("Still Louis");
  33.             Client client3 = new Client();
  34.             client3.setName("");client3.setSurName("");
  35.             //save them into a client array
  36.             List<Client> clientList = new ArrayList<>();
  37.             clientList.add(client);clientList.add(client2);clientList.add(client3);
  38.  
  39.             string sqlInsert = "insert into Person(name, surname) values (?, ?)";
  40.  
  41.             for(Client myClient : clientList) {
  42.                 boolean bool = true;
  43.                 try {
  44.                     preparedstatement ps = conn.prepareStatement(sqlInsert);
  45.                     ps.setString(1, myClient.getName());
  46.                     ps.setString(2, myClient.getSurname());
  47.  
  48.                     int count = ps.executeUpdate();
  49.                     system.out.println(myClient.getName()+" "+myClient.getSurname());
  50.                     if(myClient.getName().equals("") && myClient.getSurname().equals("")) {
  51.                         system.out.println("Error, name and surname are empties (rollback)");
  52.                         system.out.println(myClient.getName()+" "+myClient.getSurname()+" will not be saved to the BDD");
  53.                         conn.rollback(queue.remove());
  54.                         bool = false;
  55.                     }
  56.                     //if not failed the previous if statement then ...
  57.                     if(count > 0 && bool) {
  58.                         //make a save after each successful executeUpdate
  59.                         queue.add(conn.setSavepoint());
  60.                         system.out.println("save point success");
  61.                     }
  62.                 }catch(sqlexception e) {
  63.                     system.out.println("! Error !"+e.getErrorCode());
  64.                     //array{save1, save2} => array{save1, X} => array{X, X} (x mean delete)
  65.                     conn.rollback(queue.remove());
  66.                 }
  67.             }
  68.             system.out.println("MyClient list size: "+clientList.size());
  69.             //queueArray {before statement, first client, second client, third client}
  70.             //in the end: {before statement, first client, second client, X} (x mean delete)
  71.             system.out.println("Queue size (savepoint) "+queue.size());
  72.             //release the transaction and start a new one
  73.             conn.commit();
  74.             //[...]
  75.             conn.close();
  76.  
  77.             //FINAL RESULT
  78.             /*
  79.                 Super Louis
  80.                 save point success
  81.                 Not Super Still Louis
  82.                 save point success
  83.  
  84.                 Error, name and surname are empties (rollback)
  85.                   will not be saved to the BDD
  86.                 MyClient list size: 3
  87.                 Queue size (savepoint) 2
  88.              */
  89.  
  90.         } catch (sqlexception throwables) {
  91.             throwables.printStackTrace();
  92.         }
  93.     }
Parsed in 0.123 seconds