MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ClusterTransactionImpl.java
1 /*
2  * Copyright 2010 Sun Microsystems, Inc.
3  * All rights reserved. Use is subject to license terms.
4  *
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.
8  *
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.
13  *
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
17  */
18 
19 package com.mysql.clusterj.bindings;
20 
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;
39 
43 class ClusterTransactionImpl implements ClusterTransaction {
44 
46  static final I18NHelper local = I18NHelper
47  .getInstance(ClusterTransactionImpl.class);
48 
50  static final Logger logger = LoggerFactoryService.getFactory()
51  .getInstance(ClusterTransactionImpl.class);
52 
53  protected NdbTransaction ndbTransaction;
54  private List<Runnable> postExecuteCallbacks = new ArrayList<Runnable>();
55 
56  public ClusterTransactionImpl(NdbTransaction ndbTransaction) {
57  this.ndbTransaction = ndbTransaction;
58  }
59 
60  public void close() {
61  ndbTransaction.close();
62  }
63 
64  public void executeCommit() {
65  handlePendingPostExecuteCallbacks();
66  try {
67  ndbTransaction.executeCommit();
68  } catch (NdbApiException ndbApiException) {
69  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
70  ndbApiException);
71  }
72  }
73 
74  public void executeCommit(boolean abort, boolean force) {
75  handlePendingPostExecuteCallbacks();
76  AbortOption abortOption = abort?AbortOption.AbortOnError:AbortOption.AO_IgnoreError;
77  try {
78  ndbTransaction.execute(NdbTransaction.ExecType.Commit,
79  abortOption, force);
80  } catch (NdbApiException ndbApiException) {
81  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
82  ndbApiException);
83  }
84  }
85 
86  public void executeNoCommit(boolean abort, boolean force) {
87  AbortOption abortOption = abort?AbortOption.AbortOnError:AbortOption.AO_IgnoreError;
88  try {
89  ndbTransaction.execute(NdbTransaction.ExecType.NoCommit,
90  abortOption, force);
91  performPostExecuteCallbacks();
92  } catch (NdbApiException ndbApiException) {
93  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
94  ndbApiException);
95  }
96  }
97 
98  public void executeNoCommit() {
99  try {
100  ndbTransaction.executeNoCommit();
101  performPostExecuteCallbacks();
102  } catch (NdbApiException ndbApiException) {
103  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
104  ndbApiException);
105  }
106  }
107 
108  public void executeRollback() {
109  try {
110  clearPostExecuteCallbacks();
111  ndbTransaction.executeRollback();
112  } catch (NdbApiException ndbApiException) {
113  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
114  ndbApiException);
115  }
116  }
117 
118  public Operation getDeleteOperation(Table storeTable) {
119  try {
120  return new OperationImpl(ndbTransaction.getDeleteOperation(storeTable.getName()), this);
121  } catch (NdbApiException ndbApiException) {
122  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
123  ndbApiException);
124  }
125  }
126 
127  public Operation getInsertOperation(Table storeTable) {
128  try {
129  return new OperationImpl(ndbTransaction.getInsertOperation(storeTable.getName()), this);
130  } catch (NdbApiException ndbApiException) {
131  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
132  ndbApiException);
133  }
134  }
135 
136  public IndexScanOperation getSelectIndexScanOperation(Index storeIndex, Table storeTable) {
137  try {
138  return new IndexScanOperationImpl(
139  ndbTransaction.getSelectIndexScanOperation(storeIndex.getName(), storeTable.getName()), this);
140  } catch (NdbApiException ndbApiException) {
141  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
142  ndbApiException);
143  }
144  }
145 
146  public Operation getSelectOperation(Table storeTable) {
147  try {
148  return new OperationImpl(ndbTransaction.getSelectOperation(storeTable.getName()), this);
149  } catch (NdbApiException ndbApiException) {
150  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
151  ndbApiException);
152  }
153  }
154 
155  public ScanOperation getSelectScanOperation(Table storeTable) {
156  try {
157  return new ScanOperationImpl(
158  ndbTransaction.getSelectScanOperation(storeTable.getName()), this);
159  } catch (NdbApiException ndbApiException) {
160  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
161  ndbApiException);
162  }
163  }
164 
165  public ScanOperation getSelectScanOperationLockModeExclusiveScanFlagKeyInfo(Table storeTable) {
166  try {
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"),
173  ndbApiException);
174  }
175  }
176 
177  public IndexOperation getSelectUniqueOperation(Index storeIndex, Table storeTable) {
178  try {
179  return new IndexOperationImpl(
180  ndbTransaction.getSelectUniqueOperation(storeIndex.getName(), storeTable.getName()), this);
181  } catch (NdbApiException ndbApiException) {
182  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
183  ndbApiException);
184  }
185  }
186 
187  public Operation getUpdateOperation(Table storeTable) {
188  try {
189  return new OperationImpl(ndbTransaction.getUpdateOperation(storeTable.getName()), this);
190  } catch (NdbApiException ndbApiException) {
191  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
192  ndbApiException);
193  }
194  }
195 
196  public Operation getWriteOperation(Table storeTable) {
197  try {
198  return new OperationImpl(ndbTransaction.getWriteOperation(storeTable.getName()), this);
199  } catch (NdbApiException ndbApiException) {
200  throw new ClusterJDatastoreException(local.message("ERR_Datastore"),
201  ndbApiException);
202  }
203  }
204 
205  public void postExecuteCallback(Runnable callback) {
206  postExecuteCallbacks.add(callback);
207  }
208 
209  private void clearPostExecuteCallbacks() {
210  postExecuteCallbacks.clear();
211  }
212 
213  private void handlePendingPostExecuteCallbacks() {
214  // if any pending postExecuteCallbacks, flush via executeNoCommit
215  if (!postExecuteCallbacks.isEmpty()) {
216  executeNoCommit();
217  }
218  }
219 
220  private void performPostExecuteCallbacks() {
221  try {
222  for (Runnable runnable: postExecuteCallbacks) {
223  try {
224  runnable.run();
225  } catch (Throwable t) {
226  throw new ClusterJDatastoreException(
227  local.message("ERR_Datastore"), t);
228  }
229  }
230  } finally {
231  clearPostExecuteCallbacks();
232  }
233  }
234 
235 }