Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
134 additions
and
0 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
...r/src/test/java/org/hibernate/jpa/test/lock/StatementIsClosedAfterALockExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.jpa.test.lock; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.LockModeType; | ||
|
||
import org.hibernate.jpa.AvailableSettings; | ||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; | ||
|
||
import org.hibernate.testing.DialectChecks; | ||
import org.hibernate.testing.RequiresDialectFeature; | ||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.test.util.jdbc.BasicPreparedStatementObserver; | ||
import org.hibernate.test.util.jdbc.PreparedStatementObserver; | ||
import org.hibernate.test.util.jdbc.PreparedStatementProxyConnectionProvider; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* @author Andrea Boriero | ||
*/ | ||
public class StatementIsClosedAfterALockExceptionTest extends BaseEntityManagerFunctionalTestCase { | ||
|
||
private static final PreparedStatementObserver PREPARED_STATEMENT_OBSERVER = new BasicPreparedStatementObserver(); | ||
private static final PreparedStatementProxyConnectionProvider CONNECTION_PROVIDER = new PreparedStatementProxyConnectionProvider( | ||
PREPARED_STATEMENT_OBSERVER | ||
); | ||
|
||
private Integer lockId; | ||
|
||
@Override | ||
protected Map getConfig() { | ||
Map config = super.getConfig(); | ||
config.put( | ||
org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, | ||
CONNECTION_PROVIDER | ||
); | ||
return config; | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
EntityManager entityManager = getOrCreateEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
{ | ||
Lock lock = new Lock(); | ||
lock.setName( "name" ); | ||
entityManager.persist( lock ); | ||
lockId = lock.getId(); | ||
} | ||
entityManager.getTransaction().commit(); | ||
entityManager.close(); | ||
} | ||
|
||
@Override | ||
public void releaseResources() { | ||
super.releaseResources(); | ||
CONNECTION_PROVIDER.stop(); | ||
} | ||
|
||
@Test(timeout = 5 * 4000) // 20 sec | ||
@TestForIssue(jiraKey = "HHH-11617") | ||
@RequiresDialectFeature(value = DialectChecks.SupportsLockTimeouts.class, | ||
comment = "Test verifies statement is closed after a lock excpetion.", | ||
jiraKey = "HHH-11617") | ||
public void testStatementIsClosed() { | ||
|
||
EntityManager entityManager = getOrCreateEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
{ | ||
Map<String, Object> properties = new HashMap<String, Object>(); | ||
properties.put( AvailableSettings.LOCK_TIMEOUT, 0L ); | ||
Lock lock2 = entityManager.find( Lock.class, lockId, LockModeType.PESSIMISTIC_WRITE, properties ); | ||
assertEquals( | ||
"lock mode should be PESSIMISTIC_WRITE ", | ||
LockModeType.PESSIMISTIC_WRITE, | ||
entityManager.getLockMode( lock2 ) | ||
); | ||
|
||
EntityManager entityManager2 = createEntityManager(); | ||
entityManager2.getTransaction().begin(); | ||
{ | ||
try { | ||
entityManager2.find( Lock.class, lockId, LockModeType.PESSIMISTIC_WRITE, properties ); | ||
fail( "Exception should be thrown" ); | ||
} | ||
catch (Exception lte) { | ||
// Proper exception thrown for dialect supporting lock timeouts when an immediate timeout is set. | ||
lte.getCause(); | ||
} | ||
finally { | ||
try { | ||
for ( PreparedStatement statement : PREPARED_STATEMENT_OBSERVER.getPreparedStatements() ) { | ||
assertThat( | ||
"A SQL Statement was not closed : " + statement.toString(), | ||
statement.isClosed(), | ||
is( true ) | ||
); | ||
} | ||
} | ||
catch (SQLException e) { | ||
fail( e.getMessage() ); | ||
} | ||
} | ||
} | ||
entityManager2.getTransaction().commit(); | ||
entityManager2.close(); | ||
} | ||
entityManager.getTransaction().commit(); | ||
entityManager.close(); | ||
} | ||
|
||
@Override | ||
public Class[] getAnnotatedClasses() { | ||
return new Class[] { | ||
Lock.class, | ||
UnversionedLock.class | ||
}; | ||
} | ||
} |