简体   繁体   English

EntityManager JDBC连接用法

[英]EntityManager JDBC connection usage

@PersistenceContext(unitName = "myPU")
private EntityManager em;

    public void run1()
    {
       // uses em
    }
    public void run2()
    {
       // uses em
    }

As per my understanding each of the methods represent different transactions. 根据我的理解,每种方法代表不同的事务。
I'm having hard times figuring out what the equivalent 我很难弄清楚什么是等效的
code using plain JDBC could be,especially when it comes to JDBC connection usage. 可以使用纯JDBC编写代码,尤其是在使用JDBC连接时。

Is commit issued at the end of each method ? 提交是否在每种方法的末尾发出?
Is a new connection created and closed for each method ? 是否为每个方法创建并关闭了新连接?
What does it mean that the implemenation could use a connection pool ? 该实现可以使用连接池是什么意思?

As in most cases: It depends. 与大多数情况一样:这要视情况而定。

In your case, if the methods are being invoked in different transactions, you'd get an EntityManager per bean. 在您的情况下,如果在不同的事务中调用这些方法,则每个bean将获得一个EntityManager

Usually the acquisition of an SQL connection is lazy. 通常,SQL连接的获取是延迟的。 So when an EntityManager requires a connection the first time, it will fetch one from the pool you configured. 因此,当EntityManager首次需要连接时,它将从您配置的池中获取一个连接。 The container then makes sure that the connection is unavailable to other transactions. 然后,容器确保该连接对其他事务不可用。 When the transaction finishes, transactional operations such as commit or rollback are managed by the container, too. 事务完成后,容器也将管理诸如commitrollback类的事务操作。

Is commit issued at the end of each method ? 提交是否在每种方法的末尾发出?

No, at the end of each transaction. 不,在每笔交易结束时。 If your methods starts new transactions, then yes. 如果您的方法启动了新事务,则可以。

Is a new connection created and closed for each method ? 是否为每个方法创建并关闭了新连接?

The same here. 和这里一样。 If a transaction is in progress and a connection from the same pool has been used before, then it will be reused. 如果事务正在进行中,并且以前使用过来自同一池的连接,则它将被重用。 Otherwise you wouldn't see your own changes in some other operations. 否则,您将不会在其他操作中看到自己的更改。 If your methods start new transactions, then yes, you'd get new connections (usually just new wrapper instances around pooled connections to reduce connect/disconnect overheads) and they'd be released at the end of the transaction no matter the outcome. 如果您的方法开始新的事务,则可以,您将获得新的连接(通常只是池连接周围的新包装实例,以减少连接/断开连接的开销),无论结果如何,它们都将在事务结束时释放。

What does it mean that the implemenation could use a connection pool ? 该实现可以使用连接池是什么意思?

I think I don't completely understand your question. 我想我不完全理解你的问题。

Depending on your container, you usually deal with connection pools anyway. 无论如何,通常取决于容器,通常要处理连接池。 So does the EntityManager in your case. 如此做EntityManager你的情况。

Connection pooling is a way of reusing the same physical connections over and over again to, as mentioned earlier, reduce connect/disconnect overheads. 连接池是一种反复使用相同物理连接以减少连接/断开连接开销的方法。

Please let me know, if something is still unclear. 如果仍然不清楚,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM