Package javax.persistence

Examples of javax.persistence.EntityTransaction


  @Override
  public Object execute(Method method, Object[] parameters) throws Throwable {
    if (!withoutService)
      return action.execute(method, parameters);
    else {
      EntityTransaction tx = entityManager.getTransaction();
      try {
        tx.begin();
        Object result = action.execute(method, parameters);
        tx.commit();
        return result;
      } catch (Exception e) {
        tx.rollback();
        throw e.getCause();
      }
    }
  }
View Full Code Here


    TransactionalType type = transactional.type();
    if(type != TransactionalType.READOLNY){
      entityManager.setNeed2ProcessTransaction(true);
    }
   
    final EntityTransaction transaction = entityManager.getEntityManager().getTransaction();
   
    if(transaction.isActive()){
      return methodInvocation.proceed();
    }

    //开始一个新的事务
    if(type != TransactionalType.READOLNY){
      transaction.begin();
    }
   
    Object result = null;
    try {
      //执行被拦截的业务方法
      result = methodInvocation.proceed();
      //提交事务
      if(type != TransactionalType.READOLNY){
        transaction.commit();
      }
    } catch (Exception e) {
      //回滚当前事务
      if(type != TransactionalType.READOLNY && transaction.isActive()){
        transaction.rollback();
      }
      throw e;
    }
   
    log.debug("[JpaLocalTransactionInterceptor]离开=》"+methodInvocation.getMethod().getName());
View Full Code Here

  public String intercept(ActionInvocation invocation) throws Exception {
    boolean need2ProcessTransaction = false;
   
    String result = null;
    Transaction hbTS = null;
    EntityTransaction jpaTS = null;
   
    try {
      result = invocation.invoke();

      if(PersistenceGuiceContext.getInstance().isUseJPA()){
        EntityManagerFactoryHolder emfH = GuiceContext.getInstance().getBean(EntityManagerFactoryHolder.class);
        EntityManagerInfo entityManager = emfH.getEntityManagerInfo();
        jpaTS = entityManager.getEntityManager().getTransaction();
        need2ProcessTransaction = entityManager.isNeed2ProcessTransaction();
      }else if(PersistenceGuiceContext.getInstance().isUseHibernate()){
        SessionFactoryHolder sessionFH = GuiceContext.getInstance().getBean(SessionFactoryHolder.class);
        SessionInfo session = sessionFH.getSessionInfo();
        hbTS = session.getSession().getTransaction();
        need2ProcessTransaction = session.isNeed2ProcessTransaction() && !hbTS.wasCommitted();
      }

      if(need2ProcessTransaction){
        if(hbTS != null)
          hbTS.commit();
        if(jpaTS != null)
          jpaTS.commit();
      }
    } catch (Exception e) {
      if(hbTS != null)
        hbTS.rollback();
      if(jpaTS != null)
        jpaTS.rollback();
      throw e;
    }
    return result;
  }
View Full Code Here

    super(name);
  }

  final public void testAddFieldvalues() throws Exception {

    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();
    Fieldlanguagesvalues flv = new Fieldlanguagesvalues();
    flv.setStarttime(new Date());
    flv.setValue("test");
    flv.setLanguage_id(33L);
    flv.setDeleted("false");
    try {
      flv = getEntityManager().merge(flv);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }

    tx.begin();
    Fieldvalues fl = new Fieldvalues();
    fl.setStarttime(new Date());
    fl.setName("test value");
    fl.setFieldlanguagesvalue(flv);
    fl.setDeleted("false");
    try {
      getEntityManager().merge(fl);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }

  }
