Java's try-with-resources statement

Hi!

I’m exploring using Kotlin to interact with the Neo4j graph database.

Is there something like Java’s try-with-resources statement present in Kotlin?

The way to peform a Neo4j transaction in java is …

try ( Transaction tx = graphDb.beginTx() )
{
     // operations on the graph
     // ...

  tx.success();
}


 
 
The way to do it prior to java 1.7 is …

 


Transaction tx = graphDb.beginTx();
try
{
  // operations on the graph
  // …

  tx.success();
}
finally
{
  tx.close();
}



I’m guessing i’ll do it with a try/finally clause in Kotlin, right?

@Erik,

If you’re interested, I’ve started a project (easyneo4j) that will use the transactional HTTP endpoint of Neo4J. Here’s some “dsl” I have so far:

  val customer = object {
  val labels = arrayListOf(“Customer”)
  val name = “Hadi”
  val email = “Hadi@somewhere.com
  }




  val graph = EasyNeo4J()


  graph.transaction {


  createNode(customer)
  createNode(customer)


  }

What stopped progress is that I use my EasyHttp library (https://github.com/hhariri/easyhttp.jvm) and needed to add Reactive Extensions support to make it much nicer in terms of usability (and not suffer out of order requests). GET already has. Need to add for POST, PUT.   

If you’re interested, let me know, would love some collaboration on it.

The DSL- code looks really nice! Is the code for easyneo4j on github as well?

Why do you reach for the http interaction and not the native jvm interaction with neo4j?

If I could help I would, but I’m a complete newbie on all of this. Don’t think I’ll have the skills to do anything useful right now ?:expressionless:

I have a small set of functions for when I play with Neo4j.

the main one is this:

fun transactional(graphDb: GraphDatabaseService, body: GraphDatabaseService.() -> Unit) {   val tx = graphDb.beginTx()!!   try{   graphDb.body()   tx.success()   } catch(e: Exception){   println(e.getMessage())   tx.failure()   }finally{   tx.finish()   } }

You could use like this:

transactional(graphDb) {   val node = getNodeById(352)!!   println("JSON = ${node["json"]}")   node.getRelationships()!!.forEach { relationship ->   println("$relationship ${relationship.getStartNode()}[${relationship.getType()}]${relationship.getEndNode()}")   }

  val relType = MyRelationshipTypes.inJurisdiction
  val inJurisdiction = node.getRelationships(relType, Direction.OUTGOING)!!.first()
  println(“inJurisdiction = ${inJurisdiction}”)

  val andorra = getNodeById(14)!!

  inJurisdiction.delete()

  node.createRelationshipTo(andorra, relType)
}


As you could see, the second parameter for transactional is a “Builder” (http://confluence.jetbrains.com/display/Kotlin/Type-safe+Groovy-style+builders)
Basically you pass an extension function for GraphDatabaseService as a parameter

So I can use GraphDatabaseService methods like getNodeById by “free”

It's not yet, but I can push it.

In terms of newbie, I’m a newbie to Neo4J too (and Rx) and this is part of my learning curve :). The reason for using the HTTP endpoint was that after speaking to one of the guys from Neo4J, they told me it’s the most recent API and they are actively maintaining it and he recommended it as a good option.

Let me know if you want to help, I can push the source up. Most of the heavyweight will be done by EasyHttp in principle.

Would be fun to check it out, so please push if you have the time.

As I said … I don’t think I’ll have much to contribute right now. But you newer know :wink:

Thanks a lot Mario!