Java Code Examples for org.neo4j.driver.v1.Session#beginTransaction()

The following examples show how to use org.neo4j.driver.v1.Session#beginTransaction() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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;
}