2017-06-15 61 views
0

我不太清楚如何解釋這個問題,而不是我想要用數據填充的四個表中的每一個,有時其中一些是隻是空着。我無法預測哪些是空的。它可能是1和2,或者只有3,或者只有一個表。當其中一個表沒有被填充時,我會在rollback()catch塊中結束。有時他們都工作。這與某種線程中斷有關。鑑於下面的代碼和例外情況,會出現什麼問題?線程「Thread-10」中的異常java.lang.IllegalStateException

線程對象創建和存儲:

new Thread(() -> { 
     /* Loop through the "primary key" incidentNumber to create list of objects in memory */ 
     for (int i = 0; i < incidentNumberList.size(); i++) { 
       Incident incident = new Incident(); 

       /* Save Incidents to Local H2 Database */ 
       if (!(targetDateList.get(i).isEmpty())) { 
        try { 
         String targetDate = DateUtil.formatTargetDate(targetDateList.get(i)); 
         String timeRemaining = DateUtil.getTimeRemaining(targetDateList.get(i)); 

         try { 
          // Make sure no transactions are currently running to avoid JPA Error 
          if (!entityManager.getTransaction().isActive()) { 
           // Begin saving Incident objects to H2 
           entityManager.getTransaction().begin(); 
           incident.setIncidentNumber(incidentNumberList.get(i)); 
           incident.setSummary(summaryList.get(i)); 
           incident.setRequestId(requestIdList.get(i)); 
           incident.setPriority(priorityList.get(i)); 
           incident.setLastModifiedDate((lastModifiedDateList.get(i))); 
           incident.setStatus(statusList.get(i)); 
           incident.setTargetDate(targetDate); 
           incident.setTimeRemaining(timeRemaining); 
           incident = entityManager.merge(incident); 
           entityManager.getTransaction().commit(); 
          } 

         } catch (Exception e) { 
          entityManager.getTransaction().rollback(); 
         } 

        } catch (ParseException e) { 
         e.printStackTrace(); 
        } 
       } 

       /* Create 4 separate lists for each priority level, i.e. Low --> Critical, to populate each of the 4 TableViews */ 
       if (priorityList.get(i).equals(priorityType)) { 
        switch (priorityType) { 
         case "Low": 
          incidentObjectListGreen.add(incident); 
          break; 
         case "Medium": 
          incidentObjectListYellow.add(incident); 
          break; 
         case "High": 
          incidentObjectListOrange.add(incident); 
          break; 
         case "Critical": 
          incidentObjectListRed.add(incident); 
          break; 
        } 

        Incident finalIncident = incident; 
        Platform.runLater(() -> dataPriority.add(finalIncident)); 
       } 

      } 
    }).start(); 

例外:

Exception in thread "Thread-11" java.lang.IllegalStateException: 
Exception Description: No transaction is currently active 
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.rollback(EntityTransactionImpl.java:176) 
    at app.controller.TableViewController.lambda$organizeTable$2(TableViewController.java:432) 
    at java.lang.Thread.run(Thread.java:745) 
Exception in thread "Thread-10" java.lang.IllegalStateException: 
Exception Description: No transaction is currently active 
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.rollback(EntityTransactionImpl.java:176) 
    at app.controller.TableViewController.lambda$organizeTable$2(TableViewController.java:432) 
    at java.lang.Thread.run(Thread.java:745) 
Exception in thread "Thread-9" java.lang.IllegalStateException: 
Exception Description: No transaction is currently active 
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.rollback(EntityTransactionImpl.java:176) 
    at app.controller.TableViewController.lambda$organizeTable$2(TableViewController.java:432) 
    at java.lang.Thread.run(Thread.java:745) 

事件實體:

@Entity 
@Table(name = "INCIDENT") 
public class Incident implements Serializable { 

    @Id 
    @Column(name = "INCIDENTNUMBER") 
    private String incidentNumber; 

    @Column(name = "SUMMARY") 
    private String summary; 

    @Column(name = "REQUESTID") 
    private String requestId; 

    @Column(name = "PRIORITY") 
    private String priority; 

    @Column(name = "STATUS") 
    private String status; 

    @Column(name = "ASSIGNEE") 
    private String assignee; 

    @Column(name = "LASTMODIFIEDDATE") 
    private String lastModifiedDate; 

    @Column(name = "TARGETDATE") 
    private String targetDate; 

    @Column(name = "TIMEREMAINING") 
    private String timeRemaining; 

    public String getRequestId() { 
     return requestId; 
    } 

    public void setRequestId(String requestId) { 
     this.requestId = requestId; 
    } 

    public String getIncidentNumber() { 
     return incidentNumber; 
    } 

    public void setIncidentNumber(String incidentNumber) { 
     this.incidentNumber = incidentNumber; 
    } 

    public String getSummary() { 
     return summary; 
    } 

    public void setSummary(String summary) { 
     this.summary = summary; 
    } 

    public String getPriority() { 
     return priority; 
    } 

    public void setPriority(String priority) { 
     this.priority = priority; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public void setStatus(String status) { 
     this.status = status; 
    } 

    public String getAssignee() { 
     return assignee; 
    } 

    public void setAssignee(String assignee) { 
     this.assignee = assignee; 
    } 

    public String getLastModifiedDate() { 
     return lastModifiedDate; 
    } 

    public void setLastModifiedDate(String lastModifiedDate) { 
     this.lastModifiedDate = lastModifiedDate; 
    } 

    public String getTargetDate() { 
     return targetDate; 
    } 

    public void setTargetDate(String targetDate) { 
     this.targetDate = targetDate; 
    } 

    public String getTimeRemaining() { 
     return timeRemaining; 
    } 

    public void setTimeRemaining(String timeRemaining) { 
     this.timeRemaining = timeRemaining; 
    } 
} 

堆棧跟蹤:

java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 
java.lang.IllegalArgumentException: Object: [email protected] is not a known entity type. 
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222) 
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) 
    at app.controller.TableViewController.lambda$organizeTable$3(TableViewController.java:430) 
    at java.lang.Thread.run(Thread.java:745) 

回答

3

我覺得你的問題在這裏:

} catch (Exception e) { 
     entityManager.getTransaction().rollback(); 
    } 

問題#1 - 你是壓垮例外。您不會打印該消息,也不會記錄堆棧跟蹤。沒有。您只需扔掉所有可用於診斷問題的信息。

問題#2 - 您正在捕捉Exception。這太寬泛了。

問題#3 - 看起來,當你到達呼叫rollback時,沒有事務回滾。

我認爲如果您解決問題#1和#2,問題#3的原因將被揭示。

+0

沒錯。感謝您指出了這一點。我已經添加了堆棧跟蹤並更新了我的問題。望着它... – santafebound

+0

是的。它解釋了你的問題的根源。嘗試搜索有關* that *異常消息的問答。 –

相關問題