2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.be.dao.janusgraph.transactions;
23 import org.janusgraph.core.JanusGraphException;
24 import org.janusgraph.core.JanusGraph;
25 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
26 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.springframework.stereotype.Component;
29 import org.springframework.transaction.PlatformTransactionManager;
30 import org.springframework.transaction.TransactionDefinition;
31 import org.springframework.transaction.TransactionStatus;
32 import org.springframework.transaction.TransactionSystemException;
33 import org.springframework.transaction.support.SimpleTransactionStatus;
35 import javax.annotation.PostConstruct;
38 * Simple transaction manager for the janusgraph database.
39 * This manager does not deal with transactions propagation and relies on the fact that transactions are automatically created with the first operation on the graph
42 public class SimpleJanusGraphTransactionManager implements PlatformTransactionManager {
44 private static final Logger log = Logger.getLogger(SimpleJanusGraphTransactionManager.class.getName());
45 private final JanusGraphClient janusGraphClient;
46 private JanusGraph janusGraph;
48 public SimpleJanusGraphTransactionManager(JanusGraphClient janusGraphClient) {
49 this.janusGraphClient = janusGraphClient;
53 public void onInit() {
54 janusGraph = janusGraphClient.getGraph().left().on(this::onFailingToStartJanusGraph);
58 public TransactionStatus getTransaction(TransactionDefinition transactionDefinition) {
59 log.debug("#getTransaction - returning simple transaction status");
60 return new SimpleTransactionStatus();
64 public void commit(TransactionStatus transactionStatus) {
65 log.debug("#commit - committing transaction");
67 janusGraph.tx().commit();
68 } catch (JanusGraphException e) {
69 log.debug("#commit - failed to commit transaction", e);
70 throw new TransactionSystemException("failed to commit transaction", e);
75 public void rollback(TransactionStatus transactionStatus) {
76 log.debug("#rollback - committing transaction");
78 janusGraph.tx().rollback();
79 } catch (JanusGraphException e) {
80 log.debug("#rollback - failed to rollback transaction", e);
81 throw new TransactionSystemException("failed to rollback transaction", e);
85 private JanusGraph onFailingToStartJanusGraph(JanusGraphOperationStatus err) {
86 log.debug("#onFailingToStartJanusGraph - could not open janusgraph client");
87 throw new IllegalStateException("janusgraph could not be initialized: " + err);