【发布时间】:2016-08-23 12:24:57
【问题描述】:
我部署了一个 REST JSON API,在 WildFly 10 上使用 JAX-RS (RESTeasy) 和 JPA (Hibernate) 用 Java 编写。
有时,在 POST 之后立即执行 GET 需要几分钟才能完成,并且我在日志中收到多个警告/错误。
这里有一些代码:
@Stateless
@Path("/articles")
public class ArticleResource {
@PersistenceContext
private EntityManager em;
@POST
@Consumes("application/json")
public Response post(Article article) {
this.em.persist(article);
URI location = UriBuilder.fromUri("/articles/{id}").build(article.getId());
return Response.created(location).build();
}
@GET
@Path("/{id}")
@Produces("application/json")
public Article get(@PathParam("id") Long id) {
return this.em.find(Article.class, id);
}
}
那么,如果我这样做:
$ curl -i -X POST --data @article.json http://localhost/articles
我立即得到这个输出:
HTTP/1.1 201 Created
...
Location: http://localhost/articles/123
如果我立即发出 curl http://localhost/articles/123,有时返回 JSON 需要 4-6 分钟,在日志中我看到:
ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 4)
IJ031012: Unable to obtain lock in 60 seconds: org.jboss.jca.adapters.jdbc.local.LocalManagedConnection
首先:为什么它要锁定一个SELECT?我猜想在调用EntityManager.find 方法之前 请求了锁,那么我该如何注释ArticleResource.get 方法以指示容器不要请求锁?
第二:为什么在提交POST 响应时没有释放锁(我想是由先前的POST 请求设置的)?
【问题讨论】:
标签: hibernate jpa jax-rs wildfly resteasy