在相对较大的应用程序中,应如何使用实体管理器管理数据库事务? [英] How should manage Database transactions using Entity Manager in a relatively large application?

查看:93
本文介绍了在相对较大的应用程序中,应如何使用实体管理器管理数据库事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MYSQL数据库和Swing应用程序框架以及javax.persistence开发了一个相当大的CRUD应用程序. 我的问题是,鉴于javax.persistence.Entitymanager,我应该如何最好地管理我的交易? 目前,我有一个由应用程序类持有的实体管理器实例.它被传递到所有请求的页面,这些页面继而使用它来持久化和合并实体.我在应用程序启动时启动事务,并在每次进行更改时提交(并重新启动). 这样对吗?还是应该为每个组件/页面分别持有一个实体管理器? 我什么时候提交? 现在出现了所有这些问题,因为我最近开始发现以下类型的异常: java.sql.SQLException:超出了锁定等待超时;尝试重新启动事务,错误代码:1205 这使我相信我在数据库事务管理中做错了什么....

I have developed a fairly large CRUD application, using a MYSQL Database and Swing Application framework and javax.persistence. My question is how should I best manage my transactions given the javax.persistence.Entitymanager? Currently, I have one instance of Entity manager held by the Application class. It is passed to all requesting pages, which in turn use it to persist and merge entities. I start a transaction on application start-up, and commit (and restart) each time a change is made. Is this correct? or should I hold a seperate Entity Manager for each Component / page? When should I commit? All these questions arised now, because I have recently started to get exceptions of the type: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction Error Code: 1205 which led me to believe I am doing something wrong in my management of database transactions....

在此先感谢您能为我提供的任何帮助!

Thanks in advance for any help you can give me!

推荐答案

在应用程序启动时启动事务不是最好的主意.事务的寿命应尽可能短,因为每个事务都会锁定数据库.我的意思是,每次启动事务时,其他线程都无法将其写入数据库.您做事的方式正好相反:您的数据库并不仅在短时间内被锁定.这可能是导致您收到错误的原因.

Starting a transaction on application start-up is not the best idea. Transactions should be as short lived as possible because each of them locks the database. I mean, each time a transaction is started, no other thread can write to the database. The way you are doing things is exactly the opposite: your database is not locked only only during small periods of time. That is the likely cause of the error you are getting.

通常,推荐的管理交易的方法如下:

In general, the recommended way of managing the transaction is the following:

EntityManager em = EMF.getEM();
    em.getTransaction().begin();
    // your persist, merge code goes here
    em.getTransaction().commit();
    em.close();

EMF类是这样的:

public class EMF {
    private static EntityManagerFactory emf;
    static {
        emf = Persistence.createEntityManagerFactory("MyEMF");
    }
    public static EntityManager getEM(){
        return emf.createEntityManager();
    }
}

这样,您的事务仅在您的持久性代码执行期间锁定数据库.请注意,使用EMF类,您的实体管理器工厂仅创建一次.这很好,因为创建它在计算上很昂贵.但是,一旦创建了实体管理器,则创建实体管理器的实例非常便宜. 简短教程对此进行了很好的解释.

This way your transaction only locks the database during the time your persistence code is executing. Note that using the class EMF your entity manager factory is created only once. This is good because creating it is computationally expensive. However, once it is created making an instance of the entity manager is very cheap. This short tutorial explains it fairly well.

这篇关于在相对较大的应用程序中,应如何使用实体管理器管理数据库事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