Fix code style issues
This commit is contained in:
parent
406a82616b
commit
ffa6212b40
|
@ -22,197 +22,232 @@ import org.hibernate.Transaction;
|
||||||
*/
|
*/
|
||||||
public class TransactionUtil {
|
public class TransactionUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hibernate transaction function
|
* Hibernate transaction function
|
||||||
* @param <T> function result
|
*
|
||||||
*/
|
* @param <T> function result
|
||||||
@FunctionalInterface
|
*/
|
||||||
public interface HibernateTransactionFunction<T> extends Function<Session, T> {
|
@FunctionalInterface
|
||||||
/**
|
public interface HibernateTransactionFunction<T>
|
||||||
* Before transaction completion function
|
extends Function<Session, T> {
|
||||||
*/
|
/**
|
||||||
default void beforeTransactionCompletion() {
|
* Before transaction completion function
|
||||||
|
*/
|
||||||
|
default void beforeTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* After transaction completion function
|
* After transaction completion function
|
||||||
*/
|
*/
|
||||||
default void afterTransactionCompletion() {
|
default void afterTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hibernate transaction function without return value
|
* Hibernate transaction function without return value
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface HibernateTransactionConsumer extends Consumer<Session> {
|
public interface HibernateTransactionConsumer extends Consumer<Session> {
|
||||||
/**
|
/**
|
||||||
* Before transaction completion function
|
* Before transaction completion function
|
||||||
*/
|
*/
|
||||||
default void beforeTransactionCompletion() {
|
default void beforeTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* After transaction completion function
|
* After transaction completion function
|
||||||
*/
|
*/
|
||||||
default void afterTransactionCompletion() {
|
default void afterTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JPA transaction function
|
* JPA transaction function
|
||||||
* @param <T> function result
|
*
|
||||||
*/
|
* @param <T> function result
|
||||||
@FunctionalInterface
|
*/
|
||||||
public interface JPATransactionFunction<T> extends Function<EntityManager, T> {
|
@FunctionalInterface
|
||||||
/**
|
public interface JPATransactionFunction<T>
|
||||||
* Before transaction completion function
|
extends Function<EntityManager, T> {
|
||||||
*/
|
/**
|
||||||
default void beforeTransactionCompletion() {
|
* Before transaction completion function
|
||||||
|
*/
|
||||||
|
default void beforeTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* After transaction completion function
|
* After transaction completion function
|
||||||
*/
|
*/
|
||||||
default void afterTransactionCompletion() {
|
default void afterTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JPA transaction function without return value
|
* JPA transaction function without return value
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface JPATransactionVoidFunction extends Consumer<EntityManager> {
|
public interface JPATransactionVoidFunction
|
||||||
/**
|
extends Consumer<EntityManager> {
|
||||||
* Before transaction completion function
|
/**
|
||||||
*/
|
* Before transaction completion function
|
||||||
default void beforeTransactionCompletion() {
|
*/
|
||||||
|
default void beforeTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* After transaction completion function
|
* After transaction completion function
|
||||||
*/
|
*/
|
||||||
default void afterTransactionCompletion() {
|
default void afterTransactionCompletion() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute function in a JPA transaction
|
* Execute function in a JPA transaction
|
||||||
* @param factorySupplier EntityManagerFactory supplier
|
*
|
||||||
* @param function function
|
* @param factorySupplier EntityManagerFactory supplier
|
||||||
* @param <T> result type
|
* @param function function
|
||||||
* @return result
|
* @param <T> result type
|
||||||
*/
|
*
|
||||||
public static <T> T doInJPA(Supplier<EntityManagerFactory> factorySupplier, JPATransactionFunction<T> function) {
|
* @return result
|
||||||
T result = null;
|
*/
|
||||||
EntityManager entityManager = null;
|
public static <T> T doInJPA(
|
||||||
EntityTransaction txn = null;
|
Supplier<EntityManagerFactory> factorySupplier,
|
||||||
try {
|
JPATransactionFunction<T> function) {
|
||||||
entityManager = factorySupplier.get().createEntityManager();
|
T result = null;
|
||||||
function.beforeTransactionCompletion();
|
EntityManager entityManager = null;
|
||||||
txn = entityManager.getTransaction();
|
EntityTransaction txn = null;
|
||||||
txn.begin();
|
try {
|
||||||
result = function.apply(entityManager);
|
entityManager = factorySupplier.get().createEntityManager();
|
||||||
txn.commit();
|
function.beforeTransactionCompletion();
|
||||||
} catch (Throwable e) {
|
txn = entityManager.getTransaction();
|
||||||
if ( txn != null && txn.isActive()) txn.rollback();
|
txn.begin();
|
||||||
throw e;
|
result = function.apply( entityManager );
|
||||||
} finally {
|
txn.commit();
|
||||||
function.afterTransactionCompletion();
|
}
|
||||||
if (entityManager != null) {
|
catch ( Throwable e ) {
|
||||||
entityManager.close();
|
if ( txn != null && txn.isActive() ) {
|
||||||
}
|
txn.rollback();
|
||||||
}
|
}
|
||||||
return result;
|
throw e;
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
function.afterTransactionCompletion();
|
||||||
|
if ( entityManager != null ) {
|
||||||
|
entityManager.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute function in a JPA transaction without return value
|
* Execute function in a JPA transaction without return value
|
||||||
* @param factorySupplier EntityManagerFactory supplier
|
*
|
||||||
* @param function function
|
* @param factorySupplier EntityManagerFactory supplier
|
||||||
*/
|
* @param function function
|
||||||
public static void doInJPA(Supplier<EntityManagerFactory> factorySupplier, JPATransactionVoidFunction function) {
|
*/
|
||||||
EntityManager entityManager = null;
|
public static void doInJPA(
|
||||||
EntityTransaction txn = null;
|
Supplier<EntityManagerFactory> factorySupplier,
|
||||||
try {
|
JPATransactionVoidFunction function) {
|
||||||
entityManager = factorySupplier.get().createEntityManager();
|
EntityManager entityManager = null;
|
||||||
function.beforeTransactionCompletion();
|
EntityTransaction txn = null;
|
||||||
txn = entityManager.getTransaction();
|
try {
|
||||||
txn.begin();
|
entityManager = factorySupplier.get().createEntityManager();
|
||||||
function.accept(entityManager);
|
function.beforeTransactionCompletion();
|
||||||
txn.commit();
|
txn = entityManager.getTransaction();
|
||||||
} catch (Throwable e) {
|
txn.begin();
|
||||||
if ( txn != null && txn.isActive()) txn.rollback();
|
function.accept( entityManager );
|
||||||
throw e;
|
txn.commit();
|
||||||
} finally {
|
}
|
||||||
function.afterTransactionCompletion();
|
catch ( Throwable e ) {
|
||||||
if (entityManager != null) {
|
if ( txn != null && txn.isActive() ) {
|
||||||
entityManager.close();
|
txn.rollback();
|
||||||
}
|
}
|
||||||
}
|
throw e;
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
function.afterTransactionCompletion();
|
||||||
|
if ( entityManager != null ) {
|
||||||
|
entityManager.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute function in a Hibernate transaction
|
* Execute function in a Hibernate transaction
|
||||||
* @param factorySupplier EntityManagerFactory supplier
|
*
|
||||||
* @param function function
|
* @param factorySupplier EntityManagerFactory supplier
|
||||||
* @param <T> result type
|
* @param function function
|
||||||
* @return result
|
* @param <T> result type
|
||||||
*/
|
*
|
||||||
public static <T> T doInHibernate(Supplier<SessionFactory> factorySupplier, HibernateTransactionFunction<T> function) {
|
* @return result
|
||||||
T result = null;
|
*/
|
||||||
Session session = null;
|
public static <T> T doInHibernate(
|
||||||
Transaction txn = null;
|
Supplier<SessionFactory> factorySupplier,
|
||||||
try {
|
HibernateTransactionFunction<T> function) {
|
||||||
session = factorySupplier.get().openSession();
|
T result = null;
|
||||||
function.beforeTransactionCompletion();
|
Session session = null;
|
||||||
txn = session.beginTransaction();
|
Transaction txn = null;
|
||||||
|
try {
|
||||||
|
session = factorySupplier.get().openSession();
|
||||||
|
function.beforeTransactionCompletion();
|
||||||
|
txn = session.beginTransaction();
|
||||||
|
|
||||||
result = function.apply(session);
|
result = function.apply( session );
|
||||||
txn.commit();
|
txn.commit();
|
||||||
} catch (Throwable e) {
|
}
|
||||||
if ( txn != null ) txn.rollback();
|
catch ( Throwable e ) {
|
||||||
throw e;
|
if ( txn != null ) {
|
||||||
} finally {
|
txn.rollback();
|
||||||
function.afterTransactionCompletion();
|
}
|
||||||
if (session != null) {
|
throw e;
|
||||||
session.close();
|
}
|
||||||
}
|
finally {
|
||||||
}
|
function.afterTransactionCompletion();
|
||||||
return result;
|
if ( session != null ) {
|
||||||
}
|
session.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute function in a JPA transaction without return value
|
* Execute function in a JPA transaction without return value
|
||||||
* @param factorySupplier EntityManagerFactory supplier
|
*
|
||||||
* @param function function
|
* @param factorySupplier EntityManagerFactory supplier
|
||||||
*/
|
* @param function function
|
||||||
public static void doInHibernate(Supplier<SessionFactory> factorySupplier, HibernateTransactionConsumer function) {
|
*/
|
||||||
Session session = null;
|
public static void doInHibernate(
|
||||||
Transaction txn = null;
|
Supplier<SessionFactory> factorySupplier,
|
||||||
try {
|
HibernateTransactionConsumer function) {
|
||||||
session = factorySupplier.get().openSession();
|
Session session = null;
|
||||||
function.beforeTransactionCompletion();
|
Transaction txn = null;
|
||||||
txn = session.beginTransaction();
|
try {
|
||||||
|
session = factorySupplier.get().openSession();
|
||||||
|
function.beforeTransactionCompletion();
|
||||||
|
txn = session.beginTransaction();
|
||||||
|
|
||||||
function.accept(session);
|
function.accept( session );
|
||||||
txn.commit();
|
txn.commit();
|
||||||
} catch (Throwable e) {
|
}
|
||||||
if ( txn != null ) txn.rollback();
|
catch ( Throwable e ) {
|
||||||
throw e;
|
if ( txn != null ) {
|
||||||
} finally {
|
txn.rollback();
|
||||||
function.afterTransactionCompletion();
|
}
|
||||||
if (session != null) {
|
throw e;
|
||||||
session.close();
|
}
|
||||||
}
|
finally {
|
||||||
}
|
function.afterTransactionCompletion();
|
||||||
}
|
if ( session != null ) {
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue