| 1 | /** |
| 2 | * Copyright (C) 2006 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.transaction.impl; |
| 20 | |
| 21 | import java.util.*; |
| 22 | import java.sql.Connection; |
| 23 | import java.sql.SQLException; |
| 24 | import org.apache.log4j.Logger; |
| 25 | import hu.netmind.beankeeper.common.StoreException; |
| 26 | import hu.netmind.beankeeper.transaction.*; |
| 27 | import hu.netmind.beankeeper.db.Database; |
| 28 | |
| 29 | /** |
| 30 | * This class supplies the services with lightweight transactions. |
| 31 | * @author Brautigam Robert |
| 32 | * @version Revision: $Revision$ |
| 33 | */ |
| 34 | public class InternalTransactionTrackerImpl implements InternalTransactionTracker |
| 35 | { |
| 36 | private static Logger logger = Logger.getLogger(InternalTransactionTrackerImpl.class); |
| 37 | |
| 38 | private Database database = null; // Injected |
| 39 | |
| 40 | public void init(Map parameters) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | public void release() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get a lightweight transaction. |
| 50 | */ |
| 51 | public Transaction getTransaction() |
| 52 | { |
| 53 | return new TransactionImpl(database.getConnectionSource().getConnection()); |
| 54 | } |
| 55 | |
| 56 | public class TransactionImpl extends HashMap implements Transaction |
| 57 | { |
| 58 | private Connection connection = null; |
| 59 | private boolean rollbackOnly = false; |
| 60 | private TransactionStatistics stats = new TransactionStatistics(); |
| 61 | private int depth = 0; |
| 62 | |
| 63 | public TransactionImpl(Connection connection) |
| 64 | { |
| 65 | this.connection=connection; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Begin the transaction, but for safety don't support embedding transactions. |
| 70 | */ |
| 71 | public void begin() |
| 72 | { |
| 73 | if ( depth != 0 ) |
| 74 | throw new StoreException("direct transaction does not support embedded blocks, transaction already begun"); |
| 75 | depth++; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Commit a transaction. |
| 80 | */ |
| 81 | public void commit() |
| 82 | { |
| 83 | try |
| 84 | { |
| 85 | if ( isRollbackOnly() ) |
| 86 | connection.rollback(); |
| 87 | else |
| 88 | connection.commit(); |
| 89 | } catch ( SQLException e ) { |
| 90 | throw new StoreException("exception while commit",e); |
| 91 | } finally { |
| 92 | database.getConnectionSource().releaseConnection(connection); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Rollback this transaction. |
| 98 | */ |
| 99 | public void rollback() |
| 100 | { |
| 101 | try |
| 102 | { |
| 103 | connection.rollback(); |
| 104 | } catch ( SQLException e ) { |
| 105 | throw new StoreException("exception while rollback",e); |
| 106 | } finally { |
| 107 | database.getConnectionSource().releaseConnection(connection); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Mark this transaction as rollback only. |
| 113 | */ |
| 114 | public void markRollbackOnly() |
| 115 | { |
| 116 | rollbackOnly = true; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns whether this transaction was marked "rollback only". |
| 121 | */ |
| 122 | public boolean isRollbackOnly() |
| 123 | { |
| 124 | return rollbackOnly; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * No used. |
| 129 | * @return Always null. |
| 130 | */ |
| 131 | public Long getSerial() |
| 132 | { |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Not used. |
| 138 | * @return Always null. |
| 139 | */ |
| 140 | public Long getEndSerial() |
| 141 | { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get statistics from this transaction. |
| 147 | */ |
| 148 | public TransactionStatistics getStats() |
| 149 | { |
| 150 | return stats; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get the connection to the database from this transaction. |
| 155 | */ |
| 156 | public Connection getConnection() |
| 157 | { |
| 158 | return connection; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | } |
| 164 | |
| 165 | |