HHH-11616 - Refactor org.hibernate.jpa.test.lock.LockTest

HHH-8001 - Apply query timeouts to Oracle follow-on locking
This commit is contained in:
Vlad Mihalcea 2017-04-06 11:59:14 +03:00
parent 68fa5d8f9e
commit c15fa77f0f
3 changed files with 888 additions and 834 deletions

View File

@ -7,6 +7,8 @@
package org.hibernate.testing.transaction;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.function.Function;
@ -126,18 +128,22 @@ public class TransactionUtil {
*
* @param factorySupplier EntityManagerFactory supplier
* @param function function
* @param properties properties for entity manager bootstrapping
* @param <T> result type
*
* @return result
*/
public static <T> T doInJPA(
Supplier<EntityManagerFactory> factorySupplier,
JPATransactionFunction<T> function) {
JPATransactionFunction<T> function,
Map properties) {
T result = null;
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
entityManager = factorySupplier.get().createEntityManager();
entityManager = properties == null ?
factorySupplier.get().createEntityManager():
factorySupplier.get().createEntityManager(properties);
function.beforeTransactionCompletion();
txn = entityManager.getTransaction();
txn.begin();
@ -159,6 +165,73 @@ public class TransactionUtil {
return result;
}
/**
* Execute function in a JPA transaction
*
* @param factorySupplier EntityManagerFactory supplier
* @param function function
* @param <T> result type
*
* @return result
*/
public static <T> T doInJPA(
Supplier<EntityManagerFactory> factorySupplier,
JPATransactionFunction<T> function) {
return doInJPA( factorySupplier, function, null );
}
/**
* Execute function in a JPA transaction without return value
*
* @param factorySupplier EntityManagerFactory supplier
* @param function function
* @param properties properties for entity manager bootstrapping
*/
public static void doInJPA(
Supplier<EntityManagerFactory> factorySupplier,
JPATransactionVoidFunction function,
Map properties) {
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
entityManager = properties == null ?
factorySupplier.get().createEntityManager():
factorySupplier.get().createEntityManager(properties);
function.beforeTransactionCompletion();
txn = entityManager.getTransaction();
txn.begin();
function.accept( entityManager );
if ( !txn.getRollbackOnly() ) {
txn.commit();
}
else {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
}
catch ( Throwable t ) {
if ( txn != null && txn.isActive() ) {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
throw t;
}
finally {
function.afterTransactionCompletion();
if ( entityManager != null ) {
entityManager.close();
}
}
}
/**
* Execute function in a JPA transaction without return value
*
@ -168,28 +241,7 @@ public class TransactionUtil {
public static void doInJPA(
Supplier<EntityManagerFactory> factorySupplier,
JPATransactionVoidFunction function) {
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
entityManager = factorySupplier.get().createEntityManager();
function.beforeTransactionCompletion();
txn = entityManager.getTransaction();
txn.begin();
function.accept( entityManager );
txn.commit();
}
catch ( Throwable e ) {
if ( txn != null && txn.isActive() ) {
txn.rollback();
}
throw e;
}
finally {
function.afterTransactionCompletion();
if ( entityManager != null ) {
entityManager.close();
}
}
doInJPA( factorySupplier, function, null );
}
/**
@ -213,13 +265,28 @@ public class TransactionUtil {
txn = session.beginTransaction();
result = function.apply( session );
txn.commit();
}
catch ( Throwable e ) {
if ( txn != null ) {
txn.rollback();
if ( !txn.getRollbackOnly() ) {
txn.commit();
}
throw e;
else {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
}
catch ( Throwable t ) {
if ( txn != null && txn.isActive() ) {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
throw t;
}
finally {
function.afterTransactionCompletion();
@ -247,13 +314,28 @@ public class TransactionUtil {
txn = session.beginTransaction();
function.accept( session );
txn.commit();
}
catch ( Throwable e ) {
if ( txn != null ) {
txn.rollback();
if ( !txn.getRollbackOnly() ) {
txn.commit();
}
throw e;
else {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
}
catch ( Throwable t ) {
if ( txn != null && txn.isActive() ) {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
throw t;
}
finally {
function.afterTransactionCompletion();
@ -284,13 +366,28 @@ public class TransactionUtil {
txn = session.beginTransaction();
result = function.apply( session );
txn.commit();
}
catch ( Throwable e ) {
if ( txn != null ) {
txn.rollback();
if ( !txn.getRollbackOnly() ) {
txn.commit();
}
throw e;
else {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
}
catch ( Throwable t ) {
if ( txn != null && txn.isActive() ) {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
throw t;
}
finally {
function.afterTransactionCompletion();
@ -318,13 +415,28 @@ public class TransactionUtil {
txn = session.beginTransaction();
function.accept( session );
txn.commit();
}
catch ( Throwable e ) {
if ( txn != null ) {
txn.rollback();
if ( !txn.getRollbackOnly() ) {
txn.commit();
}
throw e;
else {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
}
catch ( Throwable t ) {
if ( txn != null && txn.isActive() ) {
try {
txn.rollback();
}
catch (Exception e) {
log.error( "Rollback failure", e );
}
}
throw t;
}
finally {
function.afterTransactionCompletion();

View File

@ -82,7 +82,8 @@ public class ExceptionUtil {
public static boolean isConnectionClose(Exception e) {
Throwable rootCause = ExceptionUtil.rootCause( e );
if ( rootCause != null && (
rootCause.getMessage().toLowerCase().contains( "connection is close" )
rootCause.getMessage().toLowerCase().contains( "connection is close" ) ||
rootCause.getMessage().toLowerCase().contains( "closed connection" )
) ) {
return true;
}