JPA线程安全地访问数据库

JPA线程安全地访问数据库

本文介绍了如何使Hibernate JPA线程安全地访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的实体类:
我想知道我需要做什么才能访问数据库线程安全。 $ b

  @Entity 
@Table(name =students)
@NamedQuery(name =Student.getAll,query =SELECT s FROM Student's)
public class Student {

@Id
@GeneratedValue(strategy = GenerationType。 AUTO)
私人长ID;

@Column(length = 32,name =name)
私有字符串名称;

// ...构造函数,getters和setters,toString ...
}

这是DbService类:

  public class DbService {

public EntityManager em = Persistence
.createEntityManagerFactory(MyPersistenceUnit)
.createEntityManager();

public Student add(Student student){
EntityTransaction tx = em.getTransaction();
tx.begin();
Student studentFromDb = em.merge(student);
tx.commit();
返回studentFromDb;
}

public void delete(long id){
EntityTransaction tx = em.getTransaction();
tx.begin();
em.remove(get(id));
tx.commit();
}

public Student get(long id){
return em.find(Student.class,id);
}

public void update(Student student){
EntityTransaction tx = em.getTransaction();
tx.begin();
em.merge(student);
tx.commit();
}

公共列表< Student> getAll(){
TypedQuery< Student> namedQuery =
em.createNamedQuery(Student.getAll,Student.class);
返回namedQuery.getResultList();
}

}

这里有一个类与DbService:

public class SomeDbWorker实现Runnable {
@Override
public void run(){
DbService service = new DbService();

//做某事...
service.add(...);
service.delete(...);
service.getAll();
// ...
}
}







  1. 是否足够使得add(), delete() update em>和 getAll()方法是否同步?

  2. 我可以创建多个DbService实例吗?或者我只需要创建一个实例?

  3. 也许我应该使用单例设计模式?或使DbService静态使用所有方法?


解决方案

无需同步任何内容,

>

entityManager不是线程安全的,它被设计为为每个工作单元实例化并在之后销毁。

相反,工厂的创建成本很高,应该重复使用

请参阅应用程序管理的实体管理器


I would like to know what I need to do in order to make the access to DB thread-safe.

This is my Entity class:

@Entity
@Table(name = "students")
@NamedQuery(name = "Student.getAll", query = "SELECT s FROM Student s")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(length = 32, name = "name")
    private String name;

    // ... constructor, getters and setters, toString ...
}

This is the DbService class:

public class DbService {

    public EntityManager em = Persistence
                         .createEntityManagerFactory("MyPersistenceUnit")
                         .createEntityManager();

    public Student add(Student student) {
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        Student studentFromDb = em.merge(student);
        tx.commit();
        return studentFromDb;
    }

    public void delete(long id) {
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.remove(get(id));
        tx.commit();
    }

    public Student get(long id) {
        return em.find(Student.class, id);
    }

    public void update(Student student) {
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.merge(student);
        tx.commit();
    }

    public List<Student> getAll() {
        TypedQuery<Student> namedQuery =
                     em.createNamedQuery("Student.getAll", Student.class);
        return namedQuery.getResultList();
    }

}

And here is a class that work with DbService:

public class SomeDbWorker implements Runnable {
    @Override
    public void run() {
        DbService service = new DbService();

        // do something ...
        service.add( ... );
        service.delete( ... );
        service.getAll();
        // ...
    }
}


  1. Is it enough making add(), delete(), update() and getAll() methods synchronized?
  2. Can I create multiple instances of DbService like in my source? Or do I need to create only one instance?
  3. Maybe I should to use the singleton design pattern? Or make DbService static with all methods?
解决方案

No need to synchronize anything,

the entityManager is not threadSafe and designed to be instantiated for each unit of work and destroyed just after.

The factory on the contrary is costly to create and should be reused

See http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html "Application-Managed Entity Managers"and https://stackoverflow.com/a/22773758/2087640

这篇关于如何使Hibernate JPA线程安全地访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:04