[hibernate-commits] Hibernate SVN: r18936 - core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/lock.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Mar 8 16:18:03 EST 2010


Author: smarlow at redhat.com
Date: 2010-03-08 16:18:03 -0500 (Mon, 08 Mar 2010)
New Revision: 18936

Modified:
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/lock/LockTest.java
Log:
HHH-4972 javax.persistence.query.timeout and javax.persistence.lock.timeout can be passed when creating an EMF

Modified: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/lock/LockTest.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/lock/LockTest.java	2010-03-08 18:26:51 UTC (rev 18935)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/lock/LockTest.java	2010-03-08 21:18:03 UTC (rev 18936)
@@ -691,6 +691,92 @@
 	}
 
 
+	public void testLockTimeoutEMProps() throws Exception {
+
+		EntityManager em = getOrCreateEntityManager();
+		Map TimeoutProps = new HashMap();
+		TimeoutProps.put("javax.persistence.lock.timeout", new Integer(1000) ); // 1 second timeout
+		final EntityManager em2 = createIsolatedEntityManager(TimeoutProps);
+		// TODO:  replace dialect instanceof test with a Dialect.hasCapability (e.g. supportsPessimisticLockTimeout)
+		if ( ! (getDialect() instanceof Oracle10gDialect)) {
+			log.info("skipping testLockTimeoutEMProps");
+			return;
+		}
+		Lock lock = new Lock();
+		Thread t = null;
+		FutureTask<Boolean> bgTask = null;
+		final CountDownLatch latch = new CountDownLatch(1);
+		try {
+			lock.setName( "testLockTimeoutEMProps" );
+
+			em.getTransaction().begin();
+			em.persist( lock );
+			em.getTransaction().commit();
+			em.clear();
+
+			em.getTransaction().begin();
+			lock = em.getReference( Lock.class, lock.getId() );
+			em.lock( lock, LockModeType.PESSIMISTIC_WRITE );
+			final Integer id = lock.getId();
+			lock.getName();		// force entity to be read
+			log.info("testLockTimeoutEMProps: got write lock");
+
+			bgTask = new FutureTask<Boolean>( new Callable() {
+				public Boolean call() {
+					try {
+						boolean timedOut = false;	// true (success) if LockTimeoutException occurred
+						em2.getTransaction().begin();
+						log.info("testLockTimeoutEMProps: (BG) about to read write-locked entity");
+						// we should block on the following read
+						Lock lock2 = em2.getReference( Lock.class, id );
+						lock2.getName();		//  force entity to be read
+						log.info("testLockTimeoutEMProps: (BG) read write-locked entity");
+						// em2 already has javax.persistence.lock.timeout of 1 second applied
+						try {
+							em2.lock( lock2, LockModeType.PESSIMISTIC_WRITE);
+						}
+						catch( LockTimeoutException e) {
+							// success
+							log.info("testLockTimeoutEMProps: (BG) got expected timeout exception");
+							 timedOut = true;
+						}
+						catch ( Throwable e) {
+							log.info("Expected LockTimeoutException but got unexpected exception", e);
+						}
+						em2.getTransaction().commit();
+						return new Boolean(timedOut);
+					}
+					finally {
+						latch.countDown();	// signal that we finished
+					}
+				}
+			} );
+			t = new Thread(bgTask);
+			t.setDaemon( true );
+			t.setName("Lock timeout Test (bg)");
+			t.start();
+			boolean latchSet = latch.await( 10, TimeUnit.SECONDS );  // should return quickly on success
+			assertTrue( "background test thread finished (lock timeout is broken)", latchSet);
+			assertTrue( "background test thread timed out on lock attempt", bgTask.get().booleanValue() );
+			em.getTransaction().commit();
+		}
+		finally {
+			if ( em.getTransaction().isActive() ) {
+				em.getTransaction().rollback();
+			}
+			if ( t != null) {	  // wait for background thread to finish before deleting entity
+				t.join();
+			}
+			em.getTransaction().begin();
+			lock = em.getReference( Lock.class, lock.getId() );
+			em.remove( lock );
+			em.getTransaction().commit();
+			em.close();
+			em2.close();
+		}
+	}
+
+
 	public Class[] getAnnotatedClasses() {
 		return new Class[]{
 				Lock.class,



More information about the hibernate-commits mailing list