org.neo4j.driver.v1.Transaction Java Examples

The following examples show how to use org.neo4j.driver.v1.Transaction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final String query) throws DataDeleteException {
	
	deleteDetachKeywordValidation(query);
	
	try ( Session session = GraphDBConnection.getInstance().getDriver().session() )
       {
           session.writeTransaction( new TransactionWork<Integer>()
           {
               @Override
               public Integer execute( Transaction tx )
               {
                   return createNode( tx, query );
               }
           } );
       }
}
 
Example #2
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 6 votes vote down vote up
@Override
public void writeBulk(final List<String> queries) throws DataDeleteException {
	
	validateDeleteDetach(queries);
	try ( Session session = GraphDBConnection.getInstance().getDriver().session() )
       {
           session.writeTransaction( new TransactionWork<Integer>()
           {
               @Override
               public Integer execute( Transaction tx )
               {
                   return createBulkNodes( tx, queries );
               }
           } );
       }
	
}
 
Example #3
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 6 votes vote down vote up
@Override
public StatementResult read(final String query)  throws DataDeleteException {
	deleteDetachKeywordValidation(query);
	try ( Session session = GraphDBConnection.getInstance().getDriver().session() )
       {
		StatementResult records = session.readTransaction( new TransactionWork<StatementResult>()
           {
               @Override
               public StatementResult execute( Transaction tx )
               {
                   return runQuery( tx,query );
               }
           } );
		
		return records;
       }
}
 
Example #4
Source File: QueryRunner.java    From neoprofiler with Apache License 2.0 6 votes vote down vote up
public Value runQuerySingleResult(NeoProfiler parent, String query, String columnReturn) {		
	Session s = parent.getDriver().session();

	try ( Transaction tx = s.beginTransaction()) {
		// log.info(query);
		StatementResult result = tx.run(query);
					
		if(result.hasNext()) {
			Value val = result.next().get(columnReturn);
			return val;
		}
		
		return null;
	} finally {
		s.close();
	}
}
 
Example #5
Source File: QueryRunner.java    From neoprofiler with Apache License 2.0 6 votes vote down vote up
public Map<String,List<Object>> runQueryComplexResult(NeoProfiler parent, String query, String...columns) {
	HashMap<String,List<Object>> all = new HashMap<String,List<Object>>();
	
	List<Object> retvals = new ArrayList<Object>();
	System.out.println(query);
	Session s = parent.getDriver().session();
	try ( Transaction tx = s.beginTransaction()) {
		// log.info(query);
		StatementResult result = tx.run(query);
		
		while(result.hasNext()) {
			Map<String,Object> row = result.next().asMap();
			
			for(String col : columns) { 
				if(!all.containsKey(col)) all.put(col, new ArrayList<Object>());
				all.get(col).add(row.get(col));
			}
		}	
		
		tx.close();
	} finally {
		s.close();
	}
	
	return all;
}
 
Example #6
Source File: QueryRunner.java    From neoprofiler with Apache License 2.0 6 votes vote down vote up
public List<NeoProperty> getSampleProperties(NeoProfiler parent, Entity n) {
	List<NeoProperty> props = new ArrayList<NeoProperty>();
	
	Session s = parent.getDriver().session();
	try ( Transaction tx = s.beginTransaction() ) {			 
		Iterator<String> propKeys = n.keys().iterator();
		
		while(propKeys.hasNext()) {
			String key = propKeys.next();
			Object val = n.get(key);
			
			props.add(new NeoProperty(key, val.getClass().getSimpleName()));				
		}
		
	} finally {
		s.close();
	}

	return props;
}
 
Example #7
Source File: StatefulTestBean.java    From thorntail with Apache License 2.0 6 votes vote down vote up
public String transactionEnlistmentReadAfterCallingTransactionClose() {
    // JTA transaction is started by CMT, the following obtains a Session that is enlisted into the JTA transaction.
    Session session = injectedDriver.session();
    // The only way to influence success/failure of the Neo4j + JTA transaction, is at the JTA transaction level.
    // If the JTA transaction fails, org.neo4j.driver.v1.Transaction.failure() is called.
    // If the JTA transaction succeeds, org.neo4j.driver.v1.Transaction.success() is called.
    // org.neo4j.driver.v1.Transaction.close() is also called when the JTA transaction ends.

    // Calls to Session.beginTransaction() in a JTA transaction are expected to throw a RuntimeException
    try {
        Transaction transaction = session.beginTransaction();
        fail("Calling Session.beginTransaction in a JTA transaction should throw a RuntimeException.");
    } catch (RuntimeException expectedException) {
        // success
    }

    try {
        session.run("CREATE (a:Person {name:'TRANSACTION', title:'King'})");
        return nestedBean.getPerson("TRANSACTION");
    } finally {
        if ( session.isOpen()) { // this will be true
            session.run("MATCH (a:Person) delete a");
            session.close();             // ignored, session is auto closed when the transaction ends.
        }
    }
}
 
