Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » update query and stale cache
update query and stale cache [message #662935] Fri, 01 April 2011 14:14 Go to next message
anil chalil is currently offline anil chalil
Messages: 8
Registered: October 2010
Junior Member
Hi,

I have a repository function that enable/disable an entity here is the code:

@Override
	public int updateCrewMemberStatus(CrewMember crewMember, boolean status) {
		Query query = entityManager
				.createQuery("update CrewMember c set c.enabled=:status where c=:crewMember");
		query.setParameter(query.getParameter("crewMember", CrewMember.class),
				crewMember);
		query.setParameter(query.getParameter("status", Boolean.class), status);
		return  query.executeUpdate();
	}


and here is the test code:

@Test
	public void testupdateCrewMemberStatus() {
		Pilot pilot = createPilot();
		crewMemberRepository.save(pilot);
		crewMemberRepository.getEntityManager().flush();
		assertEquals(1,
				crewMemberRepository.updateCrewMemberStatus(pilot, false));
		crewMemberRepository.getEntityManager().flush();
		CrewMember crewMember = crewMemberRepository.findById(pilot.getId());
		Boolean queryForObject = simpleJdbcTemplate.queryForObject(
				"select enabled from CREWMEMBER where id=?", Boolean.class,
				crewMember.getId());
		LOGGER.debug(crewMember.toString() + " but queryObject: "
				+ queryForObject);
}


When i run test it gives me a crewmember object with enabled field "true". But queryForObject variable is "false"(crewmember entity ,i think,also have to have enabled field false since it is already updated). Here is the debug output:

17:12:34.052 [main] INFO o.s.t.c.t.TransactionalTestExecutionListener - Began transaction (3): transaction manager [org.springframework.orm.jpa.JpaTransactionManager@591bfcbe]; rollback [true]
[EL Fine]: 2011-04-01 17:12:34.052--ClientSession(37605958)--Connection(683143888) --Thread(Thread[main,5,main])--INSERT IGNORE INTO CREWMEMBER (ID, ENABLED, NAME, SURNAME, ADDRESS, EMAIL, PHONENO, CREW_TYPE) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
bind => [3, true, test, test, test, test@test.com, 12334, PILOT]
[EL Fine]: 2011-04-01 17:12:34.053--ClientSession(37605958)--Connection(683143888) --Thread(Thread[main,5,main])--INSERT IGNORE INTO PILOT (RANK, ID) VALUES (?, ?)
bind => [CAPTAIN, 3]
[EL Fine]: 2011-04-01 17:12:34.573--ClientSession(37605958)--Connection(683143888) --Thread(Thread[main,5,main])--UPDATE CREWMEMBER SET ENABLED = ? WHERE (? = ID)
bind => [false, 3]
[EL Fine]: 2011-04-01 17:12:34.576--ClientSession(37605958)--Connection(683143888) --Thread(Thread[main,5,main])--SELECT DISTINCT CREW_TYPE FROM CREWMEMBER WHERE (ID = ?)
bind => [3]
[EL Fine]: 2011-04-01 17:12:34.578--ClientSession(37605958)--Connection(683143888) --Thread(Thread[main,5,main])--SELECT t0.ID, t0.CREW_TYPE, t0.ENABLED, t0.NAME, t0.SURNAME, t0.ADDRESS, t0.EMAIL, t0.PHONENO, t1.ID, t1.RANK FROM CREWMEMBER t0, PILOT t1 WHERE ((t0.ID = ?) AND ((t1.ID = t0.ID) AND (t0.CREW_TYPE = ?)))
bind => [3, PILOT]
17:12:34.583 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
17:12:34.584 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [select enabled from CREWMEMBER where id=?]
17:12:34.589 [main] DEBUG m.b.m.i.p.CrewMemberRepositoryTest - CrewMember [id=3, name=test, surname=test, contactInfo=ContactInfo [email=test@test.com, phoneNo=12334, address=test], enabled=true] but queryObject: false

I could not understand the behaviour of the cache here. Why did't it update? Also if i put enetityManager.refresh in updateCrewMemberStatus before return then it is working. Any idea?
Re: update query and stale cache [message #663023 is a reply to message #662935] Sat, 02 April 2011 09:52 Go to previous message
anil chalil is currently offline anil chalil
Messages: 8
Registered: October 2010
Junior Member
ok problem solved. Bulk updates bypass the in memory structures of the persistence context. Since my example done in single transaction,L1 cache also references to stale entity in transaction boundaries. This is why it shows the correct values after entityManager.refresh called.
Previous Topic:ClassCastException on EntityManager.getCriteriaBuilder()
Next Topic:OptimisticLockException with delete
Goto Forum:
  


Current Time: Sun Oct 26 09:41:26 GMT 2014

Powered by FUDForum. Page generated in 0.03778 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software