2

Imagine I have the following code for inserting a record into Apache Jena database (TBD):

private fun createPersonLogic(ds: Dataset, email: String, nick: String) {
    ds.begin(ReadWrite.WRITE)
    val model = ds.defaultModel

    val uuid = UUID.randomUUID()
    val uri = "http://mycompany.com/data/p-${uuid}"
    val person = model.createResource(uri)
    person.addProperty(VCARD.EMAIL, email)
    person.addProperty(VCARD.N,
            model.createResource()
                    .addProperty(VCARD.NICKNAME, nick))
    ds.commit()
    ds.end()
}

What is the right way to handle errors during execution of model.createResource and ds.commit -- like in the code fragment below or differently?

private fun createPersonLogic(ds: Dataset, email: String, nick: String) {
    try {
        ds.begin(ReadWrite.WRITE)
        val model = ds.defaultModel

        val uuid = UUID.randomUUID()
        val uri = "http://mycompany.com/data/p-${uuid}"
        val person = model.createResource(uri)
        person.addProperty(VCARD.EMAIL, email)
        person.addProperty(VCARD.N,
                model.createResource()
                        .addProperty(VCARD.NICKNAME, nick))
        ds.commit() 
    }
    catch (throwable:Throwable) {
        // react to the error
    }
    finally {
        ds.end()
    }
}
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325

1 Answers1

0

How could we possible know what is the correct answer in your application to such a failure?!

Seriously: it is your project, your application, your requirements. You have to be clear what such kind of problems mean to you.

Nonetheless, a "generic" is:

  1. Give feedback to the user of the system - obviously some very core operation just failed. You can't continue this operation; you might even need some rollback.
  2. There are patterns around failure situations (like the circuit breaker) that give guidance for such kind of problems. In other words: when your database refuses to write data, something important might be broken. You probably have to deal with that; beyond the scope of that one operation that failed in this case.
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • The question is **not** application-specific. There are general rules for error handilng. If you read or write to a file, you have to catch a standard set of exceptions (I/O, file not found) and close the streams in both success and failure cases. If you insert a record into a relational database with JDBC, you need to rollback the transaction in case of failure, and close the statement. These things are generic and don't depend on application. I'm looking for an established best practice for Apache Jena. – Glory to Russia Feb 03 '17 at 08:28
  • 1
    Call `ds.abort()` (in fact, the `ds.end()` will do that for you - it aborts any uncommited transaction). So the database clears up; a database error is serious (e.g. out of disk space, JVM OOME) and needs to be looked at but the data is intact, just not updated. The rest is an application issue. – AndyS Feb 03 '17 at 11:54
  • @AndyS Does it mean that my code handles persistence-related errors correctly? – Glory to Russia Feb 06 '17 at 04:08
  • Your code handles the transaction OK with try-finally. The first version is not because it can bypass the `ds.end()`. Th code also needs to react to the fact a write-transaction failed. – AndyS Feb 07 '17 at 09:18