@Override public Object invoke(MethodInvocation invocation) throws Throwable { if (null == didWeStartWork.get()) { unitOfWork.begin(); didWeStartWork.set(Boolean.TRUE); } else { // If unit of work already started we don't do anything here... // another UnitOfWorkInterceptor point point will take care... // This happens if you are nesting your calls. return invocation.proceed(); } try { return invocation.proceed(); } finally { if (null != didWeStartWork.get()) { didWeStartWork.remove(); unitOfWork.end(); } } }
public static EntityManager getEnityManager() { try { beginUnitOfWork(); return entityManagerProvider.get(); } finally { unitOfWork.end(); } }
public static void beginUnitOfWork() { //try { unitOfWork.begin(); //} catch (IllegalStateException ex) { // unitOfWork.end(); //unitOfWork.begin(); // } }
public static <T> T find(Class<T> resultType, UUID id) { try { beginUnitOfWork(); T result = em().find(resultType, id); return result; } finally { unitOfWork.end(); } }
public void start() { if(started.get()) throw new RuntimeException("transaction already started"); started.set(true); unitOfWork.begin(); emp.get().getTransaction().begin(); log.trace("Jpa transaction started"); }
public static <T> T queryZeroOne(Class<T> resultType, String queryStr, Object... params) { try { beginUnitOfWork(); final TypedQuery<T> query = em().createQuery(queryStr, resultType); if (params != null) { for (int i = 0; i < params.length; i++) { Object param = params[i]; query.setParameter(i + 1, param); // JPA uses 1-based indexes } } T result = query.getSingleResult(); return result; } catch (NoResultException x) { return null; } finally { unitOfWork.end(); // if (em != null) // em.close(); } }
public static <T> List<T> queryList(Class<T> resultType, String queryStr, Object... params) { // EntityManager em = em(); try { beginUnitOfWork(); final TypedQuery<T> query = em().createQuery(queryStr, resultType); if (params != null) { for (int i = 0; i < params.length; i++) { Object param = params[i]; query.setParameter(i + 1, param); // JPA uses 1-based indexes } } List<T> result = query.getResultList(); return result; } catch (TransientObjectException toe) { log.debug("what happened"); return null; } finally { unitOfWork.end(); // em.clear(); // if (em() != null) // em().close(); } }
public static <T> T sqlQueryOne(Class<T> resultType, String queryStr, Object... params) throws NoResultException { // EntityManager em = em(); T result = null; try { beginUnitOfWork(); log.debug("sqlQueryOne unit of work started for thread: " + Thread.currentThread()); TypedQuery<T> query = em().createQuery(queryStr, resultType); if (params != null) { for (int i = 0; i < params.length; i++) { Object param = params[i]; query.setParameter(i + 1, param); // JPA uses 1-based indexes } } result = query.getSingleResult(); return result; } catch (NoResultException e) { // context.getInjector().getInstance(Portfolio.class); // PersistUtil.insert(portfolio); throw e; //return null; } finally { unitOfWork.end(); log.trace("sqlQueryOne unit of work ended for thread: " + Thread.currentThread()); } // em.flush(); // if (em != null) //em.close(); // } }
public static <T> T namedQueryZeroOne(Class<T> resultType, String namedQuery, Object... params) { try { beginUnitOfWork(); log.trace("namedQueryZeroOne unit of work ended for thread: " + Thread.currentThread()); TypedQuery<T> query = em().createNamedQuery(namedQuery, resultType); if (params != null) { for (int i = 0; i < params.length; i++) { Object param = params[i]; query.setParameter(i + 1, param); // JPA uses 1-based indexes } } T result = query.setHint("org.hibernate.cacheable", true).getSingleResult(); return result; } catch (NoResultException x) { return null; } finally { unitOfWork.end(); log.trace("namedQueryZeroOne unit of work ended for thread: " + Thread.currentThread()); // if (em() != null) // em().close(); } }
@Override public void createAll(Collection<? extends T> entities) throws TckRepositoryException { uow.begin(); final EntityManager manager = managerProvider.get(); try { manager.getTransaction().begin(); for (T entity : entities) { manager.persist(entity); manager.flush(); } manager.getTransaction().commit(); } catch (RuntimeException x) { if (manager.getTransaction().isActive()) { manager.getTransaction().rollback(); } throw new TckRepositoryException(x.getLocalizedMessage(), x); } finally { uow.end(); } }
public static <T> List<T> namedQueryList(Class<T> resultType, String namedQuery, Object... params) { try { beginUnitOfWork(); log.trace("namedQueryList unit of work ended for thread: " + Thread.currentThread()); TypedQuery<T> query = em().createNamedQuery(namedQuery, resultType); if (params != null) { for (int i = 0; i < params.length; i++) { Object param = params[i]; query.setParameter(i + 1, param); // JPA uses 1-based indexes } } List<T> result = query.setHint("org.hibernate.cacheable", true).getResultList(); return result; } catch (NoResultException x) { return null; } finally { unitOfWork.end(); log.trace("namedQueryList unit of work ended for thread: " + Thread.currentThread()); // if (em() != null) // em().close(); } }
@Override public void removeAll() throws TckRepositoryException { uow.begin(); final EntityManager manager = managerProvider.get(); try { manager.getTransaction().begin(); // The query 'DELETE FROM Entity' won't be correct as it will ignore orphanRemoval // and may also ignore some configuration options, while EntityManager#remove won't manager .createQuery(format("SELECT e FROM %s e", getEntityName(entityClass)), entityClass) .getResultList() .forEach(manager::remove); manager.getTransaction().commit(); } catch (RuntimeException x) { if (manager.getTransaction().isActive()) { manager.getTransaction().rollback(); } throw new TckRepositoryException(x.getLocalizedMessage(), x); } finally { uow.end(); } }
public void end() { if(!started.get()) throw new RuntimeException("transaction not started"); try { EntityTransaction transaction = emp.get().getTransaction(); PersistenceTransactionSynchronization.Status status; if(transaction.getRollbackOnly()){ transaction.rollback(); log.trace("Jpa transaction rolledback"); status = Status.ROLLEDBACK; } else { transaction.commit(); log.trace("Jpa transaction committed"); status = Status.COMMITTED; } if(sync.get()!=null){ for(PersistenceTransactionSynchronization s : sync.get()){ s.afterTransaction(status); } } } finally { sync.remove(); try { unitOfWork.end(); } finally { started.remove(); } } }
public static void insert(EntityBase entity) { // EntityManager em = entityManager.get(); // unitOfWork.begin(); try { beginUnitOfWork(); em().persist(entity); } catch (Exception | Error ex) { throw ex; } finally { unitOfWork.end(); } // em().persist(entity); //em().flush(); //unitOfWork.end(); // TODO Auto-generated method stub }
public static <T> T sqlQueryLimitOne(Class<T> resultType, String queryStr, Object... params) throws NoResultException { // EntityManager em = em(); T result = null; try { beginUnitOfWork(); log.debug("sqlQueryOne unit of work started for thread: " + Thread.currentThread()); TypedQuery<T> query = em().createQuery(queryStr, resultType); if (params != null) { for (int i = 0; i < params.length; i++) { Object param = params[i]; query.setParameter(i + 1, param); // JPA uses 1-based indexes } } query.setMaxResults(1); result = query.getSingleResult(); return result; } catch (NoResultException e) { // context.getInjector().getInstance(Portfolio.class); // PersistUtil.insert(portfolio); throw e; //return null; } finally { unitOfWork.end(); log.trace("sqlQueryOne unit of work ended for thread: " + Thread.currentThread()); } // em.flush(); // if (em != null) //em.close(); // } }
public static <T> T namedQueryOne(Class<T> resultType, String namedQuery, Object... params) throws NoResultException { try { // unitOfWork. beginUnitOfWork(); final TypedQuery<T> query = em().createNamedQuery(namedQuery, resultType); if (params != null) { for (int i = 0; i < params.length; i++) { Object param = params[i]; query.setParameter(i + 1, param); // JPA uses 1-based indexes } } T result = query.getSingleResult(); return result; } catch (Exception | Error ex) { // System.out.println("Unable to perform request in " + EM.class.getSimpleName() + ":persist, full stack trace follows:" + ex); // ex.printStackTrace(); throw ex; // return null; } finally { unitOfWork.end(); } }
unitOfWork.end();