mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-12 22:41:35 +00:00
Use WeakReferences when in iterate query mode
This commit is contained in:
1 parent
ea3fb833a4
commit
3f5009aa91
5 files changed
+195
-298
No files matched your search
@@ -2,16 +2,23 @@ package org.tests.basic;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Query;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebeaninternal.api.SpiPersistenceContext;
|
||||
import io.ebeaninternal.api.SpiTransaction;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.Order;
|
||||
import org.tests.model.basic.ResetBasicData;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class TestPersistenceContext extends BaseTestCase {
|
||||
@@ -112,6 +119,65 @@ public class TestPersistenceContext extends BaseTestCase {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Disabled // run manually
|
||||
@Test
|
||||
void testPcScopes() throws InterruptedException {
|
||||
|
||||
for (int i = 0; i < 5000; i++) {
|
||||
Customer c = new Customer();
|
||||
c.setName("Customer #" + i);
|
||||
DB.save(c);
|
||||
Order o = new Order();
|
||||
o.setCustomer(c);
|
||||
DB.save(o);
|
||||
}
|
||||
|
||||
try (Transaction txn = DB.beginTransaction()) {
|
||||
List<Customer> first100 = DB.find(Customer.class).where().le("id", 100).findList();
|
||||
assertEquals(100, first100.size());
|
||||
for (Customer c : first100) {
|
||||
c.setSmallnote("one of the first 100");
|
||||
}
|
||||
|
||||
Customer[] lastBean = new Customer[1];
|
||||
DB.find(Customer.class).setLazyLoadBatchSize(1).findEach(customer -> {
|
||||
if (customer.getId() <= 100) {
|
||||
assertEquals("one of the first 100", customer.getSmallnote());
|
||||
// nested finds
|
||||
DB.find(Order.class).where().eq("customer", customer).findEach(20, consumer->{});
|
||||
} else {
|
||||
assertNotEquals("one of the first 100", customer.getSmallnote());
|
||||
}
|
||||
lastBean[0] = customer;
|
||||
});
|
||||
|
||||
SpiPersistenceContext pc = ((SpiTransaction) txn).getPersistenceContext();
|
||||
System.out.println(pc.toString()); // Customer=size:5000 (4900 weak), Order=size:100 (100 weak)
|
||||
|
||||
System.gc();
|
||||
Thread.sleep(100);
|
||||
pc.get(Customer.class, 1); // trigger expungeStaleEntries
|
||||
pc.get(Order.class, 1); // Checkme: Would it be a problem, that every classContext has its own queue
|
||||
System.out.println(pc.toString()); // Customer=size:101 (1 weak), no orders
|
||||
|
||||
first100 = DB.find(Customer.class).where().le("id", 100).findList();
|
||||
for (Customer c : first100) {
|
||||
assertEquals("one of the first 100", c.getSmallnote());
|
||||
}
|
||||
Customer lastBeanFromDb = DB.find(Customer.class).setId(lastBean[0].getId()).findOne();
|
||||
assertTrue(lastBeanFromDb == lastBean[0]);
|
||||
|
||||
// read 200
|
||||
DB.find(Customer.class).where().le("id", 200).findList();
|
||||
System.out.println(pc.toString()); // Customer=size:201 (1 weak)
|
||||
|
||||
lastBean[0] = null;
|
||||
lastBeanFromDb = null;
|
||||
System.gc();
|
||||
Thread.sleep(100);
|
||||
System.out.println(pc.toString()); // Customer=size:200 (0 weak)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user