2 * Copyright 2010 Sun Microsystems, Inc.
3 * All rights reserved. Use is subject to license terms.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.mysql.clusterj.bindings;
21 import com.mysql.cluster.ndbj.NdbApiException;
22 import com.mysql.cluster.ndbj.NdbOperation.AbortOption;
23 import com.mysql.cluster.ndbj.NdbOperation;
24 import com.mysql.cluster.ndbj.NdbScanOperation;
25 import com.mysql.cluster.ndbj.NdbTransaction;
26 import com.mysql.clusterj.ClusterJDatastoreException;
27 import com.mysql.clusterj.core.store.ClusterTransaction;
28 import com.mysql.clusterj.core.store.Index;
29 import com.mysql.clusterj.core.store.IndexOperation;
30 import com.mysql.clusterj.core.store.IndexScanOperation;
31 import com.mysql.clusterj.core.store.Operation;
32 import com.mysql.clusterj.core.store.ScanOperation;
33 import com.mysql.clusterj.core.store.Table;
34 import com.mysql.clusterj.core.util.I18NHelper;
35 import com.mysql.clusterj.core.util.Logger;
36 import com.mysql.clusterj.core.util.LoggerFactoryService;
37 import java.util.ArrayList;
38 import java.util.List;
43 class ClusterTransactionImpl implements ClusterTransaction {
45 /** My message translator */
46 static final I18NHelper local = I18NHelper
47 .getInstance(ClusterTransactionImpl.class);
50 static final Logger logger = LoggerFactoryService.getFactory()
51 .getInstance(ClusterTransactionImpl.class);
53 protected NdbTransaction ndbTransaction;
54 private List<Runnable> postExecuteCallbacks = new ArrayList<Runnable>();
56 public ClusterTransactionImpl(NdbTransaction ndbTransaction) {
57 this.ndbTransaction = ndbTransaction;
61 ndbTransaction.close();
64 public void executeCommit() {
65 handlePendingPostExecuteCallbacks();
67 ndbTransaction.executeCommit();
68 } catch (NdbApiException ndbApiException) {
69 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
74 public void executeCommit(boolean abort, boolean force) {
75 handlePendingPostExecuteCallbacks();
76 AbortOption abortOption = abort?AbortOption.AbortOnError:AbortOption.AO_IgnoreError;
78 ndbTransaction.execute(NdbTransaction.ExecType.Commit,
80 } catch (NdbApiException ndbApiException) {
81 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
86 public void executeNoCommit(boolean abort, boolean force) {
87 AbortOption abortOption = abort?AbortOption.AbortOnError:AbortOption.AO_IgnoreError;
89 ndbTransaction.execute(NdbTransaction.ExecType.NoCommit,
91 performPostExecuteCallbacks();
92 } catch (NdbApiException ndbApiException) {
93 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
98 public void executeNoCommit() {
100 ndbTransaction.executeNoCommit();
101 performPostExecuteCallbacks();
102 } catch (NdbApiException ndbApiException) {
103 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
108 public void executeRollback() {
110 clearPostExecuteCallbacks();
111 ndbTransaction.executeRollback();
112 } catch (NdbApiException ndbApiException) {
113 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
118 public Operation getDeleteOperation(Table storeTable) {
120 return new OperationImpl(ndbTransaction.getDeleteOperation(storeTable.getName()), this);
121 } catch (NdbApiException ndbApiException) {
122 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
127 public Operation getInsertOperation(Table storeTable) {
129 return new OperationImpl(ndbTransaction.getInsertOperation(storeTable.getName()), this);
130 } catch (NdbApiException ndbApiException) {
131 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
136 public IndexScanOperation getSelectIndexScanOperation(Index storeIndex, Table storeTable) {
138 return new IndexScanOperationImpl(
139 ndbTransaction.getSelectIndexScanOperation(storeIndex.getName(), storeTable.getName()), this);
140 } catch (NdbApiException ndbApiException) {
141 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
146 public Operation getSelectOperation(Table storeTable) {
148 return new OperationImpl(ndbTransaction.getSelectOperation(storeTable.getName()), this);
149 } catch (NdbApiException ndbApiException) {
150 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
155 public ScanOperation getSelectScanOperation(Table storeTable) {
157 return new ScanOperationImpl(
158 ndbTransaction.getSelectScanOperation(storeTable.getName()), this);
159 } catch (NdbApiException ndbApiException) {
160 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
165 public ScanOperation getSelectScanOperationLockModeExclusiveScanFlagKeyInfo(Table storeTable) {
167 return new ScanOperationImpl(
168 ndbTransaction.getSelectScanOperation(storeTable.getName(),
169 NdbOperation.LockMode.LM_Exclusive,
170 NdbScanOperation.ScanFlag.KEY_INFO, 0,0), this);
171 } catch (NdbApiException ndbApiException) {
172 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
177 public IndexOperation getSelectUniqueOperation(Index storeIndex, Table storeTable) {
179 return new IndexOperationImpl(
180 ndbTransaction.getSelectUniqueOperation(storeIndex.getName(), storeTable.getName()), this);
181 } catch (NdbApiException ndbApiException) {
182 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
187 public Operation getUpdateOperation(Table storeTable) {
189 return new OperationImpl(ndbTransaction.getUpdateOperation(storeTable.getName()), this);
190 } catch (NdbApiException ndbApiException) {
191 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
196 public Operation getWriteOperation(Table storeTable) {
198 return new OperationImpl(ndbTransaction.getWriteOperation(storeTable.getName()), this);
199 } catch (NdbApiException ndbApiException) {
200 throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
205 public void postExecuteCallback(Runnable callback) {
206 postExecuteCallbacks.add(callback);
209 private void clearPostExecuteCallbacks() {
210 postExecuteCallbacks.clear();
213 private void handlePendingPostExecuteCallbacks() {
214 // if any pending postExecuteCallbacks, flush via executeNoCommit
215 if (!postExecuteCallbacks.isEmpty()) {
220 private void performPostExecuteCallbacks() {
222 for (Runnable runnable: postExecuteCallbacks) {
225 } catch (Throwable t) {
226 throw new ClusterJDatastoreException(
227 local.message("ERR_Datastore"), t);
231 clearPostExecuteCallbacks();