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.config.EcompErrorName;
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;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.List;
34 public class CommitManager {
36 // TODO test using slf4j-test and make this final
37 private static Logger log = LoggerFactory.getLogger(CommitManager.class);
38 private List<ICommitHandler> commitHandlers;
39 private Integer transactionId;
40 private String userId, actionType;
42 CommitManager(Integer transactionId, String userId, String actionType, List<ICommitHandler> commitHandlers) {
43 this.commitHandlers = commitHandlers;
44 this.transactionId = transactionId;
46 this.actionType = actionType;
50 public DBActionCodeEnum transactionCommit() {
51 log.debug(LogMessages.COMMIT_ACTION_ALL_DB, transactionId, userId, actionType);
52 DBActionCodeEnum commitResult = DBActionCodeEnum.SUCCESS;
53 ICommitHandler lastHandler = null;
55 for (ICommitHandler handler : commitHandlers) {
56 lastHandler = handler;
57 log.debug(LogMessages.COMMIT_ACTION_SPECIFIC_DB, transactionId, handler.getDBType().name(), userId, actionType);
58 DBActionCodeEnum commitCode = handler.doCommit();
59 if (commitCode == DBActionCodeEnum.FAIL_GENERAL) {
60 BeEcompErrorManager.getInstance().logBeSystemError("transactionCommit on DB " + handler.getDBType().name());
61 log.debug("Commit failed for SdncTransactionID:{} on DB:{}", transactionId, handler.getDBType().name());
62 commitResult = DBActionCodeEnum.FAIL_GENERAL;
65 log.debug("Commit succeeded for SdncTransactionID:{} on DB:{}", transactionId, handler.getDBType().name());
67 } catch (Exception e) {
68 BeEcompErrorManager.getInstance().logBeSystemError("transactionCommit - on DB " + getDBName(lastHandler));
69 log.debug("Commit failed for SdncTransactionID:{} on DB:{}, Exception message:{}", transactionId, getDBName(lastHandler), e.getMessage(), e);
70 log.info(TransactionUtils.TRANSACTION_MARKER, "Commit failed for SdncTransactionID:{} on DB:{}", transactionId, getDBName(lastHandler));
71 commitResult = DBActionCodeEnum.FAIL_GENERAL;
76 private String getDBName(ICommitHandler lastHandler) {
77 String dbName = "Unknown";
78 if (lastHandler != null) {
79 dbName = lastHandler.getDBType().name();
84 // TODO test using slf4j-test and remove this
85 static void setLog(Logger log) {
86 CommitManager.log = log;