简体   繁体   English

什么时候连接返回到JPA应用程序中的连接池?

[英]When is a connection returned to the connection pool in a JPA application?

Is a connection only returned to the Connection pool in a JPA application if i call 如果我调用,连接仅返回到JPA应用程序中的连接池

entityManager.close();

?

Can the connection backing the entitymanger change during its lifecycle? 支持实体经理的连接在其生命周期中是否会发生变化?

thanks in advance mojoo 感谢提前mojoo

The JPA spec doesn't define such things and its up to the implementation to manage connections. JPA规范没有定义这样的东西,也没有定义管理连接的实现。 When a transaction is active you'd be safe to assume the connection is the same until commit, for obvious reasons. 当事务处于活动状态时,出于显而易见的原因,您可以安全地假设连接在提交之前是相同的。 Once the txn ends it may be handed back, or it may be held depending on implementation (and you don't mention yours) 一旦txn结束,它可以被交还,或者可以根据实施情况保留(并且您不提及您的)

This depends on the JPA implementation and configuration. 这取决于JPA实现和配置。

In EclipseLink by default a connection is only held for the duration of an active (dirty) transaction. 在EclipseLink中,默认情况下,连接仅在活动(脏)事务的持续时间内保持。 ie from the first modification or lock, until the commit or rollback. 即从第一次修改或锁定,直到提交或回滚。 For non-transactional queries a connection is acquired on demand and returned after the query execution. 对于非事务性查询,按需获取连接,并在查询执行后返回。 This allows for maximal usage of connection pooling. 这允许最大限度地使用连接池。 So, normally em.close() does nothing. 所以,通常em.close()什么都不做。

You can configure this using the "eclipselink.jdbc.exclusive-connection.mode" persistence unit property. 您可以使用“eclipselink.jdbc.exclusive-connection.mode”持久性单元属性对其进行配置。 "Always" will hold a connection for the life of the EntityManager. “Always”将保持EntityManager生命周期的连接。

You can also use different connection pools for transactions, versus non-transactional reads. 您还可以使用不同的连接池进行事务处理,而不使用非事务性读取。 This is useful with JTA, as you can use a non-JTA DataSource for reads. 这对JTA非常有用,因为您可以使用非JTA DataSource进行读取。

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

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