|
23 | 23 | */
|
24 | 24 | package org.hibernate.ejb.test.transaction;
|
25 | 25 |
|
26 |
| -import static org.junit.Assert.assertEquals; |
27 | 26 | import static org.junit.Assert.assertFalse;
|
28 | 27 | import static org.junit.Assert.assertTrue;
|
29 | 28 |
|
30 | 29 | import java.util.Map;
|
31 | 30 | import java.util.concurrent.CountDownLatch;
|
32 | 31 |
|
33 | 32 | import javax.persistence.EntityManager;
|
| 33 | +import javax.persistence.PersistenceException; |
34 | 34 | import javax.transaction.Status;
|
35 | 35 | import javax.transaction.Synchronization;
|
36 | 36 |
|
| 37 | +import org.hibernate.HibernateException; |
37 | 38 | import org.hibernate.Session;
|
38 | 39 | import org.hibernate.Transaction;
|
39 | 40 | import org.hibernate.ejb.AvailableSettings;
|
40 | 41 | import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
|
41 | 42 | import org.hibernate.engine.spi.SessionImplementor;
|
42 | 43 | import org.hibernate.engine.transaction.internal.jta.CMTTransaction;
|
43 | 44 | import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
|
| 45 | +import org.hibernate.exception.GenericJDBCException; |
44 | 46 | import org.hibernate.internal.SessionImpl;
|
45 | 47 | import org.hibernate.testing.TestForIssue;
|
46 | 48 | import org.hibernate.testing.jta.TestingJtaBootstrap;
|
@@ -186,32 +188,46 @@ public void afterCompletion( int i ) {
|
186 | 188 | * See HHH-7910
|
187 | 189 | */
|
188 | 190 | @Test
|
189 |
| - @TestForIssue(jiraKey="HHH-7910") |
| 191 | + @TestForIssue(jiraKey = "HHH-7910") |
190 | 192 | public void testMultiThreadTransactionTimeout() throws Exception {
|
191 | 193 | TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
|
192 |
| - |
| 194 | + |
193 | 195 | EntityManager em = entityManagerFactory().createEntityManager();
|
194 | 196 | final SessionImpl sImpl = em.unwrap( SessionImpl.class );
|
195 |
| - |
196 |
| - final CountDownLatch latch = new CountDownLatch(1); |
197 |
| - |
| 197 | + |
| 198 | + final CountDownLatch latch = new CountDownLatch( 1 ); |
| 199 | + |
198 | 200 | Thread thread = new Thread() {
|
199 | 201 | public void run() {
|
200 |
| - sImpl.getTransactionCoordinator().getSynchronizationCallbackCoordinator().afterCompletion( Status.STATUS_ROLLEDBACK ); |
| 202 | + sImpl.getTransactionCoordinator().getSynchronizationCallbackCoordinator() |
| 203 | + .afterCompletion( Status.STATUS_ROLLEDBACK ); |
201 | 204 | latch.countDown();
|
202 | 205 | }
|
203 | 206 | };
|
204 | 207 | thread.start();
|
205 |
| - |
| 208 | + |
206 | 209 | latch.await();
|
207 |
| - |
208 |
| - em.persist( new Book( "The Book of Foo", 1 ) ); |
209 |
| - |
210 |
| - // Ensure that the session was cleared by the background thread. |
211 |
| - assertEquals( "The background thread did not clear the session as expected!", |
212 |
| - 0, em.createQuery( "from Book" ).getResultList().size() ); |
213 | 210 |
|
214 |
| - TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); |
| 211 | + boolean caught = false; |
| 212 | + try { |
| 213 | + em.persist( new Book( "The Book of Foo", 1 ) ); |
| 214 | + } |
| 215 | + catch ( PersistenceException e ) { |
| 216 | + caught = e.getCause().getClass().equals( HibernateException.class ); |
| 217 | + } |
| 218 | + assertTrue( caught ); |
| 219 | + |
| 220 | + // Ensure that the connection was closed by the background thread. |
| 221 | + caught = false; |
| 222 | + try { |
| 223 | + em.createQuery( "from Book" ).getResultList(); |
| 224 | + } |
| 225 | + catch ( PersistenceException e ) { |
| 226 | + caught = e.getCause().getClass().equals( GenericJDBCException.class ); |
| 227 | + } |
| 228 | + assertTrue( caught ); |
| 229 | + |
| 230 | + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); |
215 | 231 | em.close();
|
216 | 232 | }
|
217 | 233 |
|
|
0 commit comments