title: JDBI manual transaction management
body: <p>I'm using JDBI for preparing data for test scenarios (setting up preconditions) in a separate project (rest-assured stuff). Now I have to use those JDBI-powered Daos for integration tests in a production project.</p>
  
  <p>An example data setup Dao looks like this:</p>
  
  <pre><code>class DocumentDao(jdbi: Jdbi) : DefaultCrudRepository&lt;Document&gt;(jdbi) {
  
      /* ... */
  
      override fun save(entity: Document, handle: Handle): Int {
          return handle.createUpdate(
                  """
                  INSERT INTO documents (
                      /* ... */
                  )
                  VALUES (
                      /* ... */
                  )
                  """
          )
                  .bindBean(entity)
                  .execute()
      }
      /* ... */
  }
  </code></pre>
  
  <p>And now I must use it together with a production persistence framework, because each scenario in integration tests is performed in a single transaction. So both precondition insertion and production data modification in DB must happen in a single transaction and be rolled back. Like this:</p>
  
  <pre><code>@Test
  public void LoadDocument_001() throws Exception
  {
      // Start a transaction
      startTx();
  
      Integer id = testDocumentDao.nextIdFromSequence();
  
      // Insert fake document into DB as a test precondition
      test.document.Document document = new test.document.Document(/* ... */);
  
      testDocumentDao.save(document);
  
      // Production Dao (subject) is called
      Document loadedDocument = subject.load(id.toString());
  
      // Rollback the transaction
      rollbackTx();
  }
  </code></pre>
  
  <p>startTx() and rollbackTx() are methods I can use to manually control a transaction by communicating with a connection directly. BUT! I can't control transactions for those JDBI-powered Daos. Every operation that is using JDBI happens to be automatically committed by JDBI. So Document I've created as a precondition is left in a DB after test finishes.</p>
  
  <p>Note: of course, I'm using the same connection instance in JDBI and in production persistence layer.</p>
  
  <p>So I have two questions:</p>
  
  <ol>
  <li><p>How do I do manual transaction management for JDBI-only operations? For instance, I have two Daos (DocumentDao and CustomerDao) built like the example above. I want to call methods on both of them, but I want all changes to happen in a single transaction. And then I want to roll back that transaction.</p></li>
  <li><p>How do I do manual transaction management for JDBI operation mixed with  other non-JDBI code? E.g. I'm using DocumentDao and also some production code which is changing DB state too. How can I control a transaction using JDBI and/or communicating with a connection directly? Do I have to somehow disable automatic transaction management in JDBI? I tried doing it using fake TransactionHandler with empty methods, but it doesn't take any effect.</p></li>
  </ol>