线程“Thread-10”中的异常java.lang.IllegalStateException

时间:2017-06-15 11:32:00

标签: java multithreading hibernate javafx h2

我不太确定如何解决这个问题,除了我试图填充数据的四个表中的每个表,有时其中一些只是空的。我无法预测哪些是空的。它可以是1和2,或者只是3,或者只是一个表。当其中一个表未填充时,我最终会进入rollback()catch块。有时它们都有效。这与某种线程中断有关。鉴于以下代码和例外,会出现什么问题?

用于创建和存储对象的线程:

    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: app.model.Incident@3852db6c 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: app.model.Incident@50a1f930 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: app.model.Incident@2e557d5 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: app.model.Incident@7167a75 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: app.model.Incident@13b2cc25 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: app.model.Incident@460c35f7 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: app.model.Incident@194aa755 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: app.model.Incident@599e1650 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: app.model.Incident@3d568923 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: app.model.Incident@21623389 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: app.model.Incident@85fa696 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: app.model.Incident@19263140 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: app.model.Incident@13de5a30 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: app.model.Incident@7ed84185 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: app.model.Incident@62eea24 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: app.model.Incident@a1b8252 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: app.model.Incident@1d2a61de 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: app.model.Incident@2c52980f 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: app.model.Incident@15e2781d 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: app.model.Incident@64e2ee25 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: app.model.Incident@26aaf37f 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: app.model.Incident@5b020754 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: app.model.Incident@23c1fd3e 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: app.model.Incident@48f40902 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: app.model.Incident@38c553b0 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: app.model.Incident@6f1ddc45 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: app.model.Incident@3a66baff 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: app.model.Incident@3e0db801 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)

1 个答案:

答案 0 :(得分:3)

我认为你的问题在这里:

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

问题#1 - 你正在挤压异常。您不打印信息,也不记录堆栈跟踪。没有。您只需丢弃可用于诊断问题的所有信息。

问题#2 - 你正在追赶Exception。这太宽泛了。

问题#3 - 当你到达调用rollback时,似乎没有要回滚的事务。

如果您解决问题#1和#2,我希望问题#3的原因会被揭示。