Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] J2SE: Correct way to discard changes to a transaction?

We've been working on our SWT/RCP CRUD application and are running
into a strange anomaly. Example:
*Open Editor (this creates a new model & hence a new EntityManager)
*Change Field Value: this calls, merge on our EntityManager
*Editor is marked dirty, since em.getUnitOfWork().hasChanges() returns true.
*Close editor, do not save changes.
*Open up same record again in a new editor, my changes are still there
and em.getUnitOfWork().hasChanges() returns false.

Obviously there is some sort of caching going on, so what is the
correct way to discard changes to my transaction?
Backup Code:
Creating my EntityManagerFactory:
properties.put(PersistenceUnitProperties.TARGET_DATABASE, "Derby");
		properties.put(PersistenceUnitProperties.JDBC_DRIVER,
"org.apache.derby.jdbc.ClientDriver");
		properties.put(PersistenceUnitProperties.JDBC_URL,
"jdbc:derby://localhost:1527/sample;create=true");
		properties.put(PersistenceUnitProperties.JDBC_USER, "app");
		properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "app");
		properties.put(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MIN, "1");
		properties.put(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MIN, "1");
		properties.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT, "false");
		properties.put(PersistenceUnitProperties.BATCH_WRITING, "JDBC");
		
		properties.put("eclipselink.logging.level", "FINE");
		properties.put("eclipselink.logging.timestamp", "false");
		properties.put("eclipselink.logging.session", "false");
		properties.put("eclipselink.logging.thread", "false");
		
		emf = new PersistenceProvider().createEntityManagerFactory("comics",
properties, ComicsEntityManagerFactory.class.getClassLoader());
	}
	


The "Model" that is created when an editor is opened:

public class PublisherEditorModel {
	private EntityManager em;
	private EntityTransaction transaction;
	
	public PublisherEditorModel() {
		em = ComicsEntityManagerFactory.createEntityManager();
		transaction = em.getTransaction();
		transaction.begin();
	}
	
	public void close() {
		transaction.rollback();
		em.close();
	}
	
	public void updatePublisher(Publisher publisher) {
		em.merge(publisher);
	}
	
	public void updateTitle(Title title) {
		em.merge(title);
	}
	
	public void save() {
		transaction.commit();
	}

	public boolean isDirty() {
		UnitOfWork uow = ((EntityManagerImpl)em).getUnitOfWork();
		return uow.hasChanges();
	}
}


-- 
./tch


Back to the top