Fix code style issues
This commit is contained in:
parent
406a82616b
commit
ffa6212b40
|
@ -24,10 +24,12 @@ public class TransactionUtil {
|
|||
|
||||
/**
|
||||
* Hibernate transaction function
|
||||
*
|
||||
* @param <T> function result
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface HibernateTransactionFunction<T> extends Function<Session, T> {
|
||||
public interface HibernateTransactionFunction<T>
|
||||
extends Function<Session, T> {
|
||||
/**
|
||||
* Before transaction completion function
|
||||
*/
|
||||
|
@ -65,10 +67,12 @@ public class TransactionUtil {
|
|||
|
||||
/**
|
||||
* JPA transaction function
|
||||
*
|
||||
* @param <T> function result
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JPATransactionFunction<T> extends Function<EntityManager, T> {
|
||||
public interface JPATransactionFunction<T>
|
||||
extends Function<EntityManager, T> {
|
||||
/**
|
||||
* Before transaction completion function
|
||||
*/
|
||||
|
@ -88,7 +92,8 @@ public class TransactionUtil {
|
|||
* JPA transaction function without return value
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface JPATransactionVoidFunction extends Consumer<EntityManager> {
|
||||
public interface JPATransactionVoidFunction
|
||||
extends Consumer<EntityManager> {
|
||||
/**
|
||||
* Before transaction completion function
|
||||
*/
|
||||
|
@ -106,12 +111,16 @@ public class TransactionUtil {
|
|||
|
||||
/**
|
||||
* 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) {
|
||||
public static <T> T doInJPA(
|
||||
Supplier<EntityManagerFactory> factorySupplier,
|
||||
JPATransactionFunction<T> function) {
|
||||
T result = null;
|
||||
EntityManager entityManager = null;
|
||||
EntityTransaction txn = null;
|
||||
|
@ -122,10 +131,14 @@ public class TransactionUtil {
|
|||
txn.begin();
|
||||
result = function.apply( entityManager );
|
||||
txn.commit();
|
||||
} catch (Throwable e) {
|
||||
if ( txn != null && txn.isActive()) txn.rollback();
|
||||
}
|
||||
catch ( Throwable e ) {
|
||||
if ( txn != null && txn.isActive() ) {
|
||||
txn.rollback();
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
function.afterTransactionCompletion();
|
||||
if ( entityManager != null ) {
|
||||
entityManager.close();
|
||||
|
@ -136,10 +149,13 @@ public class TransactionUtil {
|
|||
|
||||
/**
|
||||
* Execute function in a JPA transaction without return value
|
||||
*
|
||||
* @param factorySupplier EntityManagerFactory supplier
|
||||
* @param function function
|
||||
*/
|
||||
public static void doInJPA(Supplier<EntityManagerFactory> factorySupplier, JPATransactionVoidFunction function) {
|
||||
public static void doInJPA(
|
||||
Supplier<EntityManagerFactory> factorySupplier,
|
||||
JPATransactionVoidFunction function) {
|
||||
EntityManager entityManager = null;
|
||||
EntityTransaction txn = null;
|
||||
try {
|
||||
|
@ -149,10 +165,14 @@ public class TransactionUtil {
|
|||
txn.begin();
|
||||
function.accept( entityManager );
|
||||
txn.commit();
|
||||
} catch (Throwable e) {
|
||||
if ( txn != null && txn.isActive()) txn.rollback();
|
||||
}
|
||||
catch ( Throwable e ) {
|
||||
if ( txn != null && txn.isActive() ) {
|
||||
txn.rollback();
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
function.afterTransactionCompletion();
|
||||
if ( entityManager != null ) {
|
||||
entityManager.close();
|
||||
|
@ -162,12 +182,16 @@ public class TransactionUtil {
|
|||
|
||||
/**
|
||||
* Execute function in a Hibernate transaction
|
||||
*
|
||||
* @param factorySupplier EntityManagerFactory supplier
|
||||
* @param function function
|
||||
* @param <T> result type
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
public static <T> T doInHibernate(Supplier<SessionFactory> factorySupplier, HibernateTransactionFunction<T> function) {
|
||||
public static <T> T doInHibernate(
|
||||
Supplier<SessionFactory> factorySupplier,
|
||||
HibernateTransactionFunction<T> function) {
|
||||
T result = null;
|
||||
Session session = null;
|
||||
Transaction txn = null;
|
||||
|
@ -178,10 +202,14 @@ public class TransactionUtil {
|
|||
|
||||
result = function.apply( session );
|
||||
txn.commit();
|
||||
} catch (Throwable e) {
|
||||
if ( txn != null ) txn.rollback();
|
||||
}
|
||||
catch ( Throwable e ) {
|
||||
if ( txn != null ) {
|
||||
txn.rollback();
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
function.afterTransactionCompletion();
|
||||
if ( session != null ) {
|
||||
session.close();
|
||||
|
@ -192,10 +220,13 @@ public class TransactionUtil {
|
|||
|
||||
/**
|
||||
* Execute function in a JPA transaction without return value
|
||||
*
|
||||
* @param factorySupplier EntityManagerFactory supplier
|
||||
* @param function function
|
||||
*/
|
||||
public static void doInHibernate(Supplier<SessionFactory> factorySupplier, HibernateTransactionConsumer function) {
|
||||
public static void doInHibernate(
|
||||
Supplier<SessionFactory> factorySupplier,
|
||||
HibernateTransactionConsumer function) {
|
||||
Session session = null;
|
||||
Transaction txn = null;
|
||||
try {
|
||||
|
@ -205,10 +236,14 @@ public class TransactionUtil {
|
|||
|
||||
function.accept( session );
|
||||
txn.commit();
|
||||
} catch (Throwable e) {
|
||||
if ( txn != null ) txn.rollback();
|
||||
}
|
||||
catch ( Throwable e ) {
|
||||
if ( txn != null ) {
|
||||
txn.rollback();
|
||||
}
|
||||
throw e;
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
function.afterTransactionCompletion();
|
||||
if ( session != null ) {
|
||||
session.close();
|
||||
|
|
Loading…
Reference in New Issue