return;
}
String check = "Lock Modes";
Session s1 = getSessions().openSession();
Transaction t1 = s1.beginTransaction();
Part part = new Part( new Item( "EJB3 Specification" ), check, "3.3.5.3", new BigDecimal( 0.0 ) );
s1.save( part );
t1.commit();
s1.close();
Long partId = part.getId();
// Now, open a new Session and re-load the part...
s1 = getSessions().openSession();
t1 = s1.beginTransaction();
part = ( Part ) s1.get( Part.class, partId );
// now that the item is associated with the persistence-context of that session,
// open a new session and modify it "behind the back" of the first session
Session s2 = getSessions().openSession();
Transaction t2 = s2.beginTransaction();
Part part2 = ( Part ) s2.get( Part.class, partId );
part2.setName( "Lock Mode Types" );
t2.commit();
s2.close();
// at this point, s1 now contains stale data, so acquire a READ lock
// and make sure we get the already associated state (i.e., the old
// name and the old version)
s1.lock( part, LockMode.READ );
part2 = ( Part ) s1.get( Part.class, partId );
assertTrue( part == part2 );
assertEquals( "encountered non-repeatable read", check, part2.getName() );
// then acquire an UPGRADE lock; this should fail
try {
s1.lock( part, LockMode.UPGRADE );
}
catch( Throwable t ) {
// SQLServer, for example, immediately throws an exception here...
t1.rollback();
t1 = s1.beginTransaction();
}
part2 = ( Part ) s1.get( Part.class, partId );
assertTrue( part == part2 );
assertEquals( "encountered non-repeatable read", check, part2.getName() );
t1.commit();
s1.close();
// clean up