Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java EntityManager em.getTransaction().begin()锁定线程_Java_Jpa_Transactions_Deadlock_Entitymanager - Fatal编程技术网

Java EntityManager em.getTransaction().begin()锁定线程

Java EntityManager em.getTransaction().begin()锁定线程,java,jpa,transactions,deadlock,entitymanager,Java,Jpa,Transactions,Deadlock,Entitymanager,我的问题是,如果方法“begin()”能够锁定persistence.xml中“timeout”配置之外的线程 以下是一个片段: @Inject EntityManager em; @Inject ContextControl ctxCtrl; String fileType; String fileName; String hash; BufferedReader reader = null; public void run(File f, String fileType, String h

我的问题是,如果方法“begin()”能够锁定persistence.xml中“timeout”配置之外的线程

以下是一个片段:

@Inject EntityManager em;
@Inject ContextControl ctxCtrl;
String fileType;
String fileName;
String hash;
BufferedReader reader = null;

public void run(File f, String fileType, String hash) throws ProcessorException, IOException{    
        this.fileType = fileType;        
        this.hash= hash;
        this.fileName = f.getName();

        try { 
          ctxCtrl.startContext(RequestScoped.class);
          em.getTransaction().begin();
          reader = openReader(f);

          //rest of the code...

         em.getTransaction().commit();
        }catch (Exception e) {
          logger.error(e.getMessage(), e);
          try{ //for database breakdown purpose
            em.getTransaction().rollback();
          }catch(Exception e2){
            logger.error(e2.getMessage(), e2);
            throw new ProcessorException();
        }      
        throw new ProcessorException();      
    }finally{
      reader.close();
      ctxCtrl.stopContext(RequestScoped.class);   
    }  
“run”方法在循环内执行。此方法是串行执行的,不存在可能的并发性。 现在,问题是线程在“em.getTransaction().begin();”行随机停止,没有例外。由于这是一个关键区域,因此所有应用程序都会停止,并且锁永远不会释放

我能想到的唯一一件事是“begin()”方法不知怎么卡住了,但不是以异常的方式,而是以锁定的方式(因为没有捕获异常)。 我无法重新创建问题,我只能说问题与文件无关。而且,这是在生产中发生的,所以除了检查一些日志之外,我无法调试应用程序

提前谢谢

编辑 我使用Deltaspike提供CDI。Entitymanager在需要时随时注射。它是这样创建的:

类别实体管理器工厂生产商

import java.io.FileInputStream;
import java.util.Properties;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.slf4j.Logger;

@ApplicationScoped
public class EntityManagerFactoryProducer {
  @Inject Logger logger;

  @Produces
  @ApplicationScoped
  public EntityManagerFactory create() {
    Properties props = new Properties();
    try {
      props.load(new FileInputStream("cfg/connection.properties"));
    } catch (Exception e) {      
      logger.error(e.getMessage(), e);
    }
    return Persistence.createEntityManagerFactory("scgach",props);
  }

  public void destroy(@Disposes EntityManagerFactory factory) {
    factory.close();
  }
}
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

@ApplicationScoped
public class EntityManagerProducer {

  @Inject EntityManagerFactory emf;

  @Produces @RequestScoped
  public EntityManager create() {
    return emf.createEntityManager();
  }

  public void destroy(@Disposes EntityManager em) {
    if(em.isOpen())
      em.close();
  }
}
班级实体管理器制作人

import java.io.FileInputStream;
import java.util.Properties;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.slf4j.Logger;

@ApplicationScoped
public class EntityManagerFactoryProducer {
  @Inject Logger logger;

  @Produces
  @ApplicationScoped
  public EntityManagerFactory create() {
    Properties props = new Properties();
    try {
      props.load(new FileInputStream("cfg/connection.properties"));
    } catch (Exception e) {      
      logger.error(e.getMessage(), e);
    }
    return Persistence.createEntityManagerFactory("scgach",props);
  }

  public void destroy(@Disposes EntityManagerFactory factory) {
    factory.close();
  }
}
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

@ApplicationScoped
public class EntityManagerProducer {

  @Inject EntityManagerFactory emf;

  @Produces @RequestScoped
  public EntityManager create() {
    return emf.createEntityManager();
  }

  public void destroy(@Disposes EntityManager em) {
    if(em.isOpen())
      em.close();
  }
}

您的事务隔离设置是什么?@Kayaman您是否检查了数据库和连接池,以确定没有任何可疑的情况发生?@Kayaman这是在生产环境中发生的,我无权访问生产环境。我们使用hibernate+c3p0和MS SQL server 2008。请与您的服务器人员联系,并询问他们有关情况。