Ejemplo n.º 1
0
  @Test
  public void testjOOQTransactionsNested() {
    AtomicBoolean rollback1 = new AtomicBoolean(false);
    AtomicBoolean rollback2 = new AtomicBoolean(false);

    try {

      // If using Spring transactions, we don't need the c1 reference
      dsl.transaction(
          c1 -> {

            // The first insertion will work
            dsl.insertInto(BOOK)
                .set(BOOK.ID, 5)
                .set(BOOK.AUTHOR_ID, 1)
                .set(BOOK.TITLE, "Book 5")
                .execute();

            assertEquals(5, dsl.fetchCount(BOOK));

            try {

              // Nest transactions using Spring. This should create a savepoint, right here
              // If using Spring transactions, we don't need the c2 reference
              dsl.transaction(
                  c2 -> {

                    // The second insertion shouldn't work
                    for (int i = 0; i < 2; i++)
                      dsl.insertInto(BOOK)
                          .set(BOOK.ID, 6)
                          .set(BOOK.AUTHOR_ID, 1)
                          .set(BOOK.TITLE, "Book 6")
                          .execute();

                    Assert.fail();
                  });
            } catch (DataAccessException e) {
              rollback1.set(true);
            }

            // We should've rolled back to the savepoint
            assertEquals(5, dsl.fetchCount(BOOK));

            throw new org.jooq.exception.DataAccessException("Rollback");
          });
    }

    // Upon the constraint violation, the transaction must already have been rolled back
    catch (org.jooq.exception.DataAccessException e) {
      assertEquals("Rollback", e.getMessage());
      rollback2.set(true);
    }

    assertEquals(4, dsl.fetchCount(BOOK));
    assertTrue(rollback2.get());
    assertTrue(rollback2.get());
  }
Ejemplo n.º 2
0
  @Test
  public void testExplicitTransactions() {
    boolean rollback = false;

    TransactionStatus tx = txMgr.getTransaction(new DefaultTransactionDefinition());
    try {

      // This is a "bug". The same book is created twice, resulting in a
      // constraint violation exception
      for (int i = 0; i < 2; i++)
        dsl.insertInto(BOOK)
            .set(BOOK.ID, 5)
            .set(BOOK.AUTHOR_ID, 1)
            .set(BOOK.TITLE, "Book 5")
            .execute();

      Assert.fail();
    }

    // Upon the constraint violation, we explicitly roll back the transaction.
    catch (DataAccessException e) {
      txMgr.rollback(tx);
      rollback = true;
    }

    assertEquals(4, dsl.fetchCount(BOOK));
    assertTrue(rollback);
  }
Ejemplo n.º 3
0
  @Test
  public void testjOOQTransactionsSimple() {
    boolean rollback = false;

    try {
      dsl.transaction(
          c -> {

            // This is a "bug". The same book is created twice, resulting in a
            // constraint violation exception
            for (int i = 0; i < 2; i++)
              dsl.insertInto(BOOK)
                  .set(BOOK.ID, 5)
                  .set(BOOK.AUTHOR_ID, 1)
                  .set(BOOK.TITLE, "Book 5")
                  .execute();

            Assert.fail();
          });
    }

    // Upon the constraint violation, the transaction must already have been rolled back
    catch (DataAccessException e) {
      rollback = true;
    }

    assertEquals(4, dsl.fetchCount(BOOK));
    assertTrue(rollback);
  }
Ejemplo n.º 4
0
  @Test
  public void testDeclarativeTransactions() {
    boolean rollback = false;

    try {
      books.create(5, 1, "Book 5");
      Assert.fail();
    } catch (DataAccessException ignore) {
      rollback = true;
    }

    assertEquals(4, dsl.fetchCount(BOOK));
    assertTrue(rollback);
  }