删除Java批处理作业(JSR352)中的数据

huangapple go评论63阅读模式
英文:

Delete data in Java Batch Job (JSR352)

问题

以下是您要翻译的内容:

"While I have Java Batch jobs that read data, process it and store it in other places in the database, now I need a step to actually remove data from the database. All I need to run is a delete query via JPA.

The chunk based Reader/Processor/Writer pattern does not make sense here. But the Batchlet alternative is giving me a headache either. What did I do?

I created a Batchlet that gets invoked via CDI. At that moment it is easy to inject my JPA EntityManager. What is not easy is to run the update query. Code looks like this:

package ...;

import javax.batch.api.BatchProperty;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Named("CleanerBatchlet")
public class CleanerBatchlet extends AbstractBatchlet {
    public static that Logger log = LogManager.getLogger(CleanerBatchlet.class);

    @PersistenceContext(unitName = "...")
    private EntityManager entityManager; 

    @Inject
    @BatchProperty(name = "technologyIds")
    private String technologyIds;

    private void clearQueue(long technologyId) {
        //EntityManager entityManager = ...getEntityManager();

        //entityManager.getTransaction().begin();
        Query q = entityManager.createQuery("delete from Record r where r.technologyId=:technologyId");
        q.setParameter("technologyId", technologyId);
        int count = q.executeUpdate();
        //entityManager.getTransaction().commit();
        log.debug("Deleted {} entries from queue {}", count, technologyId);

        //entityManager.close();
    }

    @Override
    public String doProcess() throws Exception {
        log.debug("doProcess()");
        out.println("technologyIds=" + technologyIds);
        log.info("technologyIds=" + technologyIds);
        try {
            String[] parts = technologyIds.split(",");
            for (String part: parts) {
                long technologyId = Long.parseLong(part);
                clearQueue(technologyId);
            }
        } catch (NullPointerException | NumberFormatException e) {
            throw new IllegalStateException("technologyIds must be set to a string of comma-separated numbers.", e);
        }
        return "COMPLETED"
    }

}"


<details>
<summary>英文:</summary>

While I have Java Batch jobs that read data, process it and store it in other places in the database, now I need a step to actually remove data from the database. All I need to run is a delete query via JPA.

The chunk based Reader/Processor/Writer pattern does not make sense here. But the Batchlet alternative is giving me a headache either. What did I do?

I created a Batchlet that gets invoked via CDI. At that moment it is easy to inject my JPA EntityManager. What is not easy is to run the update query. Code looks like this:

    package ...;
    
    import javax.batch.api.BatchProperty;
    import javax.inject.Inject;
    import javax.inject.Named;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    @Named(&quot;CleanerBatchlet&quot;)
    public class CleanerBatchlet extends AbstractBatchlet {
        public static final Logger log = LogManager.getLogger(CleanerBatchlet.class);
    
        @PersistenceContext(unitName = &quot;...&quot;)
        private EntityManager entityManager;  
    
        @Inject
        @BatchProperty(name = &quot;technologyIds&quot;)
        private String technologyIds;
        
        private void clearQueue(long technologyId) {
            //EntityManager entityManager = ...getEntityManager();
            
            //entityManager.getTransaction().begin();
            Query q = entityManager.createQuery(&quot;delete from Record r where r.technologyId=:technologyId&quot;);
            q.setParameter(&quot;technologyId&quot;, technologyId);
            int count = q.executeUpdate();
            //entityManager.getTransaction().commit();
            log.debug(&quot;Deleted {} entries from queue {}&quot;, count, technologyId);
            
            //entityManager.close();
        }
        
        @Override
        public String doProcess() throws Exception {
            log.debug(&quot;doProcess()&quot;);
            out.println(&quot;technologyIds=&quot; + technologyIds);
            log.info(&quot;technologyIds=&quot; + technologyIds);
            try {
                String[] parts = technologyIds.split(&quot;,&quot;);
                for (String part: parts) {
                    long technologyId = Long.parseLong(part);
                    clearQueue(technologyId);
                }
            } catch (NullPointerException | NumberFormatException e) {
                throw new IllegalStateException(&quot;technologyIds must be set to a string of comma-separated numbers.&quot;, e);
            }
            return &quot;COMPLETED&quot;;
        }
    
    }

As you can see some lines are commented out - these are the ones I am experimenting with.
So if I run the code as-is, I get an exception telling me that the update query requires a transaction. This is regardless of which of the two persistence units in my project I use (one is configured for JTA, the other is not).

    javax.persistence.TransactionRequiredException: Executing an update/delete query

It also does not matter whether I uncomment the transaction handling code begin/commit. I still get the same error that a transaction is required to run the update query.

Even when I try to circumvent CDI and JTA completely by creating my own EntityManager via the Persistence API (and close it afterwards, respectively) I do get the very same exception.

So how can I run this delete query or other update queryies from within the batch job?

</details>


# 答案1
**得分**: 1

我建议使用普通的JDBC来运行此删除查询,可以选择自动提交或手动事务提交。

在批处理过程中,传入的事务被挂起,因此实体管理器没有事务上下文。

<details>
<summary>英文:</summary>

I&#39;d suggest using plain jdbc to run this delete query, with either auto commit or manual transaction commit.

During the batchlet processing, the incoming transaction is suspended. So the entity manager does not have a transaction context. 

</details>



# 答案2
**得分**: 1

I made it work by following this tutorial: https://dzone.com/articles/resource-local-vs-jta-transaction-types-and-payara and going for the *Classic RESOURCE_LOCAL Application* pattern.

It involves injecting the nonJTA EntityManagerFactory, using that to create the entitymanager and closing it after use. Of course the transaction has to be managed manually but after all now it works.

The essential excerpt of my code looks like this:

```java
@PersistenceUnit(unitName = "...")
private EntityManagerFactory emf;

@Inject
@BatchProperty(name = "technologyIds")
private String technologyIds;

private void clearQueue(long technologyId) {
    EntityManager entityManager = emf.createEntityManager();
    entityManager.getTransaction().begin();
    Query q = entityManager.createQuery("delete from Record r where r.technologyId=:technologyId");
    q.setParameter("technologyId", technologyId);
    q.executeUpdate();
    entityManager.getTransaction().commit();
    entityManager.close();
}
英文:

Ultimately I made it work by following this tutorial: https://dzone.com/articles/resource-local-vs-jta-transaction-types-and-payara
and going for the Classic RESOURCE_LOCAL Application pattern.

It involves injecting the nonJTA EntityManagerFactory, using that to create the entitymanager and closing it after use. Of course the transaction has to be managed manually but after all now it works.

The essential excerpt of my code looke like this:

@PersistenceUnit(unitName = &quot;...&quot;)
private EntityManagerFactory emf;  
@Inject
@BatchProperty(name = &quot;technologyIds&quot;)
private String technologyIds;
private void clearQueue(long technologyId) {
EntityManager entityManager = emf.createEntityManager(); 
entityManager.getTransaction().begin();
Query q = entityManager.createQuery(&quot;delete from Record r where r.technologyId=:technologyId&quot;);
q.setParameter(&quot;technologyId&quot;, technologyId);
q.executeUpdate();
entityManager.getTransaction().commit();
entityManager.close();
}

huangapple
  • 本文由 发表于 2023年2月7日 04:34:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定