View Full Code Here

    super(name);
  }

  final public void testCriteriaBuilder() throws Exception{
    EntityManager em = getEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
    Root<Configuration> c = cq.from(Configuration.class);
    Predicate condition = cb.equal(c.get("deleted"), "false");
    cq.where(condition);
    TypedQuery<Configuration> q = em.createQuery(cq);
    List<Configuration> result = q.getResultList();
    try {
      tx.commit();
      assertTrue("result should be empty", result.size()==0);
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }

    // add new configuration
    tx.begin();
    Configuration conf = new Configuration();
    conf.setStarttime(new Date());
    conf.setDeleted("false");
    conf.setConf_key("key1");
    conf.setConf_value("value1");
    try {
      conf = getEntityManager().merge(conf);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }
   
    result = q.getResultList();
    assertTrue("result should not be empty", result.size()>0);

    // delete configuration
    Long id = conf.getConfiguration_id();
    tx.begin();
    try {
      conf = getEntityManager().find(Configuration.class, id);
      getEntityManager().remove(conf);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }
    result = q.getResultList();
    assertTrue("result should be empty", result.size() == 0);
  }
View Full Code Here

    super(name);
  }
 
  final public void testAddErrorValues() throws Exception {

    EntityTransaction tx = getEntityManager().getTransaction();
    ErrorValues eValue = new ErrorValues();
    eValue.setErrorvalues_id(1L);
    eValue.setErrortype_id(1L);
    eValue.setDeleted("false");
    eValue.setStarttime(new Date());
    eValue.setFieldvalues_id(new Long(322));
    tx.begin();
    try {
      getEntityManager().merge(eValue);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }
  }
View Full Code Here

  final public void testAddSalutation() throws Exception {
    ArrayList<Long> Ids = new ArrayList<Long>();
    Ids.add(11L);
    PrivateMessagesDaoImpl.getInstance().updatePrivateMessagesToTrash(Ids, true, 0L);

    EntityTransaction tx = getEntityManager().getTransaction();
    Salutations sl = new Salutations();
    sl.setDeleted("false");
    sl.setName("addSalutation");
    sl.setFieldvalues_id(1000L);
    sl.setStarttime(new Date());
    tx.begin();
    try {
      getEntityManager().merge(sl);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }
   
  }
View Full Code Here

   
  }
 
  final public void testAddFieldvalues() throws Exception {

    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();
    Fieldvalues fl = new Fieldvalues();
    fl.setStarttime(new Date());
    fl.setName("test value");
    fl.setDeleted("false");
    try {
      getEntityManager().merge(fl);
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new Exception(e.getMessage());
    }
  }
View Full Code Here

  public Users_Usergroups[] getUserGroups(Long USER_ID) {
    Users_Usergroups[] usersusergroups = new Users_Usergroups[1];
    try {
      Object idf = PersistenceSessionUtil.createSession();
      EntityManager session = PersistenceSessionUtil.getSession();
      EntityTransaction tx = session.getTransaction();
      tx.begin();
      Query query = session
          .createQuery("select c from Users_Usergroups as c where c.user_id = :user_id");
      query.setParameter("user_id", USER_ID.longValue());
      int count = query.getResultList().size();
      usersusergroups = new Users_Usergroups[count];
      int k = 0;
      for (Iterator it2 = query.getResultList().iterator(); it2.hasNext();) {
        usersusergroups[k] = (Users_Usergroups) it2.next();
        k++;
      }
      tx.commit();
      PersistenceSessionUtil.closeSession(idf);
    } catch (Exception ex2) {
      log.error("getUserGroups",ex2);
    }
    return usersusergroups;
View Full Code Here

  public Users_Usergroups getUserGroupsSingle(long USER_ID) {
    Users_Usergroups usersusergroups = new Users_Usergroups();
    try {
      Object idf = PersistenceSessionUtil.createSession();
      EntityManager session = PersistenceSessionUtil.getSession();
      EntityTransaction tx = session.getTransaction();
      tx.begin();
      Query query = session
          .createQuery("select c from Users_Usergroups as c where c.user_id = :user_id");
      query.setParameter("user_id", USER_ID);
      for (Iterator it2 = query.getResultList().iterator(); it2.hasNext();) {
        usersusergroups = (Users_Usergroups) it2.next();
      }
      tx.commit();
      PersistenceSessionUtil.closeSession(idf);
    } catch (Exception ex2) {
      log.error("getUserGroupsSingle",ex2);
    }
    return usersusergroups;
View Full Code Here

TOP

Related Classes of javax.persistence.EntityTransaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.