@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()); }
@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); }
@Override public void deleteChargePoint(int chargeBoxPk) { ctx.transaction( configuration -> { DSLContext ctx = DSL.using(configuration); try { addressRepository.delete(ctx, selectAddressId(chargeBoxPk)); deleteChargePointInternal(ctx, chargeBoxPk); } catch (DataAccessException e) { throw new SteveException("Failed to delete the charge point", e); } }); }
@Override public void updateChargePoint(ChargePointForm form) { ctx.transaction( configuration -> { DSLContext ctx = DSL.using(configuration); try { Integer addressId = addressRepository.updateOrInsert(ctx, form.getAddress()); updateChargePointInternal(ctx, form, addressId); } catch (DataAccessException e) { throw new SteveException( "Failed to update the charge point with chargeBoxId '%s'", form.getChargeBoxId(), e); } }); }