Exemple #1
0
 public void put(String name, String value, String readPerm, String writePerm) {
   SysConfig cfg;
   if (prefix != null) name = prefix + name;
   try {
     boolean autoCommit = false;
     Transaction tx = db.session().getTransaction();
     if (tx == null || tx.getStatus().isNotOneOf(TransactionStatus.ACTIVE)) {
       tx = db.session().beginTransaction();
       autoCommit = true;
     }
     cfg = (SysConfig) db.session().get(SysConfig.class, name);
     boolean saveIt = false;
     if (cfg == null) {
       cfg = new SysConfig();
       cfg.setId(name);
       saveIt = true;
     }
     cfg.setReadPerm(readPerm);
     cfg.setWritePerm(writePerm);
     cfg.setValue(value);
     if (saveIt) db.session().save(cfg);
     if (autoCommit) tx.commit();
   } catch (HibernateException e) {
     db.getLog().warn(e);
   }
 }
  protected Transaction getTransaction() {
    Transaction transaction = getSession().getTransaction();
    if (transaction.getStatus() != TransactionStatus.ACTIVE) {
      transaction.begin();
    }

    return transaction;
  }
  private void withStatelessSession() {
    withBatch();

    // tag::batch-stateless-session-example[]
    StatelessSession statelessSession = null;
    Transaction txn = null;
    ScrollableResults scrollableResults = null;
    try {
      SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class);
      statelessSession = sessionFactory.openStatelessSession();

      txn = statelessSession.getTransaction();
      txn.begin();

      scrollableResults =
          statelessSession.createQuery("select p from Person p").scroll(ScrollMode.FORWARD_ONLY);

      while (scrollableResults.next()) {
        Person Person = (Person) scrollableResults.get(0);
        processPerson(Person);
        statelessSession.update(Person);
      }

      txn.commit();
    } catch (RuntimeException e) {
      if (txn != null && txn.getStatus() == TransactionStatus.ACTIVE) txn.rollback();
      throw e;
    } finally {
      if (scrollableResults != null) {
        scrollableResults.close();
      }
      if (statelessSession != null) {
        statelessSession.close();
      }
    }
    // end::batch-stateless-session-example[]
  }