1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package net.jot.persistance; import net.jot.exceptions.JOTTransactionCompletedException; import net.jot.db.JOTDBManager; import net.jot.db.JOTTaggedConnection; import net.jot.logger.JOTLogger; /** * This is used to run multiple DB queries in a transaction * * Note that once either commit() or roollback() have been called, this transaction is considered completet * and can't be used anymore (will throw JOTTransactionCompltedException) * @author thibautc */ public final class JOTTransaction { private JOTTaggedConnection con; private boolean completed = false; // do not use private JOTTransaction() { con = null; } public JOTTransaction(String storageName) throws Exception { con = JOTDBManager.getInstance().getConnection(storageName); con.getConnection().setAutoCommit(false); } public JOTTaggedConnection getConnection() { return con; } public void rollBack() throws Exception { if (completed) { throw new JOTTransactionCompletedException(); } con.getConnection().rollback(); // if no exception, this transaction is completed terminate(); } /*public void rollBack(Savepoint savept) throws Exception { if (completed) { throw new JOTTransactionCompletedException(); } con.getConnection().rollback(savept); // if no exception, this tranmsaction is completed terminate(); }*/ /*public Savepoint setSavepoint() throws Exception { if (completed) { throw new JOTTransactionCompletedException(); } return con.getConnection().setSavepoint(); } public Savepoint setSavepoint(String savept) throws Exception { if (completed) { throw new JOTTransactionCompletedException(); } return con.getConnection().setSavepoint(savept); }*/ public void setTransactionIsolation(int isolation) throws Exception { if (completed) { throw new JOTTransactionCompletedException(); } con.getConnection().setTransactionIsolation(isolation); } public void commit() throws Exception { if (completed) { throw new JOTTransactionCompletedException(); } con.getConnection().commit(); // if no exception, this tranmsaction is completed terminate(); } /** * This will terminate the transaction and free resources * In theory you should not need to call this, since commit() and rollback() will call it. * However if you want to ensure release of the connection you can call it to be safe. * @throws java.lang.Exception */ public void terminate() { if (!completed) { completed = true; try { con.getConnection().setAutoCommit(true); } catch (Exception e) { JOTLogger.logException(this, "Failed to set the connection back to autocommit", e); } try { JOTDBManager.getInstance().releaseConnection(con); } catch (Exception e) { JOTLogger.logException(this, "Failed to release the connection to the pool", e); } con = null; } } public void finalyze() throws Throwable { // if the transaction wasn't completed correctly, we will try to cleanup here // and try to release the connection to the pool. terminate(); super.finalize(); } }