public void testDelistErrorAndUnilateralRollbackOnCommit() throws Exception {
    btm.begin();

    Connection connection1 = poolingDataSource1.getConnection();
    JdbcConnectionHandle handle1 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection1);
    XAConnection xaConnection1 =
        (XAConnection) AbstractMockJdbcTest.getWrappedXAConnectionOf(handle1.getPooledConnection());
    MockXAResource xaResource1 = (MockXAResource) xaConnection1.getXAResource();
    xaResource1.setEndException(
        new BitronixXAException("screw delistment", XAException.XAER_RMERR));
    xaResource1.setRollbackException(
        new BitronixXAException("delistment was screwed, cannot rollback", XAException.XAER_RMERR));

    connection1.createStatement(); // triggers enlistment

    Connection connection2 = poolingDataSource2.getConnection();
    JdbcConnectionHandle handle2 = (JdbcConnectionHandle) Proxy.getInvocationHandler(connection2);
    XAConnection xaConnection2 =
        (XAConnection) AbstractMockJdbcTest.getWrappedXAConnectionOf(handle2.getPooledConnection());
    MockXAResource xaResource2 = (MockXAResource) xaConnection2.getXAResource();
    xaResource2.setEndException(
        new BitronixXAException("what was that transaction again?", XAException.XAER_NOTA));
    xaResource2.setRollbackException(
        new BitronixXAException(
            "delistment unilaterally rolled back, cannot rollback twice", XAException.XAER_RMERR));

    connection2.createStatement(); // triggers enlistment

    try {
      btm.commit();
      fail("expected RollbackException");
    } catch (RollbackException ex) {
      assertEquals(
          "delistment error caused transaction rollback"
              + System.getProperty("line.separator")
              + "  resource(s) [pds2] unilaterally rolled back"
              + System.getProperty("line.separator")
              + "  resource(s) [pds1] could not be delisted",
          ex.getMessage());
    }

    // check flow
    List orderedEvents = EventRecorder.getOrderedEvents();
    log.info(EventRecorder.dumpToString());

    assertEquals(8, orderedEvents.size());
    int i = 0;
    assertEquals(Status.STATUS_ACTIVE, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
    assertEquals(XAResource.TMNOFLAGS, ((XAResourceStartEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(XAResource.TMNOFLAGS, ((XAResourceStartEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(XAResource.TMSUCCESS, ((XAResourceEndEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(
        Status.STATUS_MARKED_ROLLBACK, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
    assertEquals(XAResource.TMSUCCESS, ((XAResourceEndEvent) orderedEvents.get(i++)).getFlag());
    assertEquals(
        Status.STATUS_ROLLING_BACK, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
    assertEquals(Status.STATUS_ROLLEDBACK, ((JournalLogEvent) orderedEvents.get(i++)).getStatus());
  }
  public String updateKeyValueDatabase(String key, String value) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    try {
      userTransaction.begin();

      /*
       * Since the Entity Manager (EM) is not managed by the container the developer must explicitly tell the EM
       * to join the transaction. Compare this with ManagedComponent where the container automatically
       * enlists the EM with the transaction. The persistence context managed by the EM will then be scoped
       * to the JTA transaction which means that all entities will be detached when the transaction commits.
       */
      entityManager.joinTransaction();

      // make some transactional changes
      String result = updateKeyValueDatabase(entityManager, key, value);

      /*
       * Note that the default scope of entities managed by the EM is transaction. Thus once the transaction
       * commits the entity will be detached from the EM. See also the comment in the finally block below.
       */
      userTransaction.commit();

      return result;
    } catch (RollbackException e) {
      // We tried to commit the transaction but it has already been rolled back (adding duplicate
      // keys would
      // generate this condition although the example does check for duplicates).
      Throwable t = e.getCause();

      return t != null ? t.getMessage() : e.getMessage();
    } catch (Exception e) {
      /*
       * An application cannot handle any of the other exceptions raised by begin and commit so we just
       * catch the generic exception. The meaning of the other exceptions is:
       *
       * NotSupportedException - the thread is already associated with a transaction
       * HeuristicRollbackException - should not happen since the example is interacting with a single database
       * HeuristicMixedException -  should not happen since the example is interacting with a single database
       * SystemException - the TM raised an unexpected error. There is no standard way of handling this error
       *  (another reason why CMT are preferable to managing them ourselves)
       */
      return e.getMessage();
    } finally {
      /*
       * Since the EM is transaction scoped it will not detach its objects even when calling close on it
       * until the transaction completes. Therefore we must roll back any active transaction before returning.
       */
      try {
        if (userTransaction.getStatus() == Status.STATUS_ACTIVE) userTransaction.rollback();
      } catch (Throwable e) {
        // ignore
      }

      entityManager.close();
    }
  }
 private TransactionContext bindToTransaction(Transaction tx)
     throws IllegalStateException, SystemException {
   final TransactionContextImpl txContext = new TransactionContextImpl();
   // register for call-backs
   try {
     tx.registerSynchronization(new SleeTransactionSynchronization(tx, txContext));
   } catch (RollbackException e) {
     throw new IllegalStateException(
         "Unable to register listener for created transaction. Error: " + e.getMessage());
   }
   // store tx context in thread
   TransactionContextThreadLocal.setTransactionContext(txContext);
   return txContext;
 }
Example #4
0
  /**
   * Handles the HTTP <code>POST</code> method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      String showid = request.getParameter("si");

      String[] pics;
      pics = request.getParameterValues("pics[]");

      String[] comms;
      comms = request.getParameterValues("comms[]");

      utx.begin();
      EntityManager em = emf.createEntityManager();
      for (int i = 0; i < pics.length; i++) {
        Ecpresentations etemp = em.getReference(Ecpresentations.class, showid);
        Ecinfo ef = new Ecinfo();
        ef.setPicy(pics[i]);
        ef.setDetails(comms[i]);
        ef.setShowid(etemp);
        em.persist(ef);
      }
      utx.commit();
      RequestDispatcher view = request.getRequestDispatcher("addinforeport.jsp");
      view.forward(request, response);
      em.close();
    } catch (NotSupportedException ex) {
      ex.printStackTrace();
    } catch (SystemException ex) {
      ex.printStackTrace();
    } catch (RollbackException ex) {
      ex.printStackTrace();
    } catch (HeuristicMixedException ex) {
      ex.printStackTrace();
    } catch (HeuristicRollbackException ex) {
      ex.printStackTrace();
    } catch (SecurityException ex) {
      ex.printStackTrace();
    } catch (IllegalStateException ex) {
      ex.printStackTrace();
    }
  }
  @PostConstruct
  private void init() {
    try {
      userTransaction.begin();

      initData();

      userTransaction.commit();
    } catch (NotSupportedException e) {
      e.printStackTrace();
    } catch (SystemException e) {
      e.printStackTrace();
    } catch (HeuristicRollbackException e) {
      e.printStackTrace();
    } catch (javax.transaction.RollbackException e) {
      e.printStackTrace();
    } catch (HeuristicMixedException e) {
      e.printStackTrace();
    }
  }
Example #6
0
  /** 针对oracle Clob字段的插入操作 */
  @Test
  public void testBigClobWrite() {
    PreparedDBUtil dbUtil = new PreparedDBUtil();
    TransactionManager tm = new TransactionManager();
    try {
      // 启动事务
      tm.begin();
      // 先插入一条记录,blob字段初始化为empty_lob
      dbUtil.preparedInsert("insert into test(id,clobname) values(?,?)");
      String id = DBUtil.getNextStringPrimaryKey("test");
      dbUtil.setString(1, id);
      dbUtil.setClob(2, CLOB.empty_lob()); // 先设置空的blob字段

      dbUtil.executePrepared();

      // 查找刚才的插入的记录,修改blob字段的值为一个文件
      dbUtil = new PreparedDBUtil();
      dbUtil.preparedSelect("select clobname from test where id = ?");
      dbUtil.setString(1, id);
      dbUtil.executePrepared();

      CLOB clob = (CLOB) dbUtil.getClob(0, "clobname");
      if (clob != null) {
        DBUtil.updateCLOB(clob, new java.io.File("d:\\route.txt"));
      }
      tm.commit();

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      try {
        tm.rollback();
      } catch (RollbackException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    } finally {
      tm = null;
      dbUtil = null;
    }
  }
  /**
   * Example for sending a message fire and forget
   *
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    final String methodName = "doGet";

    // We simply return some text showing what we've done
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();

    WLMJMSMessageProducer wlmConnection = null;
    boolean complete = false;
    try {
      // Begin a transaction
      userTransaction.begin();

      // Print the user transaction status
      out.println(WLMJMSTranUtils.summarizeUserTranStatus(userTransaction));

      // Create our JMS connection/session/producer using the WLM connection logic
      // Many fire-and-forget scenarios are for exactly once delivery, where a
      // database is updated in the same transaction as sending the request.
      // For this reason in this example we begin a transaction and send
      // a persistent message.
      // As such JDBC operations could be inserted into this sample, and would
      // be coordinated in an atomic transaction with sending the request.
      //
      // If you are instead looking to send nonpersistent messages, such as publishing
      // some non-critical state data on a topic, then you should consider
      // removing the transaction, changing the transaction
      // boolean in the getMessageProducer() call to false, and setting the DeliveryMode
      // to NON_PERSISTENT when sending the message.
      out.println("JMS destination: " + fireAndForgetTarget);
      wlmConnection =
          wlmJMSAttach.getMessageProducer(fireAndForgetTarget, true, Session.AUTO_ACKNOWLEDGE);

      // TODO: Replace this section with business logic
      // We create a temporary queue solely for the purpose of finding out where
      // we are connected, as the temporary queue name should show this.
      // This is inefficient, so it is for demonstration purposes only.
      TemporaryQueue temporaryQueue = wlmConnection.getSession().createTemporaryQueue();
      String temporaryQueueName = temporaryQueue.getQueueName();
      out.println("Temporary queue showing where we are connected:");
      out.println(temporaryQueueName);
      temporaryQueue.delete();
      String exampleMessageBody =
          this.getClass().getName()
              + " (thread \""
              + Thread.currentThread().getName()
              + "\") sending at "
              + new Date();
      Message message = wlmConnection.getSession().createTextMessage(exampleMessageBody);
      out.println("Sending message \"" + exampleMessageBody + "\"");

      // Send the message
      wlmConnection
          .getProducer()
          .send(
              message,
              DeliveryMode.PERSISTENT, /* We are persistent in this example */
              wlmConnection.getProducer().getPriority() /* Default priority */,
              0 /* Do not expire */);
      out.println("JMSMessageID: " + message.getJMSMessageID());

      // Commit the transaction
      userTransaction.commit();

      // Mark that we're complete, so that we throw any exception seen during cleanup,
      // and do not attempt rollback of the transaction
      complete = true;
    } catch (HeuristicMixedException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction HeuristicMixedException", e);
      throw new ServletException("Transaction HeuristicMixedException: " + e.getMessage(), e);
    } catch (HeuristicRollbackException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction HeuristicRollbackException", e);
      throw new ServletException("Transaction HeuristicRollbackException: " + e.getMessage(), e);
    } catch (RollbackException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction RollbackException", e);
      throw new ServletException("Transaction RollbackException: " + e.getMessage(), e);
    } catch (NotSupportedException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction NotSupportedException", e);
      throw new ServletException("Transaction NotSupportedException: " + e.getMessage(), e);
    } catch (SystemException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction SystemException", e);
      throw new ServletException("Transaction SystemException: " + e.getMessage(), e);
    } catch (JMSException e) {
      if (log.enabled()) log.logRootExMsg(methodName, "JMSException in business logic", e);
      throw new ServletException("JMSException: " + e.getMessage(), e);
    } finally {
      // Rollback if we didn't complete
      if (!complete)
        try {
          userTransaction.rollback();
        } catch (SystemException e) {
          // We are already on an exception path, so we should not throw this exception
          if (log.enabled()) log.logExStack(methodName, "Exception during rollback", e);
        }

      // Close off our resources, ensuring we only throw an exception if we completed
      // our earlier logic, so we don't override an earlier exception that being thrown.
      if (wlmConnection != null)
        try {
          wlmConnection.close(complete);
        } catch (JMSException e) {
          throw new ServletException("JMSException on Connection close: " + e.getMessage(), e);
        }
    }
  }
  @Test
  public void testScenario1() throws Exception {
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = null;
    // Connection conn = null;
    try {
      qs.createIndex("iIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
      // PUT 4 objects in region, QUERY, CREATEINDEX
      for (int i = 0; i < 4; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      QueryObserverImpl observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where ID != 53");
      Object r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));

      // print("Index IS: " + ((RangeIndex)i2).dump());

      // BEGIN TX PUT new 4 objects in region, QUERY,CREATEINDEX, ROLLBACK
      ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
      ta.begin();
      for (int i = 9; i < 13; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where ID != 53");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));
      Index i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
      // print("Index IS: " + ((RangeIndex)i1).dump());
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      ta.rollback();
      // PRINT INDEX AFTER ROLLBACK, REMOVEINDEX.
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      // print("AfterRollback \n"+currRegion.values());
      // print("Index IS: " + ((RangeIndex)i1).dump());
      qs.removeIndex(i1);
      // BEGIN TX PUT new 4 objects in region,CREATEINDEX, QUERY ,COMMIT
      ta.begin();
      for (int i = 9; i < 13; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
      // print("Index IS: " + ((RangeIndex)i1).dump());
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      for (int i = 9; i < 13; i++) {
        currRegion.put("key" + i, new Portfolio(i));
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 2) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));
      // print("Index on status IS: " + ((RangeIndex)i1).dump());
      // print("Index On ID IS: " + ((RangeIndex)i2).dump());
      ta.commit();
      // WAIT FOR 2 secs DISPLAYINDEX, QUERY

      Thread.sleep(2000);

      // print("Aftercommit \n"+currRegion.values());
      // print("Index IS: " + ((RangeIndex)i1).dump());
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 4) {
        fail("Query result not of expected size");
      }
      observer2 = new QueryObserverImpl();
      QueryObserverHolder.setInstance(observer2);
      q = qs.newQuery("select distinct * from /portfolios where ID != 53");
      r = q.execute();
      if (!observer2.isIndexesUsed) {
        fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
      }
      if (((Collection) r).size() != 8) {
        fail("Query result not of expected size");
      }
      // print("Size of query result :"+ ((Collection)r).size());
      // print("Result of query =" + Utils.printResult(r));
      // print("Index On ID IS: " + ((RangeIndex)i2).dump());
    } catch (RollbackException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
      ta.rollback();
    }
  }
  /**
   * Function accepts parameter in Map. The patient count will be obfuscated if the user is OBFUS
   */
  public void generateResult(Map param) throws CRCTimeOutException, I2B2DAOException {

    SetFinderConnection sfConn = (SetFinderConnection) param.get("SetFinderConnection");
    SetFinderDAOFactory sfDAOFactory = (SetFinderDAOFactory) param.get("SetFinderDAOFactory");

    // String patientSetId = (String)param.get("PatientSetId");
    String queryInstanceId = (String) param.get("QueryInstanceId");
    String TEMP_DX_TABLE = (String) param.get("TEMP_DX_TABLE");
    String resultInstanceId = (String) param.get("ResultInstanceId");
    // String itemKey = (String) param.get("ItemKey");
    String resultTypeName = (String) param.get("ResultOptionName");
    String processTimingFlag = (String) param.get("ProcessTimingFlag");
    int obfuscatedRecordCount = (Integer) param.get("ObfuscatedRecordCount");
    int transactionTimeout = (Integer) param.get("TransactionTimeout");
    TransactionManager tm = (TransactionManager) param.get("TransactionManager");
    this.setDbSchemaName(sfDAOFactory.getDataSourceLookup().getFullSchema());
    Map ontologyKeyMap = (Map) param.get("setFinderResultOntologyKeyMap");
    String serverType = (String) param.get("ServerType");
    CallOntologyUtil ontologyUtil = (CallOntologyUtil) param.get("CallOntologyUtil");
    List<String> roles = (List<String>) param.get("Roles");
    String tempTableName = "";
    PreparedStatement stmt = null;
    boolean errorFlag = false, timeoutFlag = false;
    String itemKey = "";

    int actualTotal = 0, obsfcTotal = 0;
    boolean obfscDataRoleFlag = false;
    try {
      LogTimingUtil logTimingUtil = new LogTimingUtil();
      logTimingUtil.setStartTime();
      obfscDataRoleFlag = checkDataObscRole(sfDAOFactory.getOriginalDataSourceLookup(), roles);
      itemKey = getItemKeyFromResultType(sfDAOFactory, resultTypeName);

      log.debug("Result type's " + resultTypeName + " item key value " + itemKey);

      LogTimingUtil subLogTimingUtil = new LogTimingUtil();
      subLogTimingUtil.setStartTime();
      ConceptsType conceptsType = ontologyUtil.callGetChildren(itemKey);
      if (conceptsType != null && conceptsType.getConcept().size() < 1) {
        throw new I2B2DAOException(
            "Could not fetch children result type "
                + resultTypeName
                + " item key [ "
                + itemKey
                + " ]");
      }
      subLogTimingUtil.setEndTime();
      if (processTimingFlag != null) {
        if (processTimingFlag.trim().equalsIgnoreCase(ProcessTimingReportUtil.DEBUG)) {
          ProcessTimingReportUtil ptrUtil =
              new ProcessTimingReportUtil(sfDAOFactory.getDataSourceLookup());
          ptrUtil.logProcessTimingMessage(
              queryInstanceId,
              ptrUtil.buildProcessTiming(
                  subLogTimingUtil,
                  "BUILD - " + resultTypeName + " : Ontology Call(GetChildren) ",
                  ""));
        }
      }

      String itemCountSql =
          " select count(distinct PATIENT_NUM) as item_count  from "
              + this.getDbSchemaName()
              + "observation_fact obs_fact  "
              + " where obs_fact.patient_num in (select patient_num from "
              + TEMP_DX_TABLE
              + "    ) "
              + " and obs_fact.concept_cd in (select concept_cd from "
              + this.getDbSchemaName()
              + "concept_dimension  where concept_path like ?)";

      ResultType resultType = new ResultType();
      resultType.setName(resultTypeName);
      stmt = sfConn.prepareStatement(itemCountSql);

      CancelStatementRunner csr = new CancelStatementRunner(stmt, transactionTimeout);
      Thread csrThread = new Thread(csr);
      csrThread.start();

      for (ConceptType conceptType : conceptsType.getConcept()) {

        String dimCode = this.getDimCodeInSqlFormat(conceptType);

        itemCountSql =
            " select count(distinct PATIENT_NUM) as item_count  from "
                + this.getDbSchemaName()
                + " observation_fact "
                + " where "
                + " patient_num in (select patient_num from "
                + TEMP_DX_TABLE
                + " )  and "
                + conceptType.getFacttablecolumn()
                + " IN (select "
                + conceptType.getFacttablecolumn()
                + " from "
                + getDbSchemaName()
                + conceptType.getTablename()
                + "  "
                + " where "
                + conceptType.getColumnname()
                + " "
                + conceptType.getOperator()
                + " "
                + dimCode
                + ")";

        stmt = sfConn.prepareStatement(itemCountSql);

        // // smuniraju: Currently, in postgres, a timeout value > 0 will result in "setQueryTimeout
        // is not yet implemented"
        // stmt.setQueryTimeout(transactionTimeout);
        int queryTimeout =
            (serverType.equalsIgnoreCase(DAOFactoryHelper.POSTGRES)) ? 0 : transactionTimeout;
        stmt.setQueryTimeout(queryTimeout);

        log.debug("Executing count sql [" + itemCountSql + "]");

        //
        subLogTimingUtil.setStartTime();
        ResultSet resultSet = stmt.executeQuery();
        if (csr.getSqlFinishedFlag()) {
          timeoutFlag = true;
          throw new CRCTimeOutException("The query was canceled.");
        }
        resultSet.next();
        int demoCount = resultSet.getInt("item_count");
        subLogTimingUtil.setEndTime();
        if (processTimingFlag != null) {
          if (processTimingFlag.trim().equalsIgnoreCase(ProcessTimingReportUtil.DEBUG)) {
            ProcessTimingReportUtil ptrUtil =
                new ProcessTimingReportUtil(sfDAOFactory.getDataSourceLookup());
            ptrUtil.logProcessTimingMessage(
                queryInstanceId,
                ptrUtil.buildProcessTiming(
                    subLogTimingUtil,
                    "BUILD - "
                        + resultTypeName
                        + " : COUNT SQL for "
                        + conceptType.getDimcode()
                        + " ",
                    "sql=" + itemCountSql));
          }
        }
        //

        actualTotal += demoCount;
        if (obfscDataRoleFlag) {
          GaussianBoxMuller gaussianBoxMuller = new GaussianBoxMuller();
          demoCount = (int) gaussianBoxMuller.getNormalizedValueForCount(demoCount);
          obsfcTotal += demoCount;
        }
        DataType mdataType = new DataType();
        mdataType.setValue(String.valueOf(demoCount));
        mdataType.setColumn(conceptType.getName());
        mdataType.setType("int");
        resultType.getData().add(mdataType);
      }
      csr.setSqlFinishedFlag();
      csrThread.interrupt();
      stmt.close();

      edu.harvard.i2b2.crc.datavo.i2b2result.ObjectFactory of =
          new edu.harvard.i2b2.crc.datavo.i2b2result.ObjectFactory();
      BodyType bodyType = new BodyType();
      bodyType.getAny().add(of.createResult(resultType));
      ResultEnvelopeType resultEnvelop = new ResultEnvelopeType();
      resultEnvelop.setBody(bodyType);

      JAXBUtil jaxbUtil = CRCJAXBUtil.getJAXBUtil();

      StringWriter strWriter = new StringWriter();
      subLogTimingUtil.setStartTime();
      jaxbUtil.marshaller(of.createI2B2ResultEnvelope(resultEnvelop), strWriter);
      subLogTimingUtil.setEndTime();
      tm.begin();
      IXmlResultDao xmlResultDao = sfDAOFactory.getXmlResultDao();
      xmlResultDao.createQueryXmlResult(resultInstanceId, strWriter.toString());
      //
      if (processTimingFlag != null) {
        if (!processTimingFlag.trim().equalsIgnoreCase(ProcessTimingReportUtil.NONE)) {
          ProcessTimingReportUtil ptrUtil =
              new ProcessTimingReportUtil(sfDAOFactory.getDataSourceLookup());
          if (processTimingFlag.trim().equalsIgnoreCase(ProcessTimingReportUtil.DEBUG)) {
            ptrUtil.logProcessTimingMessage(
                queryInstanceId,
                ptrUtil.buildProcessTiming(subLogTimingUtil, "JAXB - " + resultTypeName, ""));
          }
          logTimingUtil.setEndTime();
          ptrUtil.logProcessTimingMessage(
              queryInstanceId,
              ptrUtil.buildProcessTiming(logTimingUtil, "BUILD - " + resultTypeName, ""));
        }
      }
      tm.commit();
    } catch (com.microsoft.sqlserver.jdbc.SQLServerException sqlServerEx) {
      // if the setQueryTimeout worked, then the message would be timed
      // out
      if (sqlServerEx.getMessage().indexOf("timed out") > -1) {
        timeoutFlag = true;
        throw new CRCTimeOutException(sqlServerEx.getMessage(), sqlServerEx);
      } else if (sqlServerEx
              .getMessage()
              .indexOf( // if the stmt.cancel()
                  // worked, then this
                  // exception is
                  // thrown
                  "The query was canceled.")
          > -1) {

        timeoutFlag = true;
        throw new CRCTimeOutException(sqlServerEx.getMessage(), sqlServerEx);
      } else {

        errorFlag = true;
        log.error("Sqlserver error while executing sql", sqlServerEx);
        throw new I2B2DAOException("Sqlserver error while executing sql", sqlServerEx);
      }

    } catch (SQLException sqlEx) {
      // catch oracle query timeout error ORA-01013
      if (sqlEx.toString().indexOf("ORA-01013") > -1) {
        timeoutFlag = true;
        throw new CRCTimeOutException(sqlEx.getMessage(), sqlEx);
      }
      if (sqlEx.getMessage().indexOf("The query was canceled.") > -1) {
        timeoutFlag = true;
        throw new CRCTimeOutException(sqlEx.getMessage(), sqlEx);
      }
      errorFlag = true;
      log.error("Error while executing sql", sqlEx);
      throw new I2B2DAOException("Error while executing sql", sqlEx);
    } catch (Exception sqlEx) {

      errorFlag = true;
      log.error("QueryResultPatientSetGenerator.generateResult:" + sqlEx.getMessage(), sqlEx);
      throw new I2B2DAOException(
          "QueryResultPatientSetGenerator.generateResult:" + sqlEx.getMessage(), sqlEx);
    } finally {

      IQueryResultInstanceDao resultInstanceDao = sfDAOFactory.getPatientSetResultDAO();

      if (errorFlag) {
        resultInstanceDao.updatePatientSet(
            resultInstanceId, QueryStatusTypeId.STATUSTYPE_ID_ERROR, 0);
      } else {
        // set the setsize and the description of the result instance if
        // the user role is obfuscated
        if (timeoutFlag == false) { // check if the query completed
          try {
            tm.begin();

            String obfusMethod = "", description = null;
            if (obfscDataRoleFlag) {
              obfusMethod = IQueryResultInstanceDao.OBSUBTOTAL;
              // add () to the result type description
              // read the description from result type

            } else {
              obfuscatedRecordCount = actualTotal;
            }
            IQueryResultTypeDao resultTypeDao = sfDAOFactory.getQueryResultTypeDao();
            List<QtQueryResultType> resultTypeList =
                resultTypeDao.getQueryResultTypeByName(resultTypeName);

            // add "(Obfuscated)" in the description
            // description = resultTypeList.get(0)
            //		.getDescription()
            //		+ " (Obfuscated) ";
            String queryName =
                sfDAOFactory
                    .getQueryMasterDAO()
                    .getQueryDefinition(
                        sfDAOFactory
                            .getQueryInstanceDAO()
                            .getQueryInstanceByInstanceId(queryInstanceId)
                            .getQtQueryMaster()
                            .getQueryMasterId())
                    .getName();

            resultInstanceDao.updatePatientSet(
                resultInstanceId,
                QueryStatusTypeId.STATUSTYPE_ID_FINISHED,
                null,
                // obsfcTotal,
                obfuscatedRecordCount,
                actualTotal,
                obfusMethod);

            description = resultTypeList.get(0).getDescription() + " for \"" + queryName + "\"";

            // set the result instance description
            resultInstanceDao.updateResultInstanceDescription(resultInstanceId, description);
            tm.commit();
          } catch (NotSupportedException e) {
            throw new I2B2DAOException(
                "Failed to write obfuscated description " + e.getMessage(), e);
          } catch (SystemException e) {
            throw new I2B2DAOException(
                "Failed to write obfuscated description " + e.getMessage(), e);
          } catch (SecurityException e) {
            throw new I2B2DAOException(
                "Failed to write obfuscated description " + e.getMessage(), e);
          } catch (IllegalStateException e) {
            throw new I2B2DAOException(
                "Failed to write obfuscated description " + e.getMessage(), e);
          } catch (RollbackException e) {
            throw new I2B2DAOException(
                "Failed to write obfuscated description " + e.getMessage(), e);
          } catch (HeuristicMixedException e) {
            throw new I2B2DAOException(
                "Failed to write obfuscated description " + e.getMessage(), e);
          } catch (HeuristicRollbackException e) {
            throw new I2B2DAOException(
                "Failed to write obfuscated description " + e.getMessage(), e);
          }
        }
      }
    }
  }