2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.transaction.mngr;
23 import org.openecomp.sdc.be.config.BeEcompErrorManager;
24 import org.openecomp.sdc.common.log.wrappers.Logger;
25 import org.openecomp.sdc.common.transaction.api.ICommitHandler;
26 import org.openecomp.sdc.common.transaction.api.TransactionUtils;
27 import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
28 import org.openecomp.sdc.common.transaction.api.TransactionUtils.LogMessages;
30 import java.util.List;
32 public class CommitManager {
34 // TODO test using slf4j-test and make this final
35 private static Logger log = Logger.getLogger(CommitManager.class);
36 private List<ICommitHandler> commitHandlers;
37 private Integer transactionId;
38 private String userId, actionType;
40 CommitManager(Integer transactionId, String userId, String actionType, List<ICommitHandler> commitHandlers) {
41 this.commitHandlers = commitHandlers;
42 this.transactionId = transactionId;
44 this.actionType = actionType;
48 public DBActionCodeEnum transactionCommit() {
49 log.debug(LogMessages.COMMIT_ACTION_ALL_DB, transactionId, userId, actionType);
50 DBActionCodeEnum commitResult = DBActionCodeEnum.SUCCESS;
51 ICommitHandler lastHandler = null;
53 for (ICommitHandler handler : commitHandlers) {
54 lastHandler = handler;
55 log.debug(LogMessages.COMMIT_ACTION_SPECIFIC_DB, transactionId, handler.getDBType().name(), userId, actionType);
56 DBActionCodeEnum commitCode = handler.doCommit();
57 if (commitCode == DBActionCodeEnum.FAIL_GENERAL) {
58 BeEcompErrorManager.getInstance().logBeSystemError("transactionCommit on DB " + handler.getDBType().name());
59 log.debug("Commit failed for SdncTransactionID:{} on DB:{}", transactionId, handler.getDBType().name());
60 commitResult = DBActionCodeEnum.FAIL_GENERAL;
63 log.debug("Commit succeeded for SdncTransactionID:{} on DB:{}", transactionId, handler.getDBType().name());
65 } catch (Exception e) {
66 BeEcompErrorManager.getInstance().logBeSystemError("transactionCommit - on DB " + getDBName(lastHandler));
67 log.debug("Commit failed for SdncTransactionID:{} on DB:{}, Exception message:{}", transactionId, getDBName(lastHandler), e.getMessage(), e);
68 log.info(TransactionUtils.TRANSACTION_MARKER, "Commit failed for SdncTransactionID:{} on DB:{}", transactionId, getDBName(lastHandler));
69 commitResult = DBActionCodeEnum.FAIL_GENERAL;
74 private String getDBName(ICommitHandler lastHandler) {
75 String dbName = "Unknown";
76 if (lastHandler != null) {
77 dbName = lastHandler.getDBType().name();
82 // TODO test using slf4j-test and remove this
83 static void setLog(Logger log) {
84 CommitManager.log = log;