|
41 | 41 | import org.slf4j.Logger;
|
42 | 42 | import org.slf4j.LoggerFactory;
|
43 | 43 |
|
| 44 | +import com.google.common.base.Throwables; |
| 45 | + |
44 | 46 | import javax.transaction.xa.Xid;
|
45 | 47 |
|
46 | 48 | import java.io.IOException;
|
@@ -137,11 +139,22 @@ public void write(SeaTunnelRow element) {
|
137 | 139 | @Override
|
138 | 140 | public Optional<XidInfo> prepareCommit() throws IOException {
|
139 | 141 | tryOpen();
|
140 |
| - prepareCurrentTx(); |
| 142 | + |
| 143 | + boolean emptyXaTransaction = false; |
| 144 | + try { |
| 145 | + prepareCurrentTx(); |
| 146 | + } catch (Exception e) { |
| 147 | + if (Throwables.getRootCause(e) instanceof XaFacade.EmptyXaTransactionException) { |
| 148 | + emptyXaTransaction = true; |
| 149 | + LOG.info("skip prepare empty xa transaction, xid={}", currentXid); |
| 150 | + } else { |
| 151 | + throw e; |
| 152 | + } |
| 153 | + } |
141 | 154 | this.currentXid = null;
|
142 | 155 | beginTx();
|
143 | 156 | checkState(prepareXid != null, "prepare xid must not be null");
|
144 |
| - return Optional.of(new XidInfo(prepareXid, 0)); |
| 157 | + return emptyXaTransaction ? Optional.empty() : Optional.of(new XidInfo(prepareXid, 0)); |
145 | 158 | }
|
146 | 159 |
|
147 | 160 | @Override
|
@@ -186,14 +199,22 @@ private void beginTx() throws IOException {
|
186 | 199 | private void prepareCurrentTx() throws IOException {
|
187 | 200 | checkState(currentXid != null, "no current xid");
|
188 | 201 | outputFormat.flush();
|
| 202 | + |
| 203 | + Exception endAndPrepareException = null; |
189 | 204 | try {
|
190 | 205 | xaFacade.endAndPrepare(currentXid);
|
191 |
| - prepareXid = currentXid; |
192 | 206 | } catch (Exception e) {
|
| 207 | + endAndPrepareException = e; |
193 | 208 | throw new JdbcConnectorException(
|
194 | 209 | JdbcConnectorErrorCode.XA_OPERATION_FAILED,
|
195 | 210 | "unable to prepare current xa transaction",
|
196 | 211 | e);
|
| 212 | + } finally { |
| 213 | + if (endAndPrepareException == null |
| 214 | + || Throwables.getRootCause(endAndPrepareException) |
| 215 | + instanceof XaFacade.EmptyXaTransactionException) { |
| 216 | + prepareXid = currentXid; |
| 217 | + } |
197 | 218 | }
|
198 | 219 | }
|
199 | 220 | }
|
0 commit comments