/** Test that transactions do actually commit and that isInTransaction() works. */ public void testCommitTransactions() throws Exception { Address address1 = new Address(); address1.setAddress("Address 1"); Address address2 = new Address(); address2.setAddress("Address 2"); Query q = new Query(); QueryClass qcAddress = new QueryClass(Address.class); QueryField qf = new QueryField(qcAddress, "address"); ConstraintSet cs1 = new ConstraintSet(ConstraintOp.OR); cs1.addConstraint(new SimpleConstraint(qf, ConstraintOp.MATCHES, new QueryValue("Address%"))); q.addToSelect(qcAddress); q.addFrom(qcAddress); q.addToOrderBy(qf); q.setConstraint(cs1); try { writer.beginTransaction(); assertTrue(writer.isInTransaction()); writer.store(address1); writer.store(address2); // Should be nothing in OS until we commit Results res = realOs.execute(q); assertEquals(0, res.size()); // However, they should be in the WRITER. // TODO: These lines now fail, because we do not allow querying on writers with uncommitted // data. The writer should relax this restriction. res = writer.execute(q); assertEquals(2, res.size()); writer.commitTransaction(); assertFalse(writer.isInTransaction()); res = realOs.execute(q); assertEquals(2, res.size()); assertEquals(address1, (Address) ((ResultsRow) res.get(0)).get(0)); assertEquals(address2, (Address) ((ResultsRow) res.get(1)).get(0)); } finally { writer.delete(address1); writer.delete(address2); } }
public void testTransactionsAndCaches() throws Exception { Address address1 = new Address(); address1.setAddress("Address 1"); Address address2 = new Address(); address2.setAddress("Address 2"); writer.flushObjectById(); realOs.flushObjectById(); try { writer.store(address1); Integer id = address1.getId(); address2.setId(id); assertNull(realOs.pilferObjectById(id)); assertNull(writer.pilferObjectById(id)); assertNotNull("Looked for id " + id, realOs.getObjectById(id, Address.class)); assertNull(writer.pilferObjectById(id)); assertNotNull(realOs.pilferObjectById(id)); realOs.flushObjectById(); assertNotNull(writer.getObjectById(id, Address.class)); assertNotNull(writer.pilferObjectById(id)); assertNull(realOs.pilferObjectById(id)); assertNotNull(realOs.getObjectById(id, Address.class)); writer.store(address2); assertNotNull(writer.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress()); assertNotNull(realOs.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress()); writer.delete(address2); assertNull(writer.getObjectById(id, Address.class)); assertNull(realOs.getObjectById(id, Address.class)); writer.store(address1); writer.beginTransaction(); writer.store(address2); assertNotNull(writer.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress()); assertNotNull(realOs.getObjectById(id, Address.class)); assertEquals("Address 1", ((Address) realOs.getObjectById(id, Address.class)).getAddress()); writer.commitTransaction(); assertNotNull(writer.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress()); assertNotNull(realOs.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress()); writer.beginTransaction(); writer.delete(address1); assertNull(writer.getObjectById(id, Address.class)); assertNotNull(realOs.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress()); writer.abortTransaction(); assertNotNull(writer.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) writer.getObjectById(id, Address.class)).getAddress()); assertNotNull(realOs.getObjectById(id, Address.class)); assertEquals("Address 2", ((Address) realOs.getObjectById(id, Address.class)).getAddress()); } finally { writer.delete(address1); } }