プロセスの開始時にトランザクションが現在アクティブであるかどうかをチェックするサンプルをインターネットでいくつか見ました。
以下のコードは私のもので、ファクトリからEntityManagerを取得します。
begin()を実行する前に、トランザクションがアクティブであるかどうかを確認する必要がある理由がわかりません。
他のプロセスが同じEntityManagerインスタンスを使用している可能性があるためですか?(EntityManagerFactoryはシングルトンですが、EntityManagerはそうではありません)
@Path("update")
@PUT
@Consumes("application/json")
public Response machineUpdate(String content) {
JSONObject jObj = null;
EntityManager em = null;
EntityTransaction txn = null;
try {
JSONObject jObj = new JSONObject(content);
em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
//what's this line doing here???
if(em.getTransaction().isActive()) {
return HttpStatusHandler.sendConflict();
}
txn = em.getTransaction();
txn.begin();
//more process ......
}
catch(.....