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

Posted

技术标签:

【中文标题】线程“Thread-10”中的异常 java.lang.IllegalStateException【英文标题】:Exception in thread "Thread-10" java.lang.IllegalStateException 【发布时间】:2017-06-15 11:32:00 【问题描述】:

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

对象创建和存储线程:

    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】:

我认为你的问题在这里:

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

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

问题 #2 - 您正在捕获 Exception。这太宽泛了。

问题 #3 - 当您调用 rollback 时,似乎没有事务可以回滚。

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

【讨论】:

对。感谢您指出了这一点。我添加了堆栈跟踪并更新了我的问题。调查它... 是的。它解释了问题的根本原因。尝试搜索关于那个异常消息的问答。

以上是关于线程“Thread-10”中的异常 java.lang.IllegalStateException的主要内容,如果未能解决你的问题,请参考以下文章

Android:当我使用 AsyncTask 时出现“调用线程必须是准备好的 Looper 线程”错误

终止剩余的线程

C#多线程开发-处理异步操作中的异常

多线程多线程中的异常处理

线程中的终极异常处理处理

C#多线程开发-处理子线程中的异常