If my entity classes have synchronized getters and setters, is it reliable for multiple threads to access a single EM's managed entities, as long as only the thread that created the EM interacts directly with it? I've tested this and it seems to work... but I know better than to depend on the observable behavior of multithreaded code.
When I run this example and examine the database, the entity contains the value set by the second thread, as expected.
import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class ODBMultiThread { public static void main(String[] args) throws InterruptedException { EntityManagerFactory emf = Persistence.createEntityManagerFactory( "objectdb:/home/kevin/threads-test.odb"); EntityManager em = emf.createEntityManager(); final PointlessEntity pointless = new PointlessEntity(69); em.getTransaction().begin(); em.persist(pointless); em.getTransaction().commit(); em.getTransaction().begin(); Thread t = new Thread(new Runnable() { @Override public void run() { pointless.setValue(42); } }); t.start(); t.join(); em.getTransaction().commit(); // commit t's changes em.close(); emf.close(); } @Entity static class PointlessEntity { private int value; public PointlessEntity(int value) { this.value = value; } synchronized public int getValue() { return value; } synchronized public void setValue(int value) { this.value = value; } } }