/** * Sets the transaction to rollback. */ protected final void setRollbackOnly() { this.emfWork.setRollbackOnly(); }
@After public void cleanupDb() { if (this.emfWork != null) { this.emfWork.close(); this.emfWork = null; this.em = null; } }
@Override protected void runDbInitializationScripts(JuEmfUtil emfUtil) { try (EmfWork ew = emfUtil.startWork()) { new DbSchemaUtil(ew.getEm()).prepareDefaultSchemaAndTestData();; } }
@Test @DataSet("AbstractDbTestTransactionTest_testingEntity.xml") @DataVerify @DataSetExport(tablesDataSet="AbstractDbTestTransactionTest_testingEntity.xml", doPhysicalExport=false) public void multipleTransactions_areSupported() { // H2 has a locking strategy that will cause a timeout in this scenario JuAssumeUtils.dbIsNot(this.emUtil, DbType.H2); TestingEntity te1 = new TestingEntity("TE1"); this.em.persist(te1); try (EmfWork ew2 = this.startNewWork()) { TestingEntity te2 = new TestingEntity("TE2"); ew2.getEm().persist(te2); try (EmfWork ew3 = this.startNewWork()) { TestingEntity te3 = new TestingEntity("TE3"); ew3.getEm().persist(te3); } ew2.setRollbackOnly(); } } public static class MultipleTransactions_areSupported extends DataVerifier {
@Override public void evaluate() throws Throwable { // Run dbInitializationScripts dbTest.runDbInitializationScripts(dbTest.emfUtil); DbTestAnnotationHandler annotationHandler = new DbTestAnnotationHandler(method, description); // Run preAnnotations in own transaction try (EmfWork ew = dbTest.emfUtil.startWork()) { annotationHandler.executePreTestAnnotations(ew.getEmUtil()); } // Initialize protected fields of test class dbTest.emfWork = dbTest.emfUtil.startWork(); dbTest.em = dbTest.emfWork.getEm(); dbTest.emUtil = new JuEmUtil(dbTest.em); // Run test method base.evaluate(); // Run post server code in own transaction try (EmfWork ew = dbTest.emfUtil.startWork()) { annotationHandler.executePostServerCode(ew.getEmUtil()); } // Run postAnnotations in own transaction try (EmfWork ew = dbTest.emfUtil.startWork()) { annotationHandler.executePostTestAnnotations(ew.getEmUtil()); } } };
@Override protected void runDbInitializationScripts(JuEmfUtil emfUtil) { try (EmfWork ew = emfUtil.startWork()) { new DbSchemaUtil(ew.getEm()).prepareDefaultSchemaAndTestData();; } }
@Test public void canPerformWork_usingEmfUtil() { JuEmfUtil emfUtil = JuEmfUtil.create() .persistenceUnitName("ju-dbutil JuEmfUtilTest") .connectionUrl("jdbc:derby:memory:ju-dbutil_emfUtilTest_db1;create=true") .build(); try (EmfWork work = emfUtil.startWork()) { Query q = work.getEm().createNativeQuery("values 1"); Assert.assertEquals(1, q.getSingleResult()); } } }