1 package org.openecomp.sdc.be.dao.janusgraph.transactions;
3 import org.janusgraph.core.JanusGraphException;
4 import org.janusgraph.core.JanusGraph;
5 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
6 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
7 import org.openecomp.sdc.common.log.wrappers.Logger;
8 import org.springframework.stereotype.Component;
9 import org.springframework.transaction.PlatformTransactionManager;
10 import org.springframework.transaction.TransactionDefinition;
11 import org.springframework.transaction.TransactionStatus;
12 import org.springframework.transaction.TransactionSystemException;
13 import org.springframework.transaction.support.SimpleTransactionStatus;
15 import javax.annotation.PostConstruct;
18 * Simple transaction manager for the janusgraph database.
19 * 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
22 public class SimpleJanusGraphTransactionManager implements PlatformTransactionManager {
24 private static final Logger log = Logger.getLogger(SimpleJanusGraphTransactionManager.class.getName());
25 private final JanusGraphClient janusGraphClient;
26 private JanusGraph janusGraph;
28 public SimpleJanusGraphTransactionManager(JanusGraphClient janusGraphClient) {
29 this.janusGraphClient = janusGraphClient;
33 public void onInit() {
34 janusGraph = janusGraphClient.getGraph().left().on(this::onFailingToStartJanusGraph);
38 public TransactionStatus getTransaction(TransactionDefinition transactionDefinition) {
39 log.debug("#getTransaction - returning simple transaction status");
40 return new SimpleTransactionStatus();
44 public void commit(TransactionStatus transactionStatus) {
45 log.debug("#commit - committing transaction");
47 janusGraph.tx().commit();
48 } catch (JanusGraphException e) {
49 log.debug("#commit - failed to commit transaction", e);
50 throw new TransactionSystemException("failed to commit transaction", e);
55 public void rollback(TransactionStatus transactionStatus) {
56 log.debug("#rollback - committing transaction");
58 janusGraph.tx().rollback();
59 } catch (JanusGraphException e) {
60 log.debug("#rollback - failed to rollback transaction", e);
61 throw new TransactionSystemException("failed to rollback transaction", e);
65 private JanusGraph onFailingToStartJanusGraph(JanusGraphOperationStatus err) {
66 log.debug("#onFailingToStartJanusGraph - could not open janusgraph client");
67 throw new IllegalStateException("janusgraph could not be initialized: " + err);