1 3 package jodd.db; 4 5 import jodd.db.connection.ConnectionProvider; 6 import jodd.util.collection.Bag; 7 import jodd.util.collection.HashBag; 8 9 import java.sql.Connection ; 10 import java.sql.SQLException ; 11 import java.util.Iterator ; 12 13 23 public class DbSession { 24 25 28 public static DbTransactionMode DEFAULT_TX_MODE = new DbTransactionMode(); 29 30 32 protected ConnectionProvider connectionProvider; 33 protected Connection connection; 34 35 38 public DbSession(ConnectionProvider connectionProvider) { 39 this.connectionProvider = connectionProvider; 40 txActive = false; 41 txMode = DEFAULT_TX_MODE; 42 queries = new HashBag(); 43 } 44 45 46 52 public void closeSession() { 53 Iterator it = queries.iterator(); 54 while (it.hasNext()) { 55 ((DbQuery) it.next()).close(); 56 } 57 58 if (connection != null) { 59 if (txActive == true) { 60 throw new DbSqlException("Transaction was not closed before closing the session."); 61 } 62 connectionProvider.closeConnection(connection); 63 connection = null; 64 } 65 66 queries = null; 67 } 68 69 72 public boolean isSessionClosed() { 73 return queries == null; 74 } 75 76 77 79 83 protected Bag queries; 84 85 88 public int getTotalQueries() { 89 return queries.size(); 90 } 91 92 95 public int getTotalActiveQueries() { 96 int count = 0; 97 Iterator it = queries.iterator(); 98 while (it.hasNext()) { 99 if (((DbQuery) it.next()).isActive() == true) { 100 count++; 101 } 102 } 103 return count; 104 } 105 106 107 108 112 public Connection getConnection() { 113 return connection; 114 } 115 116 117 121 void attachQuery(DbQuery query) { 122 checkOpenSession(); 123 openConnectionForQuery(); 124 queries.add(query); 125 } 126 127 128 131 protected void openConnectionForQuery() { 132 if (connection == null) { 133 connection = connectionProvider.getConnection(); 134 txActive = false; try { 136 connection.setAutoCommit(true); 137 } catch (SQLException sex) { 138 throw new DbSqlException("Unable to open non-transactional connection.", sex); 139 } 140 } 141 } 142 143 145 protected boolean txActive; 146 147 protected DbTransactionMode txMode; 148 149 152 public boolean isTransactionActive() { 153 return txActive; 154 } 155 156 157 160 protected void openTx() { 161 if (connection == null) { 162 connection = connectionProvider.getConnection(); 163 } 164 txActive = true; 165 try { 166 connection.setAutoCommit(false); 167 if (txMode.getIsolation() != DbTransactionMode.ISOLATION_DEFAULT) { 168 connection.setTransactionIsolation(txMode.getIsolation()); 169 } 170 connection.setReadOnly(txMode.isReadOnly()); 171 } catch (SQLException sex) { 172 throw new DbSqlException("Unable to open and prepare transaction.", sex); 173 } 174 } 175 176 179 protected void closeTx() { 180 txActive = false; 181 try { 182 connection.setAutoCommit(true); 183 } catch (SQLException sex) { 184 throw new DbSqlException("Unable to prepare connection", sex); 185 } 186 } 187 188 189 192 public void beginTransaction(DbTransactionMode mode) { 193 checkClosedTx(); 194 this.txMode = mode; 195 openTx(); 196 } 197 198 201 public void beginTransaction() { 202 beginTransaction(DEFAULT_TX_MODE); 203 } 204 205 209 public void commitTransaction() { 210 checkActiveTx(); 211 try { 212 connection.commit(); 213 } catch (SQLException sex) { 214 throw new DbSqlException("Unable to commit transaction regulary.", sex); 215 } finally { 216 closeTx(); 217 } 218 } 219 220 223 public void rollbackTransaction() { 224 checkActiveTx(); 225 try { 226 connection.rollback(); 227 } catch (SQLException sex) { 228 throw new DbSqlException("Unable to rollback transaction regulary.", sex); 229 } finally { 230 closeTx(); 231 } 232 } 233 234 235 237 238 protected void checkOpenSession() { 239 if (queries == null) { 240 throw new DbSqlException("Session is already closed."); 241 } 242 } 243 244 protected void checkClosedTx() { 245 checkOpenSession(); 246 if (txActive == true) { 247 throw new DbSqlException("Transaction already started for this session."); 248 } 249 } 250 251 protected void checkActiveTx() { 252 checkOpenSession(); 253 if (txActive == false) { 254 throw new DbSqlException("Transaction not availiable for this session."); 255 } 256 } 257 } | Popular Tags |