Example #8
Source File: Neo4jBoltPersistWriter.java    From streams with Apache License 2.0 6 votes vote down vote up
@Override
public void write(StreamsDatum entry) {

  List<Pair<String, Map<String, Object>>> statements;
  Session session = null;
  try {
    statements = Neo4jPersistUtil.prepareStatements(entry);
    session = client.client().session();
    Transaction transaction = session.beginTransaction();
    for( Pair<String, Map<String, Object>> statement : statements ) {
      StatementResult statementResult = transaction.run( statement.getValue0(), statement.getValue1() );
      LOGGER.debug("StatementResult", statementResult.single());
    }
    transaction.success();
  } catch( Exception ex ) {
    LOGGER.error("Exception", ex);
  } finally {
    if( session != null ) {
      session.close();
    }
  }
}
 
Example #9
Source File: BoltTransactionImpl.java    From jcypher with Apache License 2.0 5 votes vote down vote up
public Transaction getTransaction() {
	if (this.transaction == null) {
		BoltDBAccess bdba = getBoltDBAccess();
		this.transaction = bdba.getSession().beginTransaction();
	}
	return this.transaction;
}
 
Example #10
Source File: DatabaseConnector.java    From Getaviz with Apache License 2.0 5 votes vote down vote up
public void executeWrite(String... statements) {
	try (Session session = driver.session(AccessMode.WRITE)) {
		session.writeTransaction((Transaction tx) -> {
			for (String statement : statements) {
				tx.run(statement);
			}
			return 1;
		});
	}
}
 
Example #11
Source File: BoltTransactionImpl.java    From jcypher with Apache License 2.0 5 votes vote down vote up
@Override
public List<JcError> close() {
	List<JcError> errors;
	if (isClosed())
		throw new RuntimeException(ERR_CLOSED);
	if (!isMyThread())
		throw new RuntimeException(ERR_THREAD);
	
	BoltDBAccess bdba = getBoltDBAccess();
	bdba.removeTx();
	if (this.transaction != null) {
		Transaction tx = getTransaction();
		if (failed)
			tx.failure();
		else
			tx.success();
		
		Throwable dbException = null;
		try {
			tx.close();
		} catch(Throwable e) {
			dbException = e;
		}
		errors = DBUtil.buildErrorList(null, dbException);
	} else 
		errors = new ArrayList<JcError>();
	
	if (errors.size() > 0)
		failure();
	setClosed();
	
	return errors;
}
 
Example #12
Source File: QueryRunner.java    From neoprofiler with Apache License 2.0 5 votes vote down vote up
public List<Object> runQueryMultipleResult(NeoProfiler parent, String query, String columnReturn) {
	Session s = parent.getDriver().session();
	List<Object> retvals = new ArrayList<Object>();
	
	try ( Transaction tx = s.beginTransaction()) {
		// log.info(query);
		StatementResult result = tx.run(query);
				
		while(result.hasNext()) {
			retvals.add(result.next().get(columnReturn));
		}					
	}
	
	return retvals;
}
 
Example #13
Source File: NeoProfiler.java    From neoprofiler with Apache License 2.0 5 votes vote down vote up
public Transaction beginTx() {
	if (this.session == null) {
		this.session = driver.session();
	}

	return this.session.beginTransaction();
}
 
Example #14
Source File: TwitterFollowNeo4jIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@AfterClass
public void cleanup() throws Exception {
  Session session = testClient.client().session();
  Transaction transaction = session.beginTransaction();
  transaction.run("MATCH ()-[r]-() DELETE r");
  transaction.run("MATCH (n) DETACH DELETE n");
  transaction.success();
  session.close();
}
 
