title: orientdb: how to fully shutdown down a memory database (Java/Scala API)
body: <p>I'm trying to write some unit test utilities for an orientDB client in scala.</p>
  
  <p>The following is intended to take a function to operate on a DB, and it should wrap the function with code to create and destroy the DB for a single unit test.</p>
  
  <p>However, there doesn't see to be much good documentation on how to clean up a memory DB (and looking at many open source projects, people seem to simply just leak databases and create new ones on a new port).</p>
  
  <p>Simply calling db.close leaves the DB listening to a port and subsequent tests fail. Calling db.drop seems to work, but only if the func succeeded in adding data to the DB.</p>
  
  <p>So, what cleanup is required in the finally clause?</p>
  
  <pre><code>  @Test
    def fTest2(): Unit = {
  
  
      def withJSONDBLoan(func: ODatabaseDocumentTx =&gt; Unit) : Unit = {
        val db: ODatabaseDocumentTx = new ODatabaseDocumentTx("memory:jsondb")
        db.create()
        try {
  
          func(db)
  
        } finally {
          if (!db.isClosed){
            db.close   // Nope. DB is leaked.
          }
          // db.drop seems to close the DB but can't
          // see when to safely call this.
        }
      }
  
  
      val query1 = "insert into ouser set name='test',password='test', status='ACTIVE'"
      withJSONDBLoan { db =&gt;
        db.command(new OCommandSQL(query1)).execute[ODocument]()
      }
  
      // Fails at create because DB already exists.
      val query2 = "insert into ouser set name='test2',password='test2', status='ACTIVE'"
      withJSONDBLoan { db =&gt;
        db.command(new OCommandSQL(query2)).execute[ODocument]()
      }
    }
  </code></pre>