Example #15
Source File: TwitterFollowNeo4jIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void prepareTest() throws IOException {

  testConfiguration = new StreamsConfigurator<>(TwitterFollowNeo4jConfiguration.class).detectCustomConfiguration();
  testClient = Neo4jBoltClient.getInstance(testConfiguration.getNeo4j());

  Session session = testClient.client().session();
  Transaction transaction = session.beginTransaction();
  transaction.run("MATCH ()-[r]-() DELETE r");
  transaction.run("MATCH (n) DETACH DELETE n");
  transaction.success();
  session.close();
}
 
Example #16
Source File: Neo4jBoltPersistIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@AfterClass
public void cleanup() throws Exception {
  Session session = testClient.client().session();
  Transaction transaction = session.beginTransaction();
  transaction.run("MATCH ()-[r]-() DELETE r");
  transaction.run("MATCH (n) DETACH DELETE n");
  transaction.success();
  session.close();
}
 
Example #17
Source File: Neo4jBoltPersistIT.java    From streams with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void prepareTest() throws IOException {

  testConfiguration = new ComponentConfigurator<>(Neo4jConfiguration.class).detectConfiguration( "Neo4jBoltPersistIT");
  testClient = Neo4jBoltClient.getInstance(testConfiguration);

  Session session = testClient.client().session();
  Transaction transaction = session.beginTransaction();
  transaction.run("MATCH ()-[r]-() DELETE r");
  transaction.run("MATCH (n) DETACH DELETE n");
  transaction.success();
  session.close();
}
 
Example #18
Source File: Neo4jBoltPersistReader.java    From streams with Apache License 2.0 5 votes vote down vote up
@Override
public StreamsResultSet readAll() {

  Session session = null;

  String query = config.getQuery();
  Map<String, Object> params = mapper.convertValue(config.getParams(), Map.class);

  try {
    session = client.client().session();
    Transaction transaction = session.beginTransaction();

    this.statementResult = client.client().session().beginTransaction().run(query, params);

    while( statementResult.hasNext()) {
      Record record = statementResult.next();
      Optional<StreamsDatum> datum = buildDatum(record);
      if( datum.isPresent()) {
        write(datum.get());
      }
    }

  } catch(Exception ex) {
    LOGGER.warn("Exception", ex);
  } finally {
    if( session != null ) {
      session.close();
    }
  }
  return readCurrent();

}
 
Example #19
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 5 votes vote down vote up
private Integer createBulkNodes(Transaction tx, List<String> queries) {
	
	Iterator<String> queryItr = queries.iterator();
	
	while(queryItr.hasNext()) {
		runQuery(tx,queryItr.next());
	}
	tx.success();
	return 1;
}
 
Example #20
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Override
public void delete(final String query) {
	try ( Session session = GraphDBConnection.getInstance().getDriver().session() )
       {
           session.writeTransaction( new TransactionWork<Integer>()
           {
               @Override
               public Integer execute( Transaction tx )
               {
                   return deleteNode( tx,query );
               }
           } );
       }
}
 
Example #21
Source File: DatabaseConnector.java    From Getaviz with Apache License 2.0 5 votes vote down vote up
public Node addNode(String statement, String parameterName) {
	Node result;
	try (Session session = driver.session()) {
           try (Transaction tx = session.beginTransaction()) {
               result = tx.run(statement + " RETURN " + parameterName).next().get(parameterName).asNode();
               tx.success();  // Mark this write as successful.
           }
       }
	return result;
}
 
Example #22
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 4 votes vote down vote up
private StatementResult runQuery(Transaction tx,String query) {
 return tx.run( query );
}
 
Example #23
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 4 votes vote down vote up
private Integer deleteNode( Transaction tx,String query )
{
 runQuery(tx,query);
    tx.success();
    return 1;
}
 
Example #24
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 4 votes vote down vote up
private Integer createNode( Transaction tx,String query )
{
 runQuery(tx,query);
    tx.success();
    return 1;
}
 
Example #25
Source File: InsightsGraphDBHandler.java    From Insights with Apache License 2.0 4 votes vote down vote up
@Override
public StatementResult execute(Transaction tx,String query) {
	return runQuery(tx,query);
}
 
Example #26
Source File: BaseGraphDBHandler.java    From Insights with Apache License 2.0 votes vote down vote up
public StatementResult execute(Transaction tx,String